cout在for循环中打印这么多次

Cout prints so many times in the for loop

我尝试编写一个程序来获取数字的根,例如,如果您在菜单中输入 3,它会尝试找到您数字的三次方根,所以如果您输入 8,它会给您 2 , 但有个问题。我正在使用 for 循环,如果我尝试在循环中为我的条件 定义一个 else,它会打印 cout,因为循环也被定义了很多次。我应该怎么做才能解决这个问题?有人可以帮帮我吗?我应该改用 while 吗?请让您的答案简单明了。 这是我的代码:

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();
}

(很快,问题是循环中的 else 被打印的次数与循环定义的次数一样多,我不希望这种情况发生。非常感谢您的帮助。)

你的程序可以更短,你只需要乘以 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(); 

这里你可能会出现数值错误,比如 8 的三次方为 1.9999,你只需要将数字四舍五入到最接近的整数即可。我会这样做:

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();