Do-Not List: No Global Variables (you can have global constants) No use of the s
ID: 3543822 • Letter: D
Question
Do-Not List:
No Global Variables (you can have global constants)
No use of the stdio library (use iostream and fstream)
Instead of the string class, you will be using arrays of characters and the cstring library
Problem You Need to Solve for This Lab:
You have noticed that there is a lot to do this term as we get familiar with C and programming on a larger scale. With a full load of classes, you may have many due dates to meet. Keeping track of everything is important. You have decided to write a program that reads tasks from the user and saves them in a data file to keep track of.
What Your Program Should Do:
Write an interactive text based menu interface (using a loop) that will allow the user to
Enter a task or assignment
Display all of the tasks that are in the file
Find a task by Course
Quit
For each task, you need to keep track of:
Course Name that it is for (e.g., CS162)
Description of the assignment (e.g., Finish Lab 2)
Due date (e.g., 9/26/2009)
Allow the program to keep looping until user wants to quit. When user enters the three items of a task, you need to read them in and write them to the external data file ("tasks.txt"). The file format could look like: (The ';' is used as a delimiter or field seperator.)
CS162;Finish Lab 2;9/26/2009
CS201;Take Quiz 1;9/28/2009
Some Implementation Requirements:
Write at least four functions WITH arguments for this assignment.
Echo your input and ask the user if it is correct (have a confirmation loop again!)
Hint: In this assignment, the description and course name may have multiple words in it. Therefore, you now SHOULD read using the 3 argument version of get.
Watch out. When using the 3 argument version of get you need to make sure to remove the delimiter or newline. Therefore, anytime you read (even a confirmation message), make sure to eat the newline!
Make sure to have a delimiter written between each item in the file
Explanation / Answer
#include<iostream>
#include<iomanip>
using namespace std;
/* structure to store data */
struct list{
char name[100];
double cost;
struct list *next;
};
/* This function will add data to the list having fist
node pointed by f
*/
void add(struct list **f,char *n,double p){
struct list *first,*ptr,*ptr1;
first = *f;
if(first ==NULL)
{
ptr=*f = new struct list;
strcpy(ptr->name,n);
ptr->cost = p;
ptr->next = NULL;
}
else
{
ptr1 = first;
while(ptr1 != NULL)
{
ptr=ptr1;
ptr1=ptr1->next;
}
ptr->next = new struct list;
ptr = ptr->next;
strcpy(ptr->name,n);
ptr->cost = p;
ptr->next = NULL;
}
}
/* Display the list data with total cost using
setf,setprecision and showpoint
*/
void display(struct list **f){
struct list *ptr;
double amount=0;
ptr=*f;
cout<<endl;
while(ptr != NULL)
{
if(strcmp(ptr->name,"")!=0)
{
cout<<ptr->name<<endl;
cout.setf(ios::fixed);
cout << setprecision(2) <<showpoint <<ptr->cost<<endl<<endl;
amount += ptr->cost;
}
ptr = ptr->next;
}
cout<<"-------------------------"<<endl<<"Total cost is :" ;
cout.setf(ios::fixed);
cout << setprecision(2)<<showpoint <<amount <<endl;
}
/* This function will delete data from list by name */
void delete_by_name(struct list **f,char *name){
struct list *ptr;
ptr=*f;
while(ptr != NULL)
{
if(strcmp(ptr->name,name)==0)
strcpy(ptr->name,"");
ptr = ptr->next;
}
}
/* Validate the input cost */
bool validate(char *ptr){
int len,i;
len =strlen(ptr);
for(i=0;i<len;i++)
if(!((ptr[i] >='0' && ptr[i] <='9' ) || ptr[i]=='.'))
{
cout<<"Invalid cost "<<endl<<"Enter again :";
return false;
}
return true;
}
int main(){
struct list *first= NULL,*ptr;
char name[100],ch;double cost;char cost_s[100];
/*Run until user don't checkout */
while(1){
cout<<"Enter name:";
do{
cin.getline(name,sizeof(name));
/* Loop til user enter name */
}while(strcmp(name,"")==0);
cout<<"Enter cost :";
do{
cin.getline(cost_s,sizeof(cost_s));
/* Loop til user don't enter cost or enter invalid cost*/
}while(strcmp(cost_s,"")==0 || validate(cost_s)==false);
cost = atof(cost_s); /* convert to double */
add(&first,name,cost);
display(&first);
cout<<"Do you want to delete cany data ? Y/N";
cin>>ch;
if(ch=='y' || ch=='Y'){
cout<<"Enter name:";
do{
cin.getline(name,sizeof(name));
/* Loop til user enter name */
}while(strcmp(name,"")==0);
delete_by_name(&first,name);
display(&first);
}
cout<<"Do you want to checkout ? Y/N :";
cin>>ch;
if(ch =='y' || ch =='Y')
break;
}
display(&first);
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.