Hey guys,

How would I make this program work without a "while" just using "if" statements. I know doing it this way is probably more efficient but there must be a way to do it with just "if" statements. Thanks!

#include <iostream>
using namespace std;

int main()
    int ftemp;
    int ctemp;
    int select = -1;

    while (select ==-1)
        cout << "Please select from the following : " << endl;
        cout << "1) Fahrenheit-to-Celsius" << endl;
        cout << "2) Celsius-to-Fahrenheit" << endl << endl;
        cout << "Enter: ";
        cin >> select;
        if (select == 1)
            cout << " Enter temperature in Fahrenheit to convert to degrees Celsius: ";
            cin >> ftemp;
            ctemp = (ftemp-32) * 5 / 9;
            cout << "Equivalent in Celsius is: " << ctemp << endl;
            system ("pause");
            return 0;
        else if (select == 2)
            cout <<" Enter temperature in Celsius to convert to degrees Fahrenheit: ";
            cin >> ctemp;
            ftemp = ctemp*9/5 + 32;
            cout << "Equivalent in Fahrenheit is: " << ftemp << endl;
            system ("pause");
            return 0;

            cout << "Valid options 1 or 2." << endl;
      select = -1;

    return 0;

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Without a loop, there's no way you'll get back to the choice option - it will become a one pass program.

From the standpoint of a continually running program, you need a third option - Quit. That choice (say, 3) should be what controls the loop.

So, you want to go back to some line without a loop?
It is not suggested. Beware, do not do this practice, but for the sake of your curiosity, there exists a goto statement. Please do not use this in your everyday life.
There is a good example on implementing goto instead of loops here:http://www.java2s.com/Tutorial/Cpp/0060__Operators-statements/gotoloopexample.htm and also here:http://www.cplusplus.com/doc/tutorial/control.html but I don't recommend it.

PS: The title of your thread suggest that the problem is something to do with Temperature Conversions though it is not. Please use more specific names such as "Control Flow without loops" or "Alternative of loops"

you can do it with a goto: Statement like

cout << "enter choice " ;
cin>> choice;

if(choice == 1)
goto start;
//and put the start in the start of your program  like 


Not letting someone use a for or while loop in C++, is like telling them to just as well write it by opcode, because it's ugly!

This article has been dead for over six months. Start a new discussion instead.