system crashed

hey guys im sorry i really need your help for my project,the system crased big time. i dont know where is the problem


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

void database();
void menu();
void F_order();
void pay();


struct node
{
int kod;
int price;
struct node *next;
};

struct node *head= NULL;
struct node *order = NULL;

void database()
{


struct node * temp= NULL;


temp = new node;
temp->kod=001;
temp->price=14;
temp->next=NULL;

head=temp;

temp =new node;
temp->kod=002;
temp->price=10;
temp->next=NULL;

temp->next=head;
head=temp;

temp =new node;
temp->kod=003;
temp->price=9;
temp->next=NULL;

temp->next=head;
head=temp;

temp =new node;
temp->kod=004;
temp->price=8;
temp->next=NULL;

temp->next=head;
head=temp;

temp =new node;
temp->kod=005;
temp->price=8;
temp->next=NULL;

temp->next=head;
head=temp;

temp =new node;
temp->kod=100;
temp->price=6;
temp->next=NULL;

temp->next=head;
head=temp;

temp =new node;
temp->kod=200;
temp->price=7;
temp->next=NULL;

temp->next=head;
head=temp;

temp = new node;
temp->kod=300;
temp->price=5;
temp->next=NULL;

temp->next=head;
head=temp;

temp = new node;
temp->kod=400;
temp->price=3;
temp->next=NULL;

temp->next=head;
head=temp;

temp = new node;
temp->kod=500;
temp->price=3;
temp->next=NULL;

temp->next=head;
head=temp;

temp = new node;
temp->kod=010;
temp->price=5;
temp->next=NULL;

temp->next=head;
head=temp;

temp = new node;
temp->kod=020;
temp->price=3;
temp->next=NULL;

temp->next=head;
head=temp;

temp =new node;
temp->kod=030;
temp->price=5;
temp->next=NULL;

temp->next=head;
head=temp;

temp = new node;
temp->kod=040;
temp->price=6;
temp->next=NULL;

temp->next=head;
head=temp;

temp=new node;
temp->kod=050;
temp->price=7;
temp->next=NULL;

temp->next=head;
head=temp;

}


//global variable
int table_no,quantity,total;

void main()
{
database();
int choose;

cout<<"Enter your table number : ";
cin>>table_no;

do
{
cout<<"What is Your Order\n";
cout<<"Please choose\n";
cout<<"1=TO display menu\n";
cout<<"2=TO place your order\n";
cout<<"3=TO see total payment amount\n";


cin>>choose;



switch (choose)
{
case 1:
menu();
break;
case 2:
F_order();
break;
case 3:
pay();
break;
case 4:
exit(0);
break;
}

}
while (1);


}

void menu()
{

int move;

cout<<"What did your want to display?\n";
cout<<"1=Main dish\n";
cout<<"2=Desert\n";
cout<<"3=Drinks\n";
cout<<"4=Back to main menu\n";
cin>>move;

do
{

switch (move)
{
case 1:

cout<<"**************************************************\n";
cout<<"* KOD * FOOD NAME * PRICE *\n";
cout<<"**************************************************\n";
cout<<"* 001 * MIXED BENTO * RM 14 *\n";
cout<<"* 002 * CHEESE RING DAKGALBI * RM 10 *\n";
cout<<"* 003 * NYAM NYEON TONGDAK * RM 9 *\n";
cout<<"* 004 * KHAO PHAD KUNG * RM 8 *\n";
cout<<"* 005 * PATD KRAM PAW * RM 8 *\n";
cout<<"**************************************************\n";

break;

case 2:

cout<<"**************************************************\n";
cout<<"* KOD * DESSERT NAME * PRICE *\n";
cout<<"**************************************************\n";
cout<<"* 010 * FROZEN CARAMEL APPLE * RM 5 *\n";
cout<<"* 020 * BANANA SPLIT * RM 3 *\n";
cout<<"* 030 * CHERRY CREAM CHEESE * RM 5 *\n";
cout<<"* 040 * CHOCOLATE DIP * RM 6 *\n";
cout<<"* 050 * BROWNIES TRIFFLE * RM 7 *\n";
cout<<"**************************************************\n";
break;

case 3:

cout<<"**************************************************\n";
cout<<"* KOD * BEVERAGE NAME * PRICE *\n";
cout<<"**************************************************\n";
cout<<"* 100 * CHOCOLATE CREAM CHIPS * RM 6 *\n";
cout<<"* 200 * EXPRESSO MOCHA * RM 7 *\n";
cout<<"* 300 * BIG MILO TABUR * RM 5 *\n";
cout<<"* 400 * ICE LEMON TEA * RM 3 *\n";
cout<<"* 500 * CARROT MILK * RM 3 *\n";
cout<<"**************************************************\n";
break;

case 4:
return;
break;

default:
cout<<"WRONG INPUT!!!\n";
break;

}

}

while (0);

}

void F_order()
{
struct node *temp1=NULL;
char choice;
char data;
int quantity;

do
{

cout<<"Enter your order\n";
cin >> data;

struct node *temp = head;

while (temp!= NULL)
{

if (temp->kod == data)

{
temp1=new node;
temp1->kod=temp->kod;
temp1->price=temp->price;
temp1->next=NULL;

if(order == NULL)
{
order=new node;

order=temp1;

temp1->next=NULL;
break;
}
else
{
temp1->next=order;

order=temp1;
}


}
else
{
temp=temp->next;

}
}
cout<<"Item price RM"<<temp1->price<<endl;
cin>>quantity;
cout<<"would you like to add more y/n?"<<endl;
cin>>choice;

}
while (choice=='y');


}

void pay()
{
int total=0;
node * temp =order;
while (order!=NULL)
{
total = total + order->price;
temp =order;
temp->next = NULL;
order=order-> next;
delete temp;
}

}
Last edited on
Hello helena97,

Welcome to the forum.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

To start with it is "<iostream>" for C++ there is no such file as "<iostream.h>".

Line 9. Is OK, but I would try to avoid using a non "const" global variable.

Line 12. Is generally written as int main() with a return 0; just before the closing brace of main.

Starting at line 26 the do/while loop will work once. The condition while (0) is the same as saying while (false) which means it will not loop back to the top. "case 4:" no one but you know it is available. You should not use "exit(1)" the 1 or higher number generally means that there is a problem. I find this bit of code to work well with a do/while loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
	int choice{};
	bool cont{ true };

	do
	{
		switch (choice)
		{
			case 1:
				break;
			case 2:
				break;
			case 4:  // the exit number
				cont = false;
				break;
		}
	} while (cont);

	return 0;
}


In the menu function, which I have no spent much time on the while condition while (4) again is the same as while (true) which is an endless loop. And you have the same problem with "case 4:" as I mentioned earlier.

For the order function it starts out OK, but falls apart at about line 139 after you print out the headings. You should print what you have entered earlier not ask for new input that you already have. At the beginning of the function you prompt for the "food_code", but not for the "quantity". this leaves the user wondering what to input. At about line 139 the "std::cin" statements would be replaced with "std::cout" statements to print what you have entered.

You have left out the line using namespace std; which is good. You should avoid using this line, but you will need to qualify what is in the "std" name space like: "std::cin", "std::cout", "std::endl" and others. it is better to start learning what is in the "std" name space now rather than later.

Also understand that generally C++ header files have no extension and a header file with a ".h" extension is an old C header and that some of these header files are not available with newer compilers. The "<stdlib.h>" is included through the "<iostream>" header file, so you do not always need to include this file.

Hope that helps,

Andy
dear andy,
i have modify my code ,but unfortunately mys system crased i dont know how to detect the problem ,what i know is it start crashed in order part
@Handy Andy,
#include<iostream.h> implies that she uses a very old compiler (TurboC++??) which even doesn't know about namespaces, they were added later to the standard.

@helena97,
could you post the updated code please.
this one is latest unfortunately its crashed ,i dont know why,but theres no error in it,pleaseee i really need your help.it start occur in void order




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

void database();
void menu();
void F_order();
void pay();


struct node
{
int kod;
int price;
struct node *next;
};

struct node *head= NULL;
struct node *order = NULL;

void database()
{


struct node * temp= NULL;


temp = new node;
temp->kod=001;
temp->price=14;
temp->next=NULL;

head=temp;

temp =new node;
temp->kod=002;
temp->price=10;
temp->next=NULL;

temp->next=head;
head=temp;

temp =new node;
temp->kod=003;
temp->price=9;
temp->next=NULL;

temp->next=head;
head=temp;

temp =new node;
temp->kod=004;
temp->price=8;
temp->next=NULL;

temp->next=head;
head=temp;

temp =new node;
temp->kod=005;
temp->price=8;
temp->next=NULL;

temp->next=head;
head=temp;

temp =new node;
temp->kod=100;
temp->price=6;
temp->next=NULL;

temp->next=head;
head=temp;

temp =new node;
temp->kod=200;
temp->price=7;
temp->next=NULL;

temp->next=head;
head=temp;

temp = new node;
temp->kod=300;
temp->price=5;
temp->next=NULL;

temp->next=head;
head=temp;

temp = new node;
temp->kod=400;
temp->price=3;
temp->next=NULL;

temp->next=head;
head=temp;

temp = new node;
temp->kod=500;
temp->price=3;
temp->next=NULL;

temp->next=head;
head=temp;

temp = new node;
temp->kod=010;
temp->price=5;
temp->next=NULL;

temp->next=head;
head=temp;

temp = new node;
temp->kod=020;
temp->price=3;
temp->next=NULL;

temp->next=head;
head=temp;

temp =new node;
temp->kod=030;
temp->price=5;
temp->next=NULL;

temp->next=head;
head=temp;

temp = new node;
temp->kod=040;
temp->price=6;
temp->next=NULL;

temp->next=head;
head=temp;

temp=new node;
temp->kod=050;
temp->price=7;
temp->next=NULL;

temp->next=head;
head=temp;

}


//global variable
int table_no,quantity,total;

void main()
{
database();
int choose;

cout<<"Enter your table number : ";
cin>>table_no;

do
{
cout<<"What is Your Order\n";
cout<<"Please choose\n";
cout<<"1=TO display menu\n";
cout<<"2=TO place your order\n";
cout<<"3=TO see total payment amount\n";


cin>>choose;



switch (choose)
{
case 1:
menu();
break;
case 2:
F_order();
break;
case 3:
pay();
break;
case 4:
exit(0);
break;
}

}
while (1);


}

void menu()
{

int move;

cout<<"What did your want to display?\n";
cout<<"1=Main dish\n";
cout<<"2=Desert\n";
cout<<"3=Drinks\n";
cout<<"4=Back to main menu\n";
cin>>move;

do
{

switch (move)
{
case 1:

cout<<"**************************************************\n";
cout<<"* KOD * FOOD NAME * PRICE *\n";
cout<<"**************************************************\n";
cout<<"* 001 * MIXED BENTO * RM 14 *\n";
cout<<"* 002 * CHEESE RING DAKGALBI * RM 10 *\n";
cout<<"* 003 * NYAM NYEON TONGDAK * RM 9 *\n";
cout<<"* 004 * KHAO PHAD KUNG * RM 8 *\n";
cout<<"* 005 * PATD KRAM PAW * RM 8 *\n";
cout<<"**************************************************\n";

break;

case 2:

cout<<"**************************************************\n";
cout<<"* KOD * DESSERT NAME * PRICE *\n";
cout<<"**************************************************\n";
cout<<"* 010 * FROZEN CARAMEL APPLE * RM 5 *\n";
cout<<"* 020 * BANANA SPLIT * RM 3 *\n";
cout<<"* 030 * CHERRY CREAM CHEESE * RM 5 *\n";
cout<<"* 040 * CHOCOLATE DIP * RM 6 *\n";
cout<<"* 050 * BROWNIES TRIFFLE * RM 7 *\n";
cout<<"**************************************************\n";
break;

case 3:

cout<<"**************************************************\n";
cout<<"* KOD * BEVERAGE NAME * PRICE *\n";
cout<<"**************************************************\n";
cout<<"* 100 * CHOCOLATE CREAM CHIPS * RM 6 *\n";
cout<<"* 200 * EXPRESSO MOCHA * RM 7 *\n";
cout<<"* 300 * BIG MILO TABUR * RM 5 *\n";
cout<<"* 400 * ICE LEMON TEA * RM 3 *\n";
cout<<"* 500 * CARROT MILK * RM 3 *\n";
cout<<"**************************************************\n";
break;

case 4:
return;
break;

default:
cout<<"WRONG INPUT!!!\n";
break;

}

}

while (0);

}

void F_order()
{
struct node *temp1=NULL;
char choice;
char data;
int quantity;

do
{

cout<<"Enter your order\n";
cin >> data;

struct node *temp = head;

while (temp!= NULL)
{

if (temp->kod == data)

{
temp1=new node;
temp1->kod=temp->kod;
temp1->price=temp->price;
temp1->next=NULL;

if(order == NULL)
{
order=new node;

order=temp1;

temp1->next=NULL;

}
else
{
temp1->next=order;

order=temp1;
}


}
else
{
temp=temp->next;

}
}
cout<<"Item price RM"<<temp1->price<<endl;
cin>>quantity;
cout<<"would you like to add more y/n?"<<endl;
cin>>choice;

}
while (choice=='y');


}

void pay()
{
int total=0;
node * temp =order;
while (order!=NULL)
{
total = total + order->price;
temp =order;
temp->next = NULL;
order=order-> next;
delete temp;
}

}
You're not helped by using a compiler from twenty years ago that accepts something similar to, but not the same as, C++.

I ran your code under a debugger. You need to learn to use one. It takes five minutes to learn to use one.

Here is the output:

(gdb) run
Starting program: C:\MinGW\msys\1.0\home\user/./a.exe
[New Thread 14728.0x2264]
Enter your table number : 2
What is Your Order
Please choose
1=TO display menu
2=TO place your order
3=TO see total payment amount
2
Enter your order
1

Program received signal SIGSEGV, Segmentation fault.
0x00401cf3 in _fu45___ZSt3cin () at 023.cpp:322
322     cout<<"Item price RM"<<temp1->price<<endl;
(gdb) print temp1
$1 = (node *) 0x0
(gdb)


Line 322 is this line:
cout<<"Item price RM"<<temp1->price<<endl;

temp1 is a null pointer. It's pointing at nothing.
@repeater i dont understand
A pointer has to point at something.

See what I entered to make your program crash? I entered 2 , 2, 1 , and then it crashed. It crashed because the pointer named temp1 wasn't pointing at anything.
ok guys one more,i can make it run but it doesnt display for pay

void pay()
{
int total=0;
node * temp =order;
while (order!=NULL)
{
total = total + order->price;
temp =order;
temp->next = NULL;
order=order-> next;
delete temp;
}

}
If you want it to display something, you need to cout something.
Topic archived. No new replies allowed.