簡體   English   中英

Cin 似乎在這個循環中失敗了。 誰能解釋我做錯了什么?

[英]Cin appears to be failing inside this loop. Can anyone explain what I'm doing wrong?

我在嘗試構建下面的代碼時遇到編譯器錯誤,我不太明白失敗的原因和原因。

它指向第 45 行 (cin>>x[ctr]),其中包含“'struct std::enable_if<false, std::basic_istream&>' 中沒有名為'type'的類型”消息。

我幾天前才開始編碼,英語不是我的母語。 如果這個問題低於社區的薪水等級,我們深表歉意。 希望你能給我指出正確的方向。

cpp.sh/34sm3

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iomanip>
//#include "_pause.h"


using namespace std;


//////////////////////////////////////////////////////////////////
//                               NOTE
// This is your program entry point. Your main logic is placed
// inside this function. You may add your functions before this
// "main()", or after this "main()" provided you added reference
// before this "main()" function.
//////////////////////////////////////////////////////////////////


//Write a program that can divide six non-zero integers (two integers per division) from the user and display the result to the user. 
//Create a function that will perform the division operation. Display only the non-decimal part of the quotient.




float quot (int num1, int num2)
{
    return num1/num2;
}


int main() 
{
    // ************************** TO DO **************************
    // Place your code logic after this comment line
    // ***********************************************************
    int x[6];
    cout<<"Enter 6 integers to divide: ";
    for(int ctr=0; ctr<6; ctr++)
    {
        cin>>[ctr];
        if(x[ctr]==0)
        {
            cout<<"Invalid number! Please enter a non-zero value: ";
            cin>>x[ctr];
        }
    }


    cout<<"The quotient of the first pair is: " << quot(x[0],x[1]) <<endl;
    cout<<"The quotient of the second pair is: " << quot(x[2],x[3]) <<endl;
    cout<<"The quotient of the third pair is: " << quot(x[4],x[5]) <<endl;


    system ("pause");
    return EXIT_SUCCESS;
}

C++ 中是否不允許循環內的 cin 語句?

我想您需要將cin>>[ctr]替換為cin>>x[ctr] .....這可能會解決您的錯誤。

對於初學者, quot應該寫成這樣

float quot (int num1, int num2)
{
    return static_cast<float>( num1 ) / num2;
}

否則,返回類型 float 沒有多大意義,因為在此表達式num1 / num2中使用了 integer 算術。

很明顯,在這個聲明中

cin>>[ctr];

有一個錯字。 您忘記指定數組名稱x

cin >> x[ctr];

最好將此 if 語句替換為

    if(x[ctr]==0)
    {
        cout<<"Invalid number! Please enter a non-zero value: ";
        cin>>x[ctr];
    }

有一段時間像

    while ( x[ctr] == 0 )
    {
        cout<<"Invalid number! Please enter a non-zero value: ";
        cin>>x[ctr];
    }

這就是你所要做的。

只需將cin>>[ctr]更改為cin>>x[ctr]
它在第41行。 需要在索引前指定數組名, x [ctr]
這是你的代碼。

我建議您使用調試器自己找出此類小的人為錯誤。 此外,在尋求解決方案之前,請閱讀您的代碼並嘗試可視化其流程。

int main() 
{
    // ************************** TO DO **************************
    // Place your code logic after this comment line
    // ***********************************************************
    int x[6];
    cout<<"Enter 6 integers to divide: ";
    for(int ctr=0; ctr<6; ctr++)
    {
        cin>>x[ctr];
        if(x[ctr]==0)
        {
            cout<<"Invalid number! Please enter a non-zero value: ";
            cin>>x[ctr];
        }
    }


    cout<<"The quotient of the first pair is: " << quot(x[0],x[1]) <<endl;
    cout<<"The quotient of the second pair is: " << quot(x[2],x[3]) <<endl;
    cout<<"The quotient of the third pair is: " << quot(x[4],x[5]) <<endl;


    system ("pause");
    return EXIT_SUCCESS;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM