Switch not working

My problem is on line 12. When I assign tempNum = 0, the program doesn't make the switch to the RECIPT case it just loops the request for an Item Number. Why doesn't this work? How can I fix it? (I feel like its something simple and obvious but I'm just not seeing it...even stepping through with the debugger has not helped.)

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
  switch (process)
  {
	case INITIAL:
	
	do
	{
        	cout<<"Please enter the Item Number"<<endl;
		cin>>tempNum;
	
		if(tempNum == 0)
		{
			process = RECIPT;
		}
		else
		{
			cout<<"Please enter the quantity bought"<<endl;
			cin>>qty;

			invIndexNum = searchInv(inventory, 5, tempNum);// find item number in inventory
		
        		if(invIndexNum >-1) //if > -1 then item is in inventory
			{
				ordIndexNum = searchCustOrder(custOrder, 5, tempNum); 
			
				if(ordIndexNum > -1)// if > -1 item has already been added to array
				{
					custOrder[ordIndexNum].QTY += qty; // adds the quanity entered to item quanity already in array
				}
				else //item is not already in array so add item to array
				{
					custOrder[x].buyItem = inventory[invIndexNum];
					custOrder[x].QTY = qty;
					x++	;// incriment to the next element to hold the next purchase		
				}
			}
			else //item is not in inventory
			{
				custOrder[x].buyItem.setItemNum(tempNum);//set custOrder with itemNum
				custOrder[x].QTY = -1;//set qty to -1 as a flag for printing recipt
				x++;
			}
		}
	
	}while(1);
	
	break;
	
case RECIPT:

	cout<<"Printing Recipt"<<endl;
		
	printRecipt(custOrder, 5);

			
	process = ANOTHER_ORDER;
	
		break;
your do while loop condition is a constant.
The do while loop is supposed to be continous untill itemNum is changed to 0. This should q the program to go to the requested case RECIPT. I have changed it to:

}while(tempNum!=0);

and it effectivly leaves the loop however it dosen't change the case to RECIPT.

Thanks, almost there! Any other ideas?
Tried this but it kicks me out of the switch:

1
2
3
4
5
if(tempNum == 0)
{
	process = RECIPT;
	break;
}


Not sure what to do....
Hi,


Is the entire switch inside a loop?

Not sure whether you know the following or not - guessing a bit on my part.

With switch the code does not 'jump' to the case because you set the variable. Once a break is encountered, control continues after the end of the switch, or loop as the case maybe.

Your code would be a bit tidier if you had each case call a function.

HTH

I wouldn't do this with switch at all. If you always go into the INITIAL case first, why not just go into the corresponding while loop without the switch and then set a boolean variable when process changes to RECIPT? Then end the while loop and go into the RECIPT case. If the while loop can end without process being equal to RECIPT, test after ending the while loop if process is equal to RECIPT. I think this makes more sense if you go into the INITIAL case anyway.
Topic archived. No new replies allowed.