2017-10-13 44 views
0

我是介绍C++计算机科学课程的学生,这是我第一次在这里发布。我们刚刚了解while循环,尽管赋值不需要它,但我正在尝试对此赋值进行输入验证。该计划旨在读取数字列表并找出列表中第一个和最后8个位置的位置。因此,如果我有四个数字(1,8,42,8)的列表,则第一个和最后8个位置是2和4.该组的大小由用户决定。C++输入验证while循环不终止

我试图做一个while循环,测试以确保用户输入的内容实际上是一个数字,但是当我尝试输入类似“。”的内容时。或“a”循环无限地继续并且不终止。我无法找到我的错误,并且据我所知,我使用的语法与我的教科书中的语法完全相同。有人能告诉我我的while循环有什么问题吗?

int numbers,   //How large the set will be 
    num,    //What the user enters for each number 
    first8position = 0, //The first position in the set that has an 8 
    last8position = 0; //The last position in the set that has an 8 

//Prompt the user to get set size 
cout << "How many numbers will be entered? "; 
cin >> numbers; 

//Loop to get all the numbers of the set and figure out 
//which position the first and last 8 are in 
for (int position = 1; position <= numbers; position++) 
{ 
    cout << "Enter num: "; 
    cin >> num; 

    //If num isn't a digit, prompt the user to enter a digit 
    while (!isdigit(num)) 
    { 
     cout << "Please enter a decimal number: "; 
     cin >> num; 
    } 

    //If num is 8, and first8position still isn't filled, 
    //set first8position to the current position. 
    //Otherwise, set last8position to the current position. 
    if (num == 8) 
    { 
     if (first8position == 0) 
      first8position = position; 
     else 
      last8position = position; 
    } 


} 

//If the set had an 8, print what its position was 
if (first8position != 0) 
    cout << "The first 8 was in position " << first8position << endl; 

//If there was more than one 8, print the last 8 position. 
//Otherwise, the first and last 8 position are the same. 
if (last8position != 0) 
    cout << "The last 8 was in position " << last8position << endl; 
else 
    cout << "The last 8 was in position " << first8position << endl; 

//If there were no 8s, say so. 
if (first8position == 0) 
    cout << "Sorry, no eights were entered."; 

return 0; 

}

+3

相关/欺骗https://stackoverflow.com/questions/19521320/why-do-i-get-an-infinite-loop-if-i-enter-a-letter-rather-than-a - – NathanOliver

+4

你也没有正确使用'std :: isdigit'。要看它是如何工作的,请看:http://en.cppreference.com/w/cpp/string/byte/isdigit – NathanOliver

回答

1

两个问题是导致你的无限循环:

首先,cin >> num,试图在一个整数值读取。如果用户输入类似于a.(不能是积分值的开始)的内容,则不会读入任何内容,并且a.保留在输入缓冲区中;因此,每个后续cin >> num将立即失败(没有给用户输入的东西的机会,因为a.仍然在输入缓冲区,并将保持在那里)。因此,在这种情况下,您必须使用cin中的这些字符,例如通过使用cin.ignore,你将不得不重置在这种情况下设置的failbit

其次,注意,如果isdigit(int c)检查ASCII - 值c是一个数字,即,如果c >= 48 && c <= 57。因此,您的支票isdigit(num)将会失败,直到用户输入一个介于4857之间的数字。

请参阅以下代码,演示如何处理输入故障。希望能帮助到你。

int main() { 

    int num; 
    cin >> num; 
    while (!cin.eof() && cin.fail()) { // failure when extracting an integral value? 
     cout << "not an integral value." << endl; 

     // clear failbit 
     cin.clear(); 

     // remove characters that are still in the input buffer (until next end of line) 
     cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 

     // next try to read in an integer 
     cin >> num; 
    } 
    if (!cin.eof()) { 
     cout << "juu:" << num << endl; 
    } 
} 
+0

你可以阅读下一个'num',并用'while(!(cin >> num))'。 – WorldSEnder

+0

@WorldSEnder:它必须是'while(!(cin >> num))',但是当到达EOF时你可能遇到无限循环。这可以通过专门测试'failbit'来避免。 –

+0

你的回答很清楚,很全面。非常感谢! – Kronimiciad