(USING C++ NoT JAVA) A scout team consists of 5 children. There are 4 duties wit
ID: 3751475 • Letter: #
Question
(USING C++ NoT JAVA) A scout team consists of 5 children. There are 4 duties with a day off to be rotated among team members. Duties are: Fetching water, collecting firewood, cooking and clean up. You are to write a program that prompts the user for the desired number of working days and prints a schedule of daily duties for the team. Use two separate liststo store team member names and the duties.
The program must read the team list and task list (both comma-separated) from an input file input.txt, which may look like the example hereafter.
Mohd,Ali,John,Alex,Mike
Water,Firewood,Cooking,Cleaning,NoDuty
Below is the code to read a line from a text file and separate it into tokens:
ifstream ifile;
ifile.open( "input.txt" );
char word[300];
ifile.getline( word, 100 );
char* token = strtok( word, "," );
while ( token != NULL ) {
// Your code here
token = strtok( NULL, "," );
}
Explanation / Answer
ScreenShot
---------------------------------------------
Program
//Header files for I/O,file read ,list STL and iteration
#include<iostream>
#include<string>
#include<fstream>
#include <list>
#include <iterator>
using namespace std;
//Main mtehod
int main()
{
//File read object
ifstream ifile;
//List for file read data store
list<string> team_mamber_names , team_member_duties;
//Two iterators
list <string> ::iterator it;
list <string> ::iterator it1;
//File open
ifile.open("C:/Users/deept/Desktop/input.txt");
int i = 0,num;
char word[300];
//File read
while (!ifile.eof()) {
ifile.getline(word, 100);
//Get each word
char* token = strtok(word, ",");
while (token != NULL) {
//If 5 words reached then store neat words in duty list other in name list
if (i < 5) {
i++;
team_mamber_names.push_back(token);
token = strtok(NULL, ",");
}
else {
team_member_duties.push_back(token);
token = strtok(NULL, ",");
}
}
}
//prompt user for work schedule day count
cout << "Enter number of working days: ";
cin >> num;
//Loop for days duty desription
for (i = 1; i <= num; i++) {
//First day and all other days after 4 days
if (i % 5 == 1) {
it = team_mamber_names.begin();
it1 = team_member_duties.begin();
cout << "Day " << i << " ----------" << endl;
for (int j = 0; j < 5; j++) {
cout << "Student Name= " << *it << " Duty= " << *it1 << endl;
advance(it, 1);
advance(it1, 1);
}
}
//Second day and all other days after 4 days
if (i % 5 == 2) {
it = team_mamber_names.begin();
it1 = team_member_duties.begin();
advance(it1, 1);
cout << "Day " << i << " ----------" << endl;
for (int j = 0; j < 5; j++) {
if (j == 4) {
cout << "Student Name= " << *it << " Duty= " << team_member_duties.front() << endl;
advance(it, 1);
}
else {
cout << "Student Name= " << *it << " Duty= " << *it1 << endl;
advance(it, 1);
advance(it1, 1);
}
}
}
//Third day and all other days after 4 days
if (i % 5 == 3) {
it = team_mamber_names.begin();
it1 = team_member_duties.begin();
advance(it1, 2);
cout << "Day " << i << " ----------" << endl;
for (int j = 0; j < 5; j++) {
if (j >= 3) {
if (j > 3) {
cout << "Student Name= " << *it << " Duty= " << *it1 << endl;
advance(it, 1);
}
else {
it1 = team_member_duties.begin();
cout << "Student Name= " << *it << " Duty= " << *it1 << endl;
advance(it, 1);
advance(it1, 1);
}
}
else {
cout << "Student Name= " << *it << " Duty= " << *it1 << endl;
advance(it, 1);
advance(it1, 1);
}
}
}
//Fourth day and all other days after 4 days
if (i % 5 == 4) {
it = team_mamber_names.begin();
it1 = team_member_duties.begin();
advance(it1, 3);
cout << "Day " << i << " ----------" << endl;
for (int j = 0; j < 5; j++) {
if (j >= 2) {
if (j > 2) {
cout << "Student Name= " << *it << " Duty= " << *it1 << endl;
advance(it, 1);
advance(it1, 1);
}
else {
it1 = team_member_duties.begin();
cout << "Student Name= " << *it << " Duty= " << *it1 << endl;
advance(it, 1);
advance(it1, 1);
}
}
else {
cout << "Student Name= " << *it << " Duty= " << *it1 << endl;
advance(it, 1);
advance(it1, 1);
}
}
}
//Fifth day and all other days after 4 days
if (i % 5 == 0) {
it = team_mamber_names.begin();
it1 = team_member_duties.begin();
advance(it1, 4);
cout << "Day " << i << " ----------" << endl;
for (int j = 0; j < 5; j++) {
if (j >= 1) {
if (j > 1) {
cout << "Student Name= " << *it << " Duty= " << *it1 << endl;
advance(it, 1);
advance(it1, 1);
}
else {
it1 = team_member_duties.begin();
cout << "Student Name= " << *it << " Duty= " << *it1 << endl;
advance(it, 1);
advance(it1, 1);
}
}
else {
cout << "Student Name= " << *it << " Duty= " << *it1 << endl;
advance(it, 1);
}
}
}
}
return 0;
}
---------------------------------
Output
Enter number of working days: 2
Day 1
----------
Student Name= Mohd Duty= Water
Student Name= Ali Duty= Firewood
Student Name= John Duty= Cooking
Student Name= Alex Duty= Cleaning
Student Name= Mike Duty= NoDuty
Day 2
----------
Student Name= Mohd Duty= Firewood
Student Name= Ali Duty= Cooking
Student Name= John Duty= Cleaning
Student Name= Alex Duty= NoDuty
Student Name= Mike Duty= Water
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.