Help: Case and Infinity Loop

Apr 22, 2013 at 2:36pm
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 <iostream>
#include <iomanip>
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;
		}
	}
}
Apr 22, 2013 at 2:43pm
you need a break at the end of each case.
Apr 22, 2013 at 2:46pm
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 Apr 22, 2013 at 2:49pm
Apr 22, 2013 at 2:50pm
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?
Apr 22, 2013 at 2:55pm
Vlad, note that user enters value of variable choice, not counter.

As mutexe said, add break; after each case:
1
2
3
	++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:
1
2
3
4
5
6
7
8
9
10
bool runAgain;
do {
    runAgain = false
    switch(choice) {
        /*...*/
        default:
            std::cout << "try again";
            runAgain = true;
    }
while(runAgain);


EDIT: to your last post:
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 Apr 22, 2013 at 2:59pm
Apr 24, 2013 at 2:33am
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 <iostream>
#include <iomanip>
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);
}
Apr 24, 2013 at 4:33am
1
2
3
4
5
6
7
	cout << setw(7);cin >> choice;

	do
	{
	cin >> choice;
	switch (choice)
		{
Last edited on Apr 24, 2013 at 4:33am
Apr 24, 2013 at 7:11am
Problem solved. Thank you!
Topic archived. No new replies allowed.