WHILE LOOP HELP

Apr 15, 2014 at 3:21pm
I am attempting to create a loop that asks for more money if the user does not pay adequately... my WHILE loop just keeps going even when temp = 0 it doesnt move on with the program

can somebody tell me what I am doing wrong PLEASE...

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
 #include <iostream>
   
	  #include <string>
	  #include <cmath>
 
	  using namespace std;
   
	  struct Machine
	  {
	  string drinkName;
	  float cost;
	  int totalStock;
 	  };
 
	  struct Machine drink[] = {{"Cola", 0.75, 20}, {"Beer", 0.75, 20}, { "Lemon-Lime", 0.75, 20}, {"Grape Soda", 0.80, 20}, {"Cream Soda", 0.80, 20}};

int main()
 
	  {
 
	  int menuChoice;
	  static float moneyTot;
	  double moneyIn;
	  double moneyIn2;
	  float temp;
	  bool mainMenu = true;

	  while (mainMenu != false)

	  {
	  cout <<"Please make a selection:" << endl;
 	  cout <<"1: Cola		- Cost: \x9C" <<drink[0].cost << "- Stock: " <<drink[0].totalStock << endl;
 	  cout <<"2: Beer		- Cost: \x9C" <<drink[1].cost << "- Stock: " <<drink[1].totalStock << endl;
 	  cout <<"3: Lemon-Lime	- Cost: \x9C" <<drink[2].cost << "- Stock: " <<drink[2].totalStock << endl;
 	  cout <<"4: Grape Soda	- Cost: \x9C" <<drink[3].cost << "- Stock: " <<drink[3].totalStock << endl;
 	  cout <<"5: Cream Soda	- Cost: \x9C" <<drink[4].cost << "- Stock: " <<drink[4].totalStock << endl;
 	  cout <<"6: Quit" << endl;
 
	  cin >> menuChoice;
 
	  if (menuChoice == 1)
 
	  {
	  	
	  cout<< "THIS MACHINE ACCEPTS AMOUNTS BETWEEN \x9C 0.01 and \x9C 1.00" << endl; 
	  cout << "PLEASE ENTER PAYMENT";
 
	  cin >> moneyIn;
	 
	  temp = drink[0].cost - moneyIn;

	  
 
	  while(temp < drink[0].cost || temp != 0)
	  {
		  cout <<"COST STILL OWNING:" << temp << endl;
		  cin >> moneyIn2;
		  temp = temp - moneyIn2;
		  moneyIn = drink[0].cost + temp;
	  }  
	  // A WHILE LOOP JUST KEEPS LOOPING EVEN PAST temp==0
	  // IF STATEMENT JUST EXITS APPLICATION AT THIS POINT !!!!
			  
	  if (moneyIn == drink[0].cost)
	  {
      drink[0].totalStock--;
	  cout <<"THANK  YOU FOR YOUR CUSTOM" << endl;
	  }
 
	  else if (moneyIn > drink[0].cost)
	  {
 
	  cout <<" YOU CHANGE IS:" << (moneyIn - drink[0].cost) << endl;
	                drink[0].totalStock--;
	  cout <<"THANK  YOU FOR YOUR CUSTOM" << endl;
	  }
	  }
	  
	  	  return 0;
 
	  }
	  }
Apr 15, 2014 at 3:35pm
If you fixed your indentation style, the code would be much more clear to you and you could easily spot the problem.
Apr 15, 2014 at 3:40pm
line 54: You want an and condition, not an or condition. If amount owed (temp) is zero it is always going to less than cost and the while condition will be true.
Topic archived. No new replies allowed.