Infinite loop!

I need to create a program that allows:

A mail order house sells five different products whose retail prices are: product 1 - $2.98, product 2 - $4.50, product 3 - $9.98, product 4 - $4.49 and product 5 - $6.87. Write a program that reads a series of pairs of numbers as follows:
a. product number
b. quantity sold
Your program should use a switch statement to determine the retail price for each product. Your program should calculate and display the total retail value of all products sold. Use a sentinel-controlled loop or end-of-data loop to determine when the program should stop looping and display the final results.

After I input all the values of the product numbers and quantities sold my program enters an infinite loop stating "The total price is: 0". I know it is a logical error, but due to inexperience am not sure how to correct it. I have been working on this since last night, five hours total, and seem to be getting nowhere!

Here is my program:

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
int product_1(0), product_2(0), product_3(0), product_4(0), product_5(0),
quantity_1(0), quantity_2(0), quantity_3(0), quantity_4(0), quantity_5(0),
money(0);

double price_1(0), price_2(0), price_3(0), price_4(0), price_5(0), TotalPrice;

cout << "Input product numbers and quantity sold:";
cin >> product_1 >> quantity_1;
cin >> product_2 >> quantity_2;
cin >> product_3 >> quantity_3;
cin >> product_4 >> quantity_4;
cin >> product_5 >> quantity_5;

switch(money)
{case 1:
price_1=quantity_1*2.98;
cout << "The price for product 1 is:" << price_1;
break;
case 2:
price_2=quantity_2*4.50;
cout << "The price for prodcut 2 is:" << price_2;
break;
case 3:
price_3=quantity_3*9.98;
cout << "The price for product 3 is:" << price_3;
break;
case 4:
price_4=quantity_4*4.49;
cout << "The price for product 4 is:" << price_4;
break;
case 5:
price_5=quantity_5*6.87;
cout << "The price for product 5 is:" << price_5;
break;}

while(money>=0)
{TotalPrice=price_1+price_2+price_3+price_4+price_5;
cout << "The total price is: " << TotalPrice << endl;}


return 0;

}


1
2
while(money>=0)
{...}


Your while loop will keep running until the value of money is less than zero. Where in your while loop do you change the value of money so that the loop will stop? Nowhere, so it will never stop.

Also, look at your switch statement:

switch(money)

Your switch statement depends on the value of the variable money. Where in your code does that get set? Once, at the start. Does it ever get changed? No. So how will you ever actually get to any of the code in the various cases in the switch statement?
Last edited on
So I changed "while(money>=0)" to "while(!cin.eof())" and erased "money(0)" and put "money;" now my program is reporting a debug error! WTF?
So now your while loop will run as long as cin is not registering the end of a file? Which file would that be?

and put "money;"


money;

That line of code does nothing at all.


The key here is to disengage fingers and think about the problem. What is your while loop meant to do?
Last edited on
My while loop is meant to figure the total amount and print the results, but also allow the user to end the program if they enter a certain variable.
I think you've misunderstood. What would be the point in a loop that just showed you the final total over and over and over and over again? The total would never change.

I've used the idea of an input file below, but it's easily adapted to a manual user input.

I think your loop is meant to loop the entire program, each time reading in the new pair of data values, and calculating the new update price.

So, the logic you're looking for is something like:

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
#include <iostream>

using namespace std;

int main()
{

  // Code here to open file, prepare to start reading data in, using an ifstream file object

  double total = 0.0; // Starting value

  while ( notReachedEndOfFile)
  {

  // Read each pair of values into variables product and quantity

    switch(product)
    {case 1:
      total=total + quantity*2.98;
      break;
    case 2:
     total=total + quantity*4.50;
      break;
    case 3:
      total=total + quantity*9.98;
      break;
    case 4:
      total=total + quantity*4.49;
      break;
    case 5:
      total=total + quantity*6.87;
      break;
}

}

// While loop has now finished looping over all input data
// Display total

cout << "Total = " << total;
return 0;
}



Last edited on
Here is what I have now and I still have not been able to finish this program. I am going on close to 24 hours on this program. Any input would be helpful. Does anyone have any more input?

//Aaron Henderson
//November 8, 2011
//Write a program that reads a series of pairs of numbers as follows:a)product number,b)quantity sold.
//Calculates and displays the total retail value of all products sold, and displays the final results
//when user chooses to stop the program.

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
int quantity_1(0), quantity_2(0), quantity_3(0), quantity_4(0), quantity_5(0), product;

cout << "Input product numbers and quantity sold:";
cin >> quantity_1;
cin >> quantity_2;
cin >> quantity_3;
cin >> quantity_4;
cin >> quantity_5;

double total=0.0;

while(!cin.eof())
{
switch(product)
{case 1:
total=total+quantity_1*2.98;
break;
case 2:
total=total+quantity_2*4.50;
break;
case 3:
total=total+quantity_3*9.98;
break;
case 4:
total=total+quantity_4*4.49;
break;
case 5:
total=total+quantity_5*6.87;
break;
}


cout << "Total =" << total;
}
return 0;

}
Your infinite loop is caused by the fact that cin.eof() never returns true. I'm not sure that it ever *can* return true, but in any event, I think you want a different test in your while statement.

Whoever wrote your instructions, though, wasn't as clear as he could have been. I'm not sure I understand the problem 100%. Are you expected to put the product numbers and desired quantities for all five products, every time through the loop?
Yes. The loop is for all five products and their quantities.
OK, I read the problem a little differently. I think it expects one pair of numbers per loop iteration. When you somehow signal that you are done with your input, it them prints the total. So, my implementation would look more like 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
#include <iostream>

using namespace std;

int main()
{
	int quantity, product;
	double total = 0.0;
	bool	exitLoop = false;

	while(!exitLoop)
	{
		cout << "Input product numbers and quantity sold (0 to exit): ";
		cin >> product;
		if (product > 0)
			cin >> quantity;

		switch(product)
		{
		case 0:
			exitLoop = true;
			break;
		case 1:
			total += quantity * 2.98;
			break;
		case 2:
			total += quantity * 4.50;
			break;
		case 3:
			total += quantity * 9.98;
			break;
		case 4:
			total += quantity * 4.49;
			break;
		case 5:
			total += quantity * 6.87;
			break;
		}
	}
	cout << "Total = " << total << endl;

	return 0;
}


The only thing I haven't figured out yet is how to signal that you're done. I'm pretty sure that cin.eof() isn't going to work, no matter what you input. Give me a little more time on this one.

Also, if your teacher hasn't mentioned it yet, reading integers can be a little problematic. What if you input invalid characters? It's best (IMO) to read in character strings, and do the conversion yourself. You'll have more control over errors that way.

EDIT: I modified my version of the program slightly. I require the user to input a 0 when he's finished. Also, bear in mind that there's no range-checking in this program; I don't know if your teacher will be OK with that or not.
Last edited on
I'm just as confused about how to fix it as you are brother:)Believe me you can have all the time you need if you have a viable solution, because, I am lost.
OK...I did modify my solution above. It works, provided you're permitted to request the user to enter a 0 as a sign s/he's done.

What kind of computers/OS are you running on? I'm curious, because I've never seen anyone use cin.eof() before.
Windows 7
OK...well, hopefully the changes I made will work for you. If not, let me know what more you want to do with it, and I'll try to help.
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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
double product[5];
double quantity[5];
int  money = 0;

double price[5]; 
 double totalPrice;

for(int i = 0; i < 5; i++){
        double num1;
        cout << "Input product numbers  "<< i << ": "<<endl;
        cin >> num1;
       product[i] = num1;
       }
       
       for(int i = 0; i < 5; i++){
        double num2;
        cout << "Input  quantity sold "<< i << ": "<<endl;
        cin >> num2;
       quantity[i] =  num2;
       }
       


cout << "This is money, just press witch price you want to know? " << endl;
cout << "1) Truck " << endl;
cout << "2) Car " << endl;
cout << "3) Potato " << endl;
cout << "4) Watermelon " << endl;
cout << "5) Books " << endl;
cin >> money;

   
       
        

switch(money)
{case 1:
price[0]=quantity[0]*2.98;
cout << "The price for product 1 is:" << price[0] << endl;
break;
case 2:
price[1]=quantity[1]*4.50;
cout << "The price for prodcut 2 is:" << price[1] << endl;
break;
case 3:
price[2]=quantity[2]*9.98;
cout << "The price for product 3 is:" << price[2] << endl;
break;
case 4:
price[3]=quantity[3]*4.49;
cout << "The price for product 4 is:" << price[3] << endl;
break;
case 5:
price[4]=quantity[4]*6.87;
cout << "The price for product 5 is:" << price[4] << endl;
break;

default:
        cout << "No product. " << endl;
        

}

while(money>=0)
{totalPrice=price[0]+price[1]+price[2]+price[3]+price[4];
cout << "The total price is: " << totalPrice << endl;
break;
}

while(money >= 0)
{totalPrice=price[0]+price[1]+price[2]+price[3]+price[4];
cout << "The total price is: " << totalPrice << endl;
break;
}






system("PAUSE");
return 0;
}


I have fixed your problem. Pay attention to "totalPrice" variable. It starts with Capital letter.
C++ is case-sensitive language. And totalPrice is working like a charm if you had only calculated all those prices under a while loop. Not the switch statement...
Thank you sooooooooooo much for your help! Your method worked the first time!
Not a problem bro :D
Last edited on
Topic archived. No new replies allowed.