i cant make it return to main choose after making ordering

Pages: 123
@ keskiverto,

Load it up and try it. I found it a quick example of how "using namespace std;" can cause a problem.

It does not look like much, but it works.

Andy
closed account (E0p9LyTq)
@ keskiverto,

http://www.cplusplus.com/reference/algorithm/count/

I didn't know there was a standard library count algorithm. I avoid such ambiguities by not having a using namespace std; statement.

@Handy Andy,

MSVC++2017 chokes on your example, declaring count to be an ambiguous symbol and halting compilation. The IDE even "red-lines" all instances of count.

BTW, I am saving your example for future reference when people say using namespace std; is not a bad habit.
Last edited on
Even better use a debugger, hopefully there is a GUI one in the IDE, other wise use a command line version. Set up a watch list of variables, step through the code 1 line at a time, see how the values change, deduce what went wrong. This has the advantages of not having to: Add output statements everywhere ; going back and deleting the output statements.


Yes, but it has the disadvantage of not being 100% as portable as the language you learned to do it in.

Know anyone using Think C on a Mac 2 anymore? Me either, but that's what I first learned on :-)

That said, if 60% of your students are using Visual C, another 30% are using X-code, and the remaining 10% are using GNU, some of them on cygwin, some on Linux, and one on MacOS who just didn't prefer xcode... what are your debugger instructions going to look like? :-)
Last edited on
@ FurryGuy,

MSVC++2017 chokes on your example, declaring count to be an ambiguous

And that is one of the points of the whole example. MY VS 2015 says much the same. Someone who does not know or yet to understand the error messages and even how the compiler works could spend hours trying to figure this out only to be told to comment out the "using" line and see what happens. Or to finally be asked what the intended use is the "count" variable as defined in the program or the "std::count(...)" function that the compiler is trying to use because of the "using" line. Since "std::count++;" as the compiles sees it is missing the () and parameters.

I do not remember how I came up with or where I stumbled across it, but I thought it is a good teaching tool.

I hope that makes sense,

Andy
@lastchance,

Thank you that is nice to know. At the moment I do not have the ability to have several compilers or IDEs to work with yet. Someday.

I do believe you are correct that as a global variable it is initialized to zero, but do not always count on it. This comes from what I have read in other threads.

The point this little program is trying to make is how the line"using namespace std;" causes the compiler to put "std::" in front of everything and if it finds something in the standard name space it tries to use that first. I have also seen this problem with functions and most recently with a program that defined a class as "Polygon" which turned out to be, I believe, as a windows name and/or function.

Andy
I see.

GCC does not pull in algorithm, when you include iostream. Hence I could not repeat the "issue". IIRC, even in GCC the interdependencies of standard library headers have been worse before.
Sorry Andy, I deleted my post when I realised what MSVC was probably doing. As @Keskiverto notes, unless <algorithm> is included (directly or indirectly) then the compiler isn't going to find count.

I appreciate your example, but if a compiler coughs back at me that there is ambiguity then I deal with it then. I'm sure that I've inadvertently tried to name variables after most of the maths functions at one time or another.

One day the C++ standard may decide to make 'sum' a reserved word - that doesn't stop me using it. Let the future take care of itself.

If you add
#include <algorithm>
to your example then you will be able to make your point. (Even if it will never quite persuade me!)

Thanks for your hard work trying to get some of the beginners to fix their code - even if the OP here doesn't fully appreciate it.
Last edited on
@lastchance,

Actually for me "std::count(...)" came from the "xutility" header file which came eventually from the "iostream" header file. Which is why it is so sneaky.

I will look into the "<algorithm>" header file and see what it says.

Andy
@handy andy

it display 1??
sorry guys im giving on this course maybe im not suitable in this .so this is my last program

basically my program is about ordering system.
i used queue,struct to made this program

my problem in this program??
=is it doesnt display the menu that have ordered.
got rejected twice ^^

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <iostream.h>
#include <stdlib.h>

void menu();
void F_order();
void pay();
void display();



struct queue
{   
	char kod[6];
	struct queue *next;
};

struct queue *front = NULL;
struct queue *rear = NULL;


//global variable
int table_no,kod;

void main()
{
	int choose;

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

	do
	{
	cout<<"Choose a number\n";
	cout<<"1=TO display menu\n";
	cout<<"2=TO place customer order\n";
	cout<<"3=TO display the customer order\n";
	cout<<"4=TO diplay payment amount for customer order\n";
	cout<<"5=Exit\n\n";
	

	cin>>choose;

	cout<<"\n"<<endl;



	switch (choose)
	{
	case 1:
		menu();
	break;

	case 2:
		F_order();
	break;

	case 3:
		display();
	break;

	case 4:
		pay();
	break;

	case 5:
		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=Beverage\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<<"**************************************************\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<<"**************************************************\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<<"**************************************************\n";

	break;

	case 4:
		return;
	break;

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

	}

	}

	while (0);

}


void F_order()
{

	struct queue *temp=NULL;
	char choice;

	do
	{
		temp= new queue;
		cout<<"Enter food code\n";
		cin>>temp->kod;
		temp->next=NULL;
		if(front==NULL)
		{
			front=rear=temp;
		}
		else
		{
		rear->next=temp;
		rear=temp;
		}



		cout<<"would you like to add more y/n?"<<endl;
		cin>>choice;

	}while (choice =='y');

	

}

void pay()
{
	int total=0,price,quantity,a;
	char ans;

	do
	{

	cout<<"Enter item amount\n";
	cin>>a;

	for (int i=1;i<=a;i++)
	{
		cout<<"Enter food price\n";
		cin>>price;
		cout<<"Enter food quantity\n";
		cin>>quantity;
		
		cout<<"Item amount="<<i<<endl;
		total=total+(price * quantity);
		cout<<"Total price = "<<total<<endl;

	}

	cout<<"Want to repeat?\n";
	cout<<"Yes=y\n";
	cout<<"No=n\n";
	cin>>ans;
	}
	while(ans=='y');
	
}

void display()
{
	if (front == NULL)
		cout<<"NO ORDER\n";
	else
	{
		struct queue * temp = front;
		while (temp->next !=NULL)
		{
			cout<<temp->kod<<endl;
			temp = temp ->next;
		}

		cout<<temp->kod<<endl;
	}
}

so umm ,hope you guys like it .
my problem in this program??
=is it doesnt display the menu that have ordered.

Do you mean the output of display()?

How does it differ from expected?
Hello helena97,

I have always believed the program has good potential, but you are going about things in the wrong way.

I will check out your last code and see what I can come up with.

Andy
yes @keskiverto

@handy_andy thanks still willing to reply me ,im greatful you were here.
i dont know andy.im in the middle of despair and desperate right now .
How does the output of display() differ from expected?


yes


Could you be more specific?
Enter your table number : 12
Choose a number
1=TO display menu
2=TO place customer order
3=TO display the customer order
4=TO diplay payment amount for customer order
5=Exit

2


Enter food code
001
would you like to add more y/n?
y
Enter food code
002
would you like to add more y/n?
n
Choose a number
1=TO display menu
2=TO place customer order
3=TO display the customer order
4=TO diplay payment amount for customer order
5=Exit

3


001                                 
002
Choose a number
1=TO display menu
2=TO place customer order
3=TO display the customer order
4=TO diplay payment amount for customer order
5=Exit

4


Enter item amount
2
Enter food price
12
Enter food quantity
12
Item amount=1
Total price = 144
Enter food price
1
Enter food quantity
1
Item amount=2
Total price = 145
Want to repeat?
Yes=y
No=n



this is the ouput it should display price and the name of the item but it makes user have to input by manual ,any suggestion ???
Hello helena97,

I have not worked on the "pay" function yet. It was more necessary to get the order part right first.

These are the changes I made working with what you had to start with:

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>  // <--- There is no <iostream.h>.
#include <iomanip>
#include <string>
#include <cctype>
#include <chrono>  // <--- For code used in case 3 of the "menu" function.
#include <thread>  // <--- For code used in case 3 of the "menu" function.
#include <conio.h>  // <--- Not necessary. Something I used in testing to speed things up. Used in "F_order".

//#include <stdlib.h>

void menu();
void F_order(const double price[]);  // <--- Added array to function.
void pay();
void display();

constexpr std::size_t MAXSIZE{ 9 };  // <--- Because there 9 items on the menus.

struct queue
{
	std::string kod;
	std::size_t s_qty{};
	double s_price{};
	struct queue *next;
};

struct queue *front = NULL;
struct queue *rear = NULL;


//global variable
//  <--- Should not use global variables like these.
//int table_no, kod;  // <--- "table_no" used only once and "kod" never used as an int.

void main()
{
	constexpr double price[9]{ 1.0, 2.0 , 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };  // <--- Change prices as needed.

	int table_no;  // <--- Better placed here. Or maybe in the struct?
	int choose;
	bool cont{ true };  // <--- Added. Used for do/while loop.

	// <--- No use as of now. Just bypassed  for testing.
	//std::cout << "Enter your table number : ";
	//std::cin >> table_no;

	do
	{
		std::cout << "\nChoose a number\n";
		std::cout << "1 = TO display menu\n";  // <--- Changed '=' to ' = '. Looks better.
		std::cout << "2 = TO place customer order\n";
		std::cout << "3 = TO display the customer order\n";
		std::cout << "4 = TO diplay payment amount for customer order\n";
		std::cout << "5 = Exit\n";
		std::cout << "Enter choice: ";
		std::cin >> choose;

		std::cout << std::endl;

		switch (choose)
		{
			case 1:
				menu();
				break;
			case 2:
				F_order(price);
				break;
			case 3:
				display();
				break;
			case 4:
				pay();
				break;
			case 5:
				cont=false;
				break;
		}

	} while (cont);  // <--- Changed.
}


And in the "F_order" function I did this:
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
void F_order(const double price[])
{

	struct queue *temp = NULL;
	char choice;
	std::size_t count{};

	do
	{
		int code{};
		temp = new queue;

		// would be helpful to display available items.

		if (count < MAXSIZE)
		{
			std::cout << "Enter food code\n";
			std::cin >> temp->kod;

			std::cout << "Enter Quantity: ";
			std::cin >> temp->s_qty;

			code = std::stoi(temp->kod);  // <--- Converts string to an int.

			switch (code)
			{
			case 1:
				temp->s_price = price[0];
				break;
			case 2:
				temp->s_price = price[1];
				break;
			case 3:
				temp->s_price = price[2];
				break;
			case 10:
				temp->s_price = price[3];
				break;
			case 20:
				temp->s_price = price[4];
				break;
			case 30:
				temp->s_price = price[5];
				break;
			case 100:
				temp->s_price = price[6];
				break;
			case 200:
				temp->s_price = price[7];
				break;
			case 300:
				temp->s_price = price[8];
				break;
			default:
				break;
			}
		}

		count++;

		temp->next = NULL;

		if (front == NULL)
		{
			front = rear = temp;
		}
		else
		{
			rear->next = temp;
			rear = temp;
		}

		std::cout << "would you like to add more y/n?" << std::endl;
		//std::cin >> choice;
		choice = _getch();  // <--- Used this way for testing.

	} while (std::tolower(choice) != 'n');
	std::cout << std::endl;
}


Notice I added quantity and price to the struct. Since this struct becomes a node in a linked list each node having all the information you need makes it easier to display and use in the "pay" function.

The program can do multiple orders, but the linked list would become a problem on the second or third order because it would only add to the linked list, so the second order would be correct, but say the display would start at order one and also display order two and so on.

You would either have to delete the current linked list or change the address of "front" for each new order.

You did a nice job with the linked list code. That impressed me.

Thinking about this storing each struct in a vector of structs would be easier. After you are done with an order "vectorName.clear();" will erase everything allowing you to start with a new order.

Almost for got I did the "display" function this way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void display()
{
	if (front == NULL)
		std::cout << "NO ORDER\n";
	else
	{
		struct queue * temp = front;

		std::cout << std::fixed << std::showpoint << std::setprecision(2);

		std::cout << " Item        Qty      Price" << std::endl;

		while (temp->next != NULL)
		{
			std::cout << std::setw(4) << temp->kod << "        " << std::setw(3)
				<< temp->s_qty << std::setw(5) << "        " << temp->s_price << std::endl;
			temp = temp->next;
		}

		std::cout << std::setw(4) << temp->kod << "        " << std::setw(3)
			<< temp->s_qty << std::setw(5) << "        " << temp->s_price << std::endl;
		//std::cout << temp->kod << std::endl;
	}
}


The while condition is causing the extra cout after the while loop. I have not spent much time on this yet, but I know it is fixable so that each node can be printed inside the while loop.

Hope that helps,

Andy
andy....you dont have to do that
and why your code are more prettier than mine

omg marry me andy
Hello helena97,

Worked on the "pay" function and came up with this:

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
void pay()
{
	double total{};// , price{}, quantity{}, a{};
	char ans{};
	bool cont{ true };
	queue* temp = NULL;

	std::cout << std::fixed << std::showpoint << std::setprecision(2);

	do
	{
		temp = front;  // <--- Always starts at the beginning.

		while (cont)
		{
			total += temp->s_price * temp->s_qty;
			//std::cout << "Total amount is: " << total << std::endl;
			temp = temp->next;

			if (temp == NULL)
				break;
		}

		std::cout << "\nTotal price = " << total << std::endl;

		std::cout << "\nWant to repeat? y/n: ";
		//std::cout << "Yes=y\n";
		//std::cout << "No=n\n";
		std::cin >> ans;

		if (std::tolower(ans) == 'y')
			total = 0.0;  // <--- Resets total so you do not continue to add to it.

	} while (std::tolower(ans) == 'y');
}


This makes use of the linked list that was created in the "F_order" function.

Hope that helps,

Andy
what if i make it all data variable include struct as global variable
Pages: 123