Basic Restaurant Bill with OOP

Hi guys, am trying to learn c++... but this code is giving me a strange error..
([Error] name lookup of 'i' changed for ISO 'for' scoping [-fpermissive] )
can some one help me to correct it..
Thanks in advance.. God Bless u all.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////////
class restaurant
{
	private:
		string dish [10];
		int price [10];
		int num, cost;
	public:
		void inputorder()
		{
			cout<<"Enter number of dishes (<=10): ";
			cin>>num;
			for (int i=0; i<=num; i++)
				cout<<"Enter dish name and price: ";
				cin>>dish[i]>>price[i];
			cout<<endl;
		}
		void calculatebill()
		{
			for (int i=0; i<=num; i++)
				cost += price[i];
		}
		void showbill()
		{
			system("cls");
			cout<<setw(15)<<"Dish Name"<<setw(20)<<"Price"<<endl<<endl;
			for (int i=0; i<=num; i++)
				cout<<setw(15)<<dish[i]<<setw(20)<<"Rs. "<<price[i]<<endl;
			for(int l=1; l<=45; l++)
				cout<<"-";
			cout<<endl;
			cout<<setw(15)<<"Total Price"<<setw(20)<<cost<<endl<<endl;
		}
};
//////////////////////////////////////////////////////////////////////////
int main()
{
	restaurant order, bill, show;
	order.inputorder();
	bill.calculatebill();
	show.showbill();
	system("pause");
	return 0;
}


i got it compiled by declaring i outside loops.. but its not working correctly.. help plz..
Last edited on
Look closely at this snippet:
1
2
3
			for (int i=0; i<=num; i++)
				cout<<"Enter dish name and price: ";
				cin>>dish[i]>>price[i];


Since the second line is not part of the loop i is undefined.

In pre-standard C++ when initializing a variable in the initialization section of a for(;;) loop the variable was actually initialized as being before the loop. In standard C++ this variable is contained within the loop and goes out of scope at the end of the loop.
Thank u so much.. now its taking inputs.. but calculatebill and showbill function are not working.. After taking inputs, an error occurred, "code.exe has stopped working".. Now Whats wrong?
Please post your current code.
Here's the current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
////////////////////////////////////////////////////////////////////
class restaurant
{
	private:
		string dish [10];
		int price [10];
		int num, cost;
	public:
		void inputorder()
		{
			cout<<"Enter number of dishes (<=10): ";
			cin>>num;
			for (int i=0; i<=num-1; i++)
				{
					system("cls");
					cout<<"Enter dish name and price: ";
					cin>>dish[i]>>price[i];
				}
			cout<<endl;
		}
		void calculatebill()
		{
			for (int i=0; i<=num-1; i++)
				cost += price[i];
		}
		void showbill()
		{
			system("cls");
			cout<<setw(15)<<"Dish Name"<<setw(20)<<"Price"<<endl<<endl;
			for (int i=0; i<=num-1; i++)
				cout<<setw(15)<<dish[i]<<setw(20)<<"Rs. "<<price[i]<<endl;
			for(int l=1; l<=45; l++)
				cout<<"-";
			cout<<endl;
			cout<<setw(15)<<"Total Price"<<setw(20)<<cost<<endl<<endl;
		}
};
//////////////////////////////////////////////////////////////////////////
int main()
{
	restaurant order;
	order.inputorder();
	order.calculatebill();
	order.showbill();
	system("pause");
	return 0;
}


Its giving results.. but with incorrect total price..
Last edited on
I think you need to review how classes work. You have created three instances of your class but you only initialize the values in one of these instances (order). All the values in bill and show are uninitialized. You probably only want one instance for this assignment.

1
2
3
4
5
6
7
8
9
int main()
{
	restaurant order;
	order.inputorder();
	order.calculatebill();
	order.showbill();
	system("pause");
	return 0;
}
I initialized cost=0 and its working fine.
Actually today was my first day to classes and objects.. so silly mistakes happened. but thanks alot. You helped me really patiently.
God bless u. Take care.
Last edited on
Topic archived. No new replies allowed.