Write a program that uses a dynamic list of strings to keep track of a list of c
ID: 3792944 • Letter: W
Question
Write a program that uses a dynamic list of strings to keep track of a list of chores that you have to accomplish today. The user of the program can request several services: Add an item to the list of chores; Ask how many chores are in the list; Have the list of chores printed to the screen; Delete an item from the list, Exit the program If you know how to read and write strings from a file, then have the program obtain its initial list of chores from a file. When the program ends, it should write all unfinished chores back to this file.Explanation / Answer
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
std::vector <std::string> chores; // Vector to hold our words we read in.
std::string str; // Temp string to
std::ifstream fin("chores.txt"); // Open it up!
while (fin >> str) // Will read up to eof() and stop at every
{ // whitespace it hits. (like spaces!)
chores.push_back(str);
}
fin.close(); // Close that file!
int c;
while(c!=5)
{
cout<<" 1:Add an item to list of chores 2:Ask how many chores are in list 3:Have the list of chore printed to Screen 4:Delete an item from the list 5:Exit Enter you choice:";
cin>>c;
if(c==1)
{string s;
cout<<"Enter item you want to Add:";cin>>s;//taking user input
chores.push_back(s);//adding item ..to list
}
else if(c==2)
{
cout<<"Total number of chores:"<<chores.size()<<endl; //printing list size
}
else if(c==3)
{
cout<<"Items are: ";
for (int i = 0; i < chores.size(); ++i)
std::cout << chores.at(i) << std::endl; // printing items
}
else if(c==4)
{
string s;
cout<<"Enter item name you want to delete:";cin>>s;//taking user input.
for (int i = 0; i < chores.size(); ++i)
{
if(s.compare(chores.at(i))==0)//finding approriate item to delete
{
//erasing from list..
chores.erase(chores.begin()+i);cout<<"item removed ";break;
}
}
}
ofstream OFileObject; // Create Object of Ofstream
OFileObject.open ("chores.txt"); // Opening a File
for (int i = 0; i < chores.size(); ++i)//writing to files..
{OFileObject<<chores[i]<<" ";}
OFileObject.close(); // Closing the file
}
return 0;
}
ouput:-
1:Add an item to list of chores
2:Ask how many chores are in list
3:Have the list of chore printed to Screen
4:Delete an item from the list
5:Exit
Enter you choice:1
Enter item you want to Add:pool
1:Add an item to list of chores
2:Ask how many chores are in list
3:Have the list of chore printed to Screen
4:Delete an item from the list
5:Exit
Enter you choice:1
Enter item you want to Add:job
1:Add an item to list of chores
2:Ask how many chores are in list
3:Have the list of chore printed to Screen
4:Delete an item from the list
5:Exit
Enter you choice:2
Total number of chores:6
1:Add an item to list of chores
2:Ask how many chores are in list
3:Have the list of chore printed to Screen
4:Delete an item from the list
5:Exit
Enter you choice:3
Items are:
running
swimming
archestra
jogging
pool
job
1:Add an item to list of chores
2:Ask how many chores are in list
3:Have the list of chore printed to Screen
4:Delete an item from the list
5:Exit
Enter you choice:4
Enter item name you want to delete:job
item removed
1:Add an item to list of chores
2:Ask how many chores are in list
3:Have the list of chore printed to Screen
4:Delete an item from the list
5:Exit
Enter you choice:5
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.