Cant seem to get loop to work right

I am using this style of loop
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
void print_label::labelWork(string gameName, double gamePrice, string usedOrNew, int count)
{
	
	int input; 
	int num;
	

	stackType<string> gameNameStack(50);//page of labels only would have 50 labels
	stackType<double> gamePriceStack(50);//page of labels only would have 50 labels
	stackType<string> gameUsedOrNew(50); //page of labels only would have 50 labels

	stackType<string> gameNameTemp(50);//temporary stack for gameNameStack
	stackType<double> gamePriceTemp(50);//temporary stack for gamePriceStack
	stackType<string> gameUsedNewTemp(50);//temporary stack for gameUsedOrNew 
	
	cout << "Label Maker" << endl;
	cout << endl;

	cout << "Press 1 to Enter Data" << endl;
	cout << "Press 2 to Delete Data" << endl;
	cout << "Press 3 to Display Data" << endl;
	cout << "Press 4 to Show this Menu." << endl;
	cout << "Press 5 to exit" << endl;
	
	cin >> input; 
	
	
	while (input != 5) //while input != q, loop this entire code
	{ 
		if (input = 1)//for inputting
		{
			cout << "Enter Data" << endl;
			cout << endl;
			cout << "Press 0 to return to the menu when prompted." <<endl;
			
			while(input != 0)
			{
				cout << "GAME NAME: ";
				cin >> gameName;//get game name
				gameNameStack.push(gameName);//push gameName into stack

				cout << "PRICE: ";
				cin >> static_cast<double>(gamePrice);//get game price
				gamePriceStack.push(gamePrice);//push price into stack


				cout << "USED OR NEW: ";
				cin >> usedOrNew;//get used or new
				gameUsedOrNew.push(usedOrNew);//push usedOrnew onto the stack
				count++;//increment count by one
		
				//for copying
				gameNameTemp = gameNameStack;
				gamePriceTemp = gamePriceStack;
				gameUsedNewTemp = gameUsedOrNew; 

				cout << "Continue?" << endl;
				cin >> input;
			}//end while
				cout << "Choose Operation (1,2,3,4,5)" << endl;
		}//end if: 1
		
		if (input = 2)//for deleting
		{
			cout << "Delete Data" << endl;
			cout << endl;

			cout << "Enter the number of label entries you wish to delete" << endl;
			cout << num << endl; 
			cout << "Deleting " << num << " labels" << endl; 
			//for loop loops through until input is complete
			for (int i = 0; i > num; i++)
			{
				//delete one gameName and copy the new version into temp stack
				gameName = gameNameStack.top();
				gameNameStack.pop();
				gameNameTemp = gameNameStack;
				//delete one game Price and copy the new version into temp stack
				gamePrice = gamePriceStack.top();
				gamePriceStack.pop();
				gamePriceTemp = gamePriceStack;
				//delete one gameUsedOrNew and copy the new version into temp stack
				usedOrNew = gameUsedOrNew.top();
				gameUsedOrNew.pop();
				gameUsedNewTemp = gameUsedOrNew;
				
				count--;//decrement count (for printing) 
			}
			cout << "Choose Operation (1,2,3,4,5)" << endl;
		}//end if: 2
		if (input = 3)//for displaying
		{
			if (count <= 0)//if count is not 1 or greater, then don't output
			{
				cout << "NULL DATA" << endl; 
			}//end if
			else if (count > 0)
			{
				while ((!gameNameTemp.isEmptyStack()) && (!gamePriceTemp.isEmptyStack()) && (!gameUsedNewTemp.isEmptyStack()))
				{
					for (int i = 0; i < count; i++)
					{
						//set the variables to the top of the stack (respectively)
						gameName = gameNameTemp.top();
						gamePrice = gamePriceTemp.top();
						usedOrNew = gameUsedNewTemp.top();

						//output the list
						cout << gameName << "  " << gamePrice << "  " << usedOrNew << endl;
						
						//incase the user would like to continue with the 
						//stack they have, copy these 
						//BACK into the original stack.
						gameNameStack = gameNameTemp;
						gamePriceStack = gamePriceTemp;
						gameUsedOrNew = gameUsedNewTemp;

						//pop the tops of the temporary stacks so we can see the next output
						gameNameTemp.pop();
						gamePriceTemp.pop();
						gameUsedNewTemp.pop();

					}//end for
				}//end while
			}//end else if
			cout << "Choose Operation (1,2,3,4,5)" << endl;
		}//end if: 3
		if (input = 4)
		{
			cout << "Label Maker" << endl;
			cout << endl;

			cout << "Press a to Enter Data" << endl;
			cout << "Press d to Delete Data" << endl;
			cout << "Press i to Display Data" << endl;
			cout << "Press m to Show this Menu." << endl;
			cout << "Press q to exit" << endl;
		}//end if: 4
		while (input != 1|| 2 || 3|| 4|| 5)
		{
			cout << "Invalid input" << endl;
		}//end if
		cin >> input;
	}//end while
}//end getLabelInfo














I know this is a lot of code, but I cannot get the loops to work correctly!
It always returns the first if statement. And If I remove that, then it only returns the next if statement. Any ideas?
You've got to be careful about using == for comparisons in your if statements. All of your if statements are assigning values to variable input using = which is always going to be true.

~psault
This check doesn't look too good either:
while (input != 1|| 2 || 3|| 4|| 5)
So, what is a better option? I am at a loss here, I tried string values, char values, and bool values. What would you recommend?
Well, the reason is because:

while (input != 1|| 2 || 3|| 4|| 5) is basically:
while (input != (1|| 2 || 3|| 4|| 5)) which is not what you want.

You want:

while(input != 1 || input != 2 ||/*...*/)
According to the order of operations, it would be more like:
 
while( ( input != 1 ) || ( 2 ) || ( 3 ) || ( 4 ) || ( 5 ) );


Note:
 
if( 2 );  // always true 

here is another possibility:

while (input != 1 && input != 2 && input != 3 && input != 4 && input != 5)

Note:
because we are testining whether the number is not between 1 and 5 inclusive
we can also do:
while ( ! (input >= 1 && input <=5) )

Last edited on
if(input = number)
needs to be changed to
if(input == number)

the "=" symbol assigns values
the "==" symbol checks values

also see what guestgulkan said
@Helios

Thank you for the link, it makes a lot of sense when I am explained how assignment works in lamen terms.

@Firedraco
Thank you, good info to remeber (i book mark all my requests)

@guestgulken

That makes a lot more sense, and is easier to read.

@banane
Thank you, I made a nub mistake with the = and == operators. >_<
Topic archived. No new replies allowed.