I want to create a do while loop for a statement to keep on looping until a following number or string value is passed. this is the code that i was trying to put in.

#include "vending.h"

int main()
{
	cout << "\n\n\n\n----------Welcome to the coolest vending machine!!!!!!!-----------\n\n";
	cout << "Press 1 and enter to display the list of products in the vending machine.\n";
	cout << "Press 2 and enter to check the price of each product.\n";
	cout << "Press 3 and enter to check the quantity of the products.\n";
	cout << "Press 4 and enter twice to exit out of the vending machine.\n";
	cout << endl;

	vendingM vmObject;
	int x;
	
	do{
	cin >> x;
	cout << endl;

	switch(x)
	{
	case 1: 
		vmObject.displayList();
		break;
	case 2:
		
		//do{
			vmObject.priceChecker();
		//}while(y != 0);
		break;
	case 3:
	
		//do{
			vmObject.quantityOfProducts();

		//}while(q!=0);

	}
	}while( x!=4);

	
	
	



	system("pause");
	return 0;
};

help!!

Can you specify, what error you are getting?

Im guessing you are getting error due to the face that you didnt specify a default case for switch?

if not, please tell us what kind of error you are experiencing.

also, I suggest using

cin.get();
//instead of
system("PAUSE");

Can you specify, what error you are getting?

i am not getting any error.it just doesnt break out of the loop. it continues looping.

Im guessing you are getting error due to the face that you didnt specify a default case for switch?

if not, please tell us what kind of error you are experiencing.

also, I suggest using

cin.get();
//instead of
system("PAUSE");

i am not getting any error.it just doesnt break out of the loop. it continues looping.

I don't see any problem other than poorly formatted code. See this -- proper formatting usually helps find errors.
Also, in case #3 you should add a break. If you add more CASEs and forget, you've got another error.

Also, in case #3 you should add a break. If you add more CASEs and forget, you've got another error.

With all due respect, Do the last switch case statement require a break? I think not. Off course, in future should he add more cases, and forget to put break, it can be an error.....

commented: Any reason you needed to say exactly what I said - and no more? -4

i am not getting any error.it just doesnt break out of the loop. it continues looping.

Well OP, Your code do have a problem, see while using "Do while" loop you should increment the value of the integer inside the loop

So currently the value for (x) is never getting incremented. Thus the value never reaches 4, so it becomes infinite loop.

#include "vending.h"
 
int main()
{
	cout << "\n\n\n\n----------Welcome to the coolest vending machine!!!!!!!-----------\n\n";
	cout << "Press 1 and enter to display the list of products in the vending machine.\n";
	cout << "Press 2 and enter to check the price of each product.\n";
	cout << "Press 3 and enter to check the quantity of the products.\n";
	cout << "Press 4 and enter twice to exit out of the vending machine.\n";
	cout << endl;
 
	vendingM vmObject;
	int x;
 
	do{
	cin >> x;
	cout << endl;
 
	switch(x)
	{
	case 1: 
		vmObject.displayList();
		x++;
                break;
	case 2:
 
		//do{
			vmObject.priceChecker();
		//}while(y != 0);
                        x++;		
                        break;
	case 3:
 
		//do{
			vmObject.quantityOfProducts();
 
		//}while(q!=0);
                        x++;
	}
	}while( x!=4);
 
 
 
 
 
 
 
	system("pause");
	return 0;
};

Now the value of x will be incremented in each case, and loop will continue as long as x!=4.

Well OP, Your code do have a problem, see while using "Do while" loop you should increment the value of the integer inside the loop

So currently the value for (x) is never getting incremented. Thus the value never reaches 4, so it becomes infinite loop.

#include "vending.h"
 
int main()
{
	cout << "\n\n\n\n----------Welcome to the coolest vending machine!!!!!!!-----------\n\n";
	cout << "Press 1 and enter to display the list of products in the vending machine.\n";
	cout << "Press 2 and enter to check the price of each product.\n";
	cout << "Press 3 and enter to check the quantity of the products.\n";
	cout << "Press 4 and enter twice to exit out of the vending machine.\n";
	cout << endl;
 
	vendingM vmObject;
	int x;
 
	do{
	cin >> x;
	cout << endl;
 
	switch(x)
	{
	case 1: 
		vmObject.displayList();
		x++;
                break;
	case 2:
 
		//do{
			vmObject.priceChecker();
		//}while(y != 0);
                        x++;		
                        break;
	case 3:
 
		//do{
			vmObject.quantityOfProducts();
 
		//}while(q!=0);
                        x++;
	}
	}while( x!=4);
 
 
 
 
 
 
 
	system("pause");
	return 0;
};

Now the value of x will be incremented in each case, and loop will continue as long as x!=4.

hey thanks a lot..i don't know why in the world i was thinking that if we input 4 as the value to be passed to the variable it will break out of the loop..my bad!!..you see..i am in the learning process and i have a lot to learn and people like you to provide your help to newbies like me is very appreciative. thanks!.

Well OP, Your code do have a problem, see while using "Do while" loop you should increment the value of the integer inside the loop

So currently the value for (x) is never getting incremented. Thus the value never reaches 4, so it becomes infinite loop.

Not true. What does cin >> x; do to x immediately after the do ?

With all due respect, Do the last switch case statement require a break? I think not. Off course, in future should he add more cases, and forget to put break, it can be an error.....

Come on WaltP, I asked a question (To you off course), with all the kindness in the world. With that also mentioned what could be the answer(The one i think was correct). So basically i was seeking your answer for that part.

And presently if there is no needed of a "break" after the third case. I was justifying your statement, by saying that you actually meant about the future problem, if the OP should add more cases, and not about the present piece of code.

Final word: I seeked your knowledge & justified your statement. You don't need to thumbs me down every now and then.

And my post wasn't a copy of your's. At least i don't mean to....

Not true. What does cin >> x; do to x immediately after the do ?

My bad! I didn't notice it. You are right. The value is getting changed. Then what do think can cause this never ending loop?

And Op, Sorry my code actually deviate from the logic you have been working on. So just forget it. Your code seems to be fine.

One thing you may try is this (Should the problem persist)

break; //of the case three
case(4):  exit(); //include "process" header file...

That way should the user input 4, it will terminate the program, not waiting for the test statement of do-while loop...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.