Start New Discussion within our Software Development Community

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.
MY CODE..

#include<iostream>
#include<conio.h>

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;
    cin>>num;
    while( num == 1 || num == 2 || num == 'y' || num == 'Y')
    {
    if(num == 1){
        cout<<"Please enter the temperature in centigrade"<<endl;
        cin>>centigrade;
        result = (centigrade * 9/5) + 32;

    }
     if (num==2)
    {
        cout<<"Please enter the temperature in fahrenheit"<<endl;
        cin>>fahrenheit;
        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";
    cin>>num;
    if (num== 'n' || num== 'N')
    {
        cout<<"Exit the Program";
    }

}
    getch();
    return 0;
}

To loop a whole program this is what I use:

char continue;
do
{
    // 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.

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