Search code examples
c++visual-studiofor-loopconsole-applicationdev-c++

Cout prints so many times in the for loop


I tried to write a program that gets the roots of a number for example if you entered 3 in the menu it would try to find the third root of your number so if you have entered 8, it would give you 2, but there is a problem. I am using a for loop and if I try to define an else for my condition in the loop it would print the cout as many times the loop is defined too. What should I do to fix this? Can someone please help me? Should I use while instead? And please keep your answers simple and obvious. Here is my code:

system("cls");
    system("color 78");
    int roots;
    cout << "Which root are you trying to find out?" << endl;
    cin >> roots;
    switch (roots)
    {
    case 1:
    {
        system("cls");
        double po;
        cout << "Please enter the number you wnat to see its first root: " << endl;
        cin >> po;
        cout << "Your number's first root is: " << po << endl;
    Backhome();

    }
    break;
    case 2:
    {
    system("cls");
    double p;
    cout <<"Please enter the number you want to find its second root:"<< endl;
    cin >> p;
    cout <<"Your number's second root is: "<< sqrt(p) << endl;
    cout << "Your number's second root is: " << "-" << sqrt(p) << endl;
    Backhome();
    }
    break;
case 3:
{
    system("cls");
    double th;
    cout << "Please enter the number you want to find its third root: "<< endl;
    cin >> th;
    for (int i= -10000 ; i <= 10000; i++)
    {
        if ((i*i*i) == th)
        {
            cout << "Your number's third root is: " << i << endl;
        }
        else
        {
         cout << "Your number doesn't have a third root." << endl;
         }
    }
    Backhome();
}
break;
case 4:
{
    system("cls");
    double foot;
    cout << "Enter the number you want to see its fourth root: " << endl;
    cin >> foot;
    for (int i = -10000; i <= 10000; i++)
    {
        if ((i*i*i*i) == foot)
        {
            cout << "Your number's fourth root is: " << i << endl;
        }
        else
        {
         cout << "Your number doesn't have a fourth root." << endl;
         }
    }
    Backhome();
}
break;
case 5:
{
    system("cls");
    double pive;
    cout << "Enter the number you want to see its fifth root: " << endl;
    cin >> pive;
    for (int i = -10000; i <= 10000; i++)
    {
        if ((i*i*i*i*i) == pive)
        {
            cout << "Your number's fifth root is: " << i << endl;
        }

        else
        {
         cout << "Your number doesn't have a fifth root." << endl;
         }
}
    Backhome();
}

(Shortly, the problem is that the elses in the loops are printed as many times as the loop is defined and I don't want that to happen. I would appreciate your help.)


Solution

  • Your program can be quite shorter, you just need to power the number by 1/roots:

    system("cls");
    system("color 78");
    int roots;
    cout << "Which root are you trying to find out?" << endl;
    cin >> roots;    
    system("cls");
    double po;
    cout << "Please enter the number you wnat to see its " << roots << "th root: " << endl;
    cin >> po;
    cout << "Your number's " << roots << "th root is: " << pow(po, 1.0/roots) << endl;
    Backhome(); 
    

    You'll probably have numerical errors here, like getting 1.9999 for the third root of 8, you just need to round the number to the nearest integer. I would do:

    double result = pow(po, 1.0/roots);
    int resultUp = ceil(result);
    int resultDown = floor(result);
    int nearestInt = resultUp;
    if ((result - resultDown) < (resultUp - result))
         nearestInt = resultDown;
    
    if (abs(pow(nearestInt, roots) - po) < 1e-6)
         result = nearestInt;
    
    cout << "Your number's " << roots << "th root is: " << result << endl;
    Backhome();