Question

How to stop the program from exiting after display detail. When there is food detail, it...

How to stop the program from exiting after display detail. When there is food detail, it will display and exit the program. What can i do to make it not exit the program and back to main menu.

#include <iostream>
#include <iomanip>
#include<string.h>
using namespace std;

struct food{
int order_id;
string food_code,flavor,customer_id;
string address,name;
int weight,unit_price,qty,contact_number;
struct food *next;
};
  

class Foodsystem{
food *head,*temp,*temp2,*end;
static int id;
  
public:
Foodsystem(){ head=NULL;end=NULL;}

void Place_Order();
void View_food_details();
void Modify_food_details();
void Delete_food_details();
};
  
int Foodsystem::id=1000;
  
void Foodsystem::Place_Order(){
temp=new food;
cout<<endl;
cout<<"enter your name ::";
cin>>temp->name;
cout<<"enter contact number::";
cin>>temp->contact_number;
cin.ignore();
cout<<"enter address::";
getline(cin,temp->address);
cout<<"customer_id::";
getline(cin,temp->customer_id);
cout<<"enter food code u want to order::";
getline(cin,temp->food_code);
cout<<"enter flavor you want";
getline(cin,temp->flavor);
cout<<"enter the food weight in kg";
cin>>temp->weight;
cout<<"enter unit price";
cin>>temp->unit_price;
cout<<"quanity of item you want::";
cin>>temp->qty;
temp->order_id=id++;
if(head==NULL)
{head=temp;end=head;}
else
{end->next=temp;end=temp;}

}
  
void Foodsystem::View_food_details()
{
temp=head;
while(temp){
cout<<"your order id::";
cout<<temp->order_id<<endl;
cout<<"customer_id::";
cout<<temp->customer_id<<endl;
cout<<"food code::";
cout<<temp->food_code<<endl;
cout<<"flavor";
cout<<temp->flavor<<endl;
cout<<"the food weight in kg";
cout<<temp->weight<<endl;
cout<<"unit price";
cout<<temp->unit_price<<endl;
cout<<"quanity of item ::";
cout<<temp->qty<<endl;
cout<<"bill amount";
cout<<temp->unit_price *temp->qty<<endl;
temp=temp->next;
}
}
  


int main() {
// Write C++ code here
Foodsystem fs;
int choice;
while(1)
{
cout<<endl<<"1. Place Order" <<endl<<"2. View the food details"<<endl<<"3. Modify food details"<<endl<<"4. Delete food details"<<endl<<"5. exit"<<endl;
cout<<"enter your choice";
cin>>choice;
switch(choice){
case 1 : fs.Place_Order(); break;
case 2 : fs.View_food_details();break;
// case 3 : fs.Modify_food_details();break;
// case 4 : fs.Delete_food_details();break;
case 5 : exit(0);
default : cout<<"wrong choice";
  
}
}

return 0;
}

Homework Answers

Answer #1

I added a nested while loop in the main function where befor the switch case statements so that now everytime after entering the number the loop gets executed properly. Now the control doesnt exits the program after showing details but it stays to take further inputs from the user and exits only when 5 is pressed. And you have to enter the right data types or else it will fall in an infinite loop.

#include <iostream>
#include <iomanip>
#include<string.h>
using namespace std;

struct food{
int order_id;
string food_code,flavor,customer_id;
string address,name;
int weight,unit_price,qty,contact_number;
struct food *next;
};
  

class Foodsystem{
food *head,*temp,*temp2,*end;
static int id;
  
public:
Foodsystem(){ head=NULL;end=NULL;}

void Place_Order();
void View_food_details();
void Modify_food_details();
void Delete_food_details();
};
  
int Foodsystem::id=1000;
  
void Foodsystem::Place_Order(){
temp=new food();
cout<<endl;
cout<<"enter your name ::";
cin>>temp->name;
cout<<"enter contact number::";
cin>>temp->contact_number;
cin.ignore();
cout<<"enter address::";
getline(cin,temp->address);
cout<<"customer_id::";
getline(cin,temp->customer_id);
cout<<"enter food code u want to order::";
getline(cin,temp->food_code);
cout<<"enter flavor you want";
getline(cin,temp->flavor);
cout<<"enter the food weight in kg";
cin>>temp->weight;
cout<<"enter unit price";
cin>>temp->unit_price;
cout<<"quanity of item you want::";
cin>>temp->qty;
temp->order_id=id++;
if(head==NULL)
{head=temp;end=head;}
else
{end->next=temp;end=temp;}

}
  
void Foodsystem::View_food_details()
{
temp=head;
while(temp){
cout<<"your order id::";
cout<<temp->order_id<<endl;
cout<<"customer_id::";
cout<<temp->customer_id<<endl;
cout<<"food code::";
cout<<temp->food_code<<endl;
cout<<"flavor";
cout<<temp->flavor<<endl;
cout<<"the food weight in kg";
cout<<temp->weight<<endl;
cout<<"unit price";
cout<<temp->unit_price<<endl;
cout<<"quanity of item ::";
cout<<temp->qty<<endl;
cout<<"bill amount";
cout<<temp->unit_price *temp->qty<<endl;
temp=temp->next;
}
}
  


int main() {
// Write C++ code here
Foodsystem fs;
int choice;
while(1)
{
cout<<endl<<"1. Place Order" <<endl<<"2. View the food details"<<endl<<"3. Modify food details"<<endl<<"4. Delete food details"<<endl<<"5. exit"<<endl;
cout<<"enter your choice";
cin>>choice;
while(1){
switch(choice){
case 1 : fs.Place_Order(); break;
case 2 : fs.View_food_details();break;
// case 3 : fs.Modify_food_details();break;
// case 4 : fs.Delete_food_details();break;
case 5 : exit(0);
default : cout<<"wrong choice";
  
}
}
}

return 0;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
How do i stop the loop if no next value in the list and prevent it...
How do i stop the loop if no next value in the list and prevent it from exit the program instead back to main menu. Let say i add food detail then i display it. It will display the detail but it will exit after it. struct food{ int order_id; string food_code,flavor,customer_id; string address,name; int weight,unit_price,qty,contact_number; struct food *next; }; void Foodsystem::Place_Order(){ temp=new food; cout<<endl; cin.ignore(); cout<<"Enter your Name"; getline(cin,temp->name); cout<<"enter contact number::"; cin>>temp->contact_number; cin.ignore(); cout<<"enter address::"; getline(cin,temp->address); cout<<"customer_id::"; getline(cin,temp->customer_id);...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
Write a program that reads a string and outputs the number of lowercase vowels in the...
Write a program that reads a string and outputs the number of lowercase vowels in the string. Your program must contain a function with a parameter of a char variable that returns an int. The function will return a 1 if the char being passed in is a lowercase vowel, and a 0 for any other character. The output for your main program should be: There are XXXX lowercase vowels in string yyyyyyyyyyyyyyyyyyyyyy Where XXXX is the count of lowercase...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
USING C++ Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Create your objects in the stack...
USING C++ Topics: Friend functions Copy constructor ----------------------------------------------------------------------------------------------------------------------------------------- Lab 3.1 Create your objects in the stack (not on the heap). Add a friend function, kilotopound, which will convert kilograms to pounds. Change your weight mutator to ask whether weight is input in kilograms or pounds. If it is kilograms, call the friend function kilotopound to convert it to pounds and return pounds. There are 2.2 pounds in one kilogram. Create an object on the stack with the following information:     ...
C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using...
C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using linked lists. Currently I am able to input processes and store / display them properly. The issue begins somewhere after I have displayed the processlist (I get a segmentation fault (core dumped) or the code doesnt seem to run). I have marked the location of where I think the issue begins with a comment. Please fix the code so that it is working properly....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT