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;
}
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;
}
Get Answers For Free
Most questions answered within 1 hours.