Hi, I need help with this C++ problem. There is no need to use any structs, or c
ID: 3685199 • Letter: H
Question
Hi, I need help with this C++ problem. There is no need to use any structs, or classes, or pointers for this assignment. It should be very simple, should use char arrays, loops, and keep a running total of the amount. The menu should be displayed in a loop until the user enters ‘q’ to quit. 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
The program should do the following:
Have you ever been low on cash and couldn’t go beyond a certain dollar limit when shopping? You sort of need a calculator in your head. It would be cool if a device was actually part of the cart and as you add an item into the cart it would increment your total counter. To solve this, we are going to write a program that keeps a tally of the amount that we have spent so far as we visit a store.
1. Allow the shopper (user) to enter in the product name and the cost. This should be echoed and confirmed. Make sure to check for bad data.
2. Output the total thus far.
3. The user should be allowed to continue this until they want to checkout.
4. Your program needs to keep a running total.
5. Upon checkout, the grand total should be displayed.
6. Display money using proper formatting. You will need to have the following before displaying dollars and cents: cout.setf(ios::fixed, ios::floatfield);cout.setf(ios::showpoint); cout.precision(2);
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
/* 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.