C ++陷入无限循环

时间:2013-11-04 17:05:11

标签: c++ loops while-loop infinite

好吧,伙计们,我是编程新手,需要一些帮助。我有一个程序,它输入一个句子并显示单词和元音的数量。然后,如果用户需要,我想重复该程序,但是当我使用do-while循环时,会陷入无限循环。在我输入'Y'重复之后,它会循环返回以显示我为前一个句子输入的元音和单词数。

这是我的代码:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    char sentence[50];
    int countA=0, countE=0, countI=0, countO=0, countU=0, countSP=0;
    char repeat;

    do {
    cout << "Enter sentence : ";
    cin.get(sentence, 50, '\n'); cin.ignore(10, '\n');


    cout << sentence;
    cout << "\nThird character is : " << sentence[2];
    cout << "\nLast character is : " << sentence[strlen(sentence)-1];
    cout << "\nLength of sentence is : " << strlen(sentence);

    for(int x=0; x < strlen(sentence); x++) {
        char ch = tolower (sentence[x]);
        switch (ch) {
        case 'a':   countA++;break;
        case 'e':   countE++;break;
        case 'i':   countI++;break;
        case 'o':   countO++;break;
        case 'u':   countU++;break;
        case ' ':   countSP++;break;
        }
    }


    cout << "\nNumber of A's : " << countA;
    cout << "\nNumber of E's : " << countE;
    cout << "\nNumber of I's : " << countI;
    cout << "\nNumber of O's : " << countO;
    cout << "\nNumber of U's : " << countU;
    cout << "\nNumber of words : " << countSP+1;

    cout << "\n\nWould you like to enter a new sentence? (Y/N): ";
    cin >> repeat;

    }while (repeat == 'y' || repeat == 'Y');

    _getche();
    return 0;
}

5 个答案:

答案 0 :(得分:3)

表达式(repeat == 'y' && repeat == 'Y') 总是等于false,因为repeat不能等于'y' {{1} }。

你可能意味着:

'Y'

答案 1 :(得分:1)

(repeat == 'y' && repeat == 'Y');更改为(repeat == 'y' || repeat == 'Y');

编辑: 你也没有大括号可以打开或关闭你的循环试试这个。

while (repeat == 'y' || repeat == 'Y')
   {
      _getche();
   }

因为循环没有正文而且它不知道要执行什么。

EDIT2为什么不这样做?

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
     do while (repeat == 'y' || repeat == 'Y') {
     Enter()
     cout << "\n\nWould you like to enter a new sentence? (Y/N): ";
     cin >> repeat;
}

}
return 0;

Enter(){
    char sentence[50];
    int countA=0, countE=0, countI=0, countO=0, countU=0, countSP=0;
    char repeat = Y;

cout << "Enter sentence : ";
cin.get(sentence, 50, '\n'); cin.ignore(10, '\n');


cout << sentence;
cout << "\nThird character is : " << sentence[2];
cout << "\nLast character is : " << sentence[strlen(sentence)-1];
cout << "\nLength of sentence is : " << strlen(sentence);

for(int x=0; x < strlen(sentence); x++) {
    char ch = tolower (sentence[x]);
    switch (ch) {
    case 'a':   countA++;break;
    case 'e':   countE++;break;
    case 'i':   countI++;break;
    case 'o':   countO++;break;
    case 'u':   countU++;break;
    case ' ':   countSP++;break;
    }



cout << "\nNumber of A's : " << countA;
cout << "\nNumber of E's : " << countE;
cout << "\nNumber of I's : " << countI;
cout << "\nNumber of O's : " << countO;
cout << "\nNumber of U's : " << countU;
cout << "\nNumber of words : " << countSP+1;

repeat = ' ';
}

答案 2 :(得分:1)

尝试将repeat == 'y' && repeat == 'Y'替换为repeat == 'y' || repeat == 'Y'),因为代码中的条件永远不会成立。

答案 3 :(得分:1)

要记住的主要事情是C ++执行所谓的短路评估。如果&amp;&amp;和/或条件为假,然后所有都为假。例如,

int y = 1;
int x = 2;
if (y == 0 && x ==2) {
....
}

它只是要检查第一部分。由于y = 1,它将返回一个假布尔值,并且永远不会执行此if语句。

同样明智,使用or,||,如果条件的一边为真,则条件将返回True布尔值,然后执行条件。

对于您的情况,正确的方法是:

(repeat == 'Y' || repeat == 'y');

这样,如果第一面为真,则满足条件并执行。

答案 4 :(得分:0)

您需要将循环开头的repeat设置为Yy以外的任何内容(例如:repeat = NULL;