C++ 无法在for循环中多次打印

C++ 无法在for循环中多次打印,c++,visual-studio,for-loop,console-application,dev-c++,C++,Visual Studio,For Loop,Console Application,Dev C++,我试着写一个程序来获取一个数字的根,例如,如果你在菜单中输入3,它会试图找到你的数字的第三个根,所以如果你输入8,它会给你2,但有一个问题。我正在使用for循环,如果我尝试在循环中为我的条件定义一个else,它将打印cout,打印次数与定义循环的次数相同。我该怎么做才能解决这个问题?有人能帮我吗?我应该改用while吗?请保持你的答案简单明了。 这是我的密码: system("cls"); system("color 78"); int roots; cout <&

我试着写一个程序来获取一个数字的根,例如,如果你在菜单中输入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();
}
系统(“cls”);
系统(“颜色78”);
int根;
库特根;
开关(根)
{
案例1:
{
系统(“cls”);
双坡;
库特波;

cout您的程序可能会很短,您只需将数字加1/根:

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(); 
系统(“cls”);
系统(“颜色78”);
int根;
库特根;
系统(“cls”);
双坡;

cout您的程序可能会很短,您只需将数字加1/根:

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(); 
系统(“cls”);
系统(“颜色78”);
int根;
库特根;
系统(“cls”);
双坡;
库特