### Help: Case and Infinity Loop

This is a near complete code which is designed to convert fahrenheit or celsius. I face two problem which I'm not sure why its happening.

1. Case I input 1, it executes both case 1 and case 2 becomes an infinity loop. But case I input 2, it only executes case 2 and there's no loop.
2. For some reason, in 'Fahrenheit to Celsius Converter', whatever value I input for fahrenheit, the return value (fahrenheitcelsius) will be zero.

Help. I'm not why its happening.

 ```//converter.cpp #include #include using namespace std; int main() { int celsius; int fahrenheit; int choice; int counter = 0; int celsiusfahrenheit; int fahrenheitcelsius; cout << "Please choose your converter by inputting the number '1' or '2'\n"; cout << "\n1. Celsius to Fahrenheit Converter"; cout << "\n2. Fahrenheit to Celsius Converter\n\n"; cout << setw(7);cin >> choice; while (counter != 1) { switch (choice) { case 1: cout << "\nYou are now using: 'Celsius to Fahrenheit Converter'"; cout << "\n\nPlease input temperature in Celsius"; cin >> celsius; celsiusfahrenheit = celsius * (9/5) + 32; cout << celsius << " " << celsiusfahrenheit << endl; ++counter; case 2: cout << "You are now using: 'Fahrenheit to Celsius Converter'"; cout << "\nPlease input temperature in Fahrenheit"; cin >> fahrenheit; fahrenheitcelsius = (fahrenheit - 32) * (5/9); cout << fahrenheit << " " << fahrenheitcelsius << endl; ++counter; default: cout << "Please input your choice again"; break; } } } ```
you need a break at the end of each case.
If the user will enter 2 then the loop will iterate UINT_MAX - 1 times.

If the user will enter 1 the loop even will not executed.:)

You should change the condition of the loop.

EDIT: I am wrong. Because you did not place break the loop will be infinite.:)
Last edited on
Okay I fix my loop problem. Now the last problem I have is why whatever value I inputted for fahrenheit (refer to case 2), fahrenheitcelsius (after computing the formula) will always be zero?
Vlad, note that user enters value of variable choice, not counter.

As mutexe said, add `break;` after each case:
 ``123`` `````` ++counter; break; case 2:``````

And change condition of while loop to `counter = 0`. Or you might want to use boolean variable and do-while loop for that:
 ``12345678910`` ``````bool runAgain; do { runAgain = false switch(choice) { /*...*/ default: std::cout << "try again"; runAgain = true; } while(runAgain);``````

5/9
is an integer division and always equals 0.
Use 5.0/9.0

Also in your celsius loop 9/5 alway equals to 1, so it is just calculating celsius + 32
Last edited on
Hi. I still have problem with the loop. I receive infinity loop every time I inputted a case beside 1 and 2.
Here's my new code.

 ```//converter.cpp #include #include using namespace std; int main() { int celsius; int fahrenheit; int choice; int counter = 0; double celsiusfahrenheit; double fahrenheitcelsius; cout << "Please choose your converter by inputting the number '1' or '2'\n"; cout << "\n1. Celsius to Fahrenheit Converter"; cout << "\n2. Fahrenheit to Celsius Converter\n\n"; cout << setw(7);cin >> choice; do { switch (choice) { case 1: cout << "\nYou are now using: 'Celsius to Fahrenheit Converter'"; cout << "\n\nPlease input temperature in Celsius"; cin >> celsius; celsiusfahrenheit = celsius * (9.0/5.0) + 32; cout << "After Converting using 'FCelsius to Fahrenheit Converter' : \n\n"; cout << setw(7) << "Celsius" << setw(7) << "Fahrenheit\n"; cout << setw(7) << celsius << setw(7) << celsiusfahrenheit; ++counter; break; case 2: cout << "You are now using: 'Fahrenheit to Celsius Converter'"; cout << "\nPlease input temperature in Fahrenheit"; cin >> fahrenheit; fahrenheitcelsius = (fahrenheit - 32) * (5.0/9.0); cout << "After Converting using 'Fahrenheit to Celcius Converter': \n\n"; cout << setw(7) << fahrenheit << setw(7) << fahrenheitcelsius; ++counter; break; default: cout << "Please input your choice again"; break; } } while (counter < 1); } ```
 ``1234567`` `````` cout << setw(7);cin >> choice; do { cin >> choice; switch (choice) {``````
Last edited on
Problem solved. Thank you!
Topic archived. No new replies allowed.