Hey guys ..!!
so today I was solving another problem involving a loop in it and i'm not sure exactly how to apply that condition to repeat it unless the terminating condition becomes true.
I tried to do it on my own but its not working. can anyone of you please tell me how to do it correctly.


using namespace std;

int main()
    int num;
    float centigrade, fahrenheit, result; 
    cout<<"Please select from the following options. Press (1 or 2). \n1.Centigrade to Fahrenheit. \n2.Fahrenheit to Centigrade."<<endl;
    while( num == 1 || num == 2 || num == 'y' || num == 'Y')
    if(num == 1){
        cout<<"Please enter the temperature in centigrade"<<endl;
        result = (centigrade * 9/5) + 32;

     if (num==2)
        cout<<"Please enter the temperature in fahrenheit"<<endl;
        result = (fahrenheit- 32)* 5/9;
    }if (num !=1 && num != 2)
        cout<<"\n You entered the invalid input number.";
    cout<<"Temperature is : "<<result;
    cout<<"\n\npress 'Y' for again repeat and 'N' for exit\n";
    if (num== 'n' || num== 'N')
        cout<<"Exit the Program";

    return 0;

To loop a whole program this is what I use:

char continue;
    // program code goes here
    std::cout << <<"Press 'Y' to repeat and 'N' tp exit\n";
    std::cin >> continue;
} while (::tolower(continue) != 'n');

This will continue the loop as long as an N or n is not entered.

Edited 1 Year Ago by NathanOliver

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