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);
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<order_id< cout<<"customer_id::";
cout<customer_id< cout<<"food code::";
cout<food_code< cout<<"flavor";
cout<flavor< cout<<"the food weight in kg";
cout<weight< cout<<"unit price";
cout<unit_price< cout<<"quanity of item ::";
cout<qty< cout<<"bill amount";
cout<unit_price *temp->qty< temp=temp->next;
}
}
Please use "do while" statement in your code like below:
do { statement(s); } while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false.
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
in condition, you can put if list is empty or not.
Since provided code is not complete, so I cannot help more than this. If you can provide the complete, I can help more on this.
Get Answers For Free
Most questions answered within 1 hours.