so im trying to write this program that converts Fahrenheit to Celsius and vice versa but im gettin an infinate loop at the menu. i go to select the number that i want and it just displays the menu again, i though i was doin it right?

#include <iostream>	//for cin and cout
#include <iomanip>  
#include <cmath>

using namespace std;

void main()
{
	double C,	//Celsius
                       F;//Fahrenheit

	int choice;	//Number for choice

	#define cls   ("cls")	// clear system
	#define pause ("pause")	// pause system

	do
	{
		
		cout << "Select one the the following options: \n\n";
			cout << "  1. Temperature conversion from Fahrenheit to Celsius: \n\n"
				 << "  2. Temperature conversion from Celsius to Fahrenheit: \n\n"
				 << "  3. Quit the program now: \n" << endl;

		cin >> choice;
		
		switch (choice)
		{
		case '1': cout << "Enter the degrees in Fahrenheit: \n\n";
				  cin >> F;
				  
				  C = 5 / 9 * ( F - 32 );

				  cout << "The conversion is: " << C << endl;
				  break;

		case '2': cout << "Enter the degrees in Celsius: \n\n";
				  cin >> C;

				  F = 9 * C / 5 + 32;

				  cout << "The conversion is: " << F << endl;
				  break;

		case '3': cout << "Program closed" << endl;
				  break;
		}
	}
	while (choice != '3');
}

<< moderator edit: added [code][/code] tags >>

Dani AI

Generated

Good catch from — the menu kept repeating because the switch labels didn’t match the type you read. A few other improvements and pitfalls worth addressing so the same or related problems don’t pop up later:

  • Use a standard int main() and return a value. void main() is nonstandard.
  • Watch integer division: 5/9 is zero with integers. Use floating-point constants (5.0/9.0) or reorder the math so you multiply by a float.
  • Validate input: if cin >> choice fails (non-numeric input), cin goes into a fail state and the loop can turn into an effective infinite loop unless you clear() and ignore() the bad input.
  • Add a default case to the switch to handle unexpected choices and give the user feedback.
  • Avoid unnecessary #define shorthands for system calls; system() is platform-specific and has security implications.

A minimal pattern to follow (input validation + proper division + clear switch) looks like:

#include <iostream>
#include <limits>

int main()
{
    int choice;
    double fahrenheit, celsius;

    while (true) {
        std::cout << "Enter 1, 2, or 3: ";
        if (!(std::cin >> choice)) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid input; please enter a number.\n";
            continue;
        }

        switch (choice) {
            case 1:
                // convert using floating-point math
                break;
            case 2:
                // convert using floating-point math
                break;
            case 3:
                return 0;
            default:
                std::cout << "Choice must be 1, 2 or 3.\n";
        }
    }
}

If the menu still loops unexpectedly, instrument the code with a small cout after reading choice to confirm what value was parsed, and test with non-numeric input to ensure your cin.clear() / ignore() path runs. For the authoritative rule on main signatures see cppreference: .

Recommended Answers

All 2 Replies

Since choice is an int, compare it with 1, 2, and 3 -- not with characters '1', '2', and '3'.

oh geez i never though about thanks makes sense now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.