简体   繁体   English

C ++陷入无限循环

[英]C++ Stuck in infinite loop

Alright guys, I'm new to programming and need a little help. 好的,我是编程新手,需要一点帮助。 I have a program that takes a sentence that's entered and shows the number of words and vowels. 我有一个程序,该程序接受输入的句子并显示单词和元音的数量。 I then want to repeat the program if the user wants so, but when I use the do-while loop, in gets stuck in an infinite loop. 然后,如果用户需要,我想重复该程序,但是当我使用do-while循环时,in陷入无限循环。 After I enter 'Y' to repeat, it loops back to show the vowels and number of words I entered for the previous sentence. 输入“ Y”重复后,它会循环显示我为上一个句子输入的元音和单词数。

Here's my code: 这是我的代码:

#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;
}

The expression (repeat == 'y' && repeat == 'Y') will always equal false, as repeat cannot be equal to both 'y' and 'Y' . 表达式(repeat == 'y' && repeat == 'Y')始终等于false,因为repeat不能同时等于'y' 'Y'

You might have meant: 您可能意味着:

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

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

EDIT: You also have no braces to open or close your loop try this. 编辑:您也没有括​​号打开或关闭您的循环尝试此。

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

because the loop has no body and it does not know what to execute. 因为循环没有主体,也不知道要执行什么。

EDIT2 why not do this? 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 = ' ';
}

Try replacing repeat == 'y' && repeat == 'Y' with repeat == 'y' || repeat == 'Y') 尝试用repeat == 'y' && repeat == 'Y' repeat == 'y' || repeat == 'Y')替换repeat == 'y' && repeat == 'Y' repeat == 'y' || repeat == 'Y') , because the condition in your code can never be true. repeat == 'y' || repeat == 'Y') ,因为代码中的条件永远不会为真。

The main thing to remember is that C ++ does what is called Short-Circuit evaluation. 要记住的主要事情是C ++做所谓的短路评估。 If one side of the && condition is false, then everything is false. 如果&&条件的一侧为假,则一切为假。 For an example, 举个例子

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

It is just going to check the first part. 它只是要检查第一部分。 Since y = 1, it is going to return a false Boolean and this if statement will never be executed. 由于y = 1,它将返回一个错误的布尔值,并且此if语句将永远不会执行。

Like wise, with or, ||, if one side of the condition is true, then the condition will return a True Boolean and then the condition will be executed. 同样,如果条件的一侧为true,则使用or或||,则条件将返回True布尔值,然后将执行条件。

For your situation, the correct way to do this would be: 对于您的情况,正确的方法是:

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

This way, if the first side is true, then the condition will be met and it will execute. 这样,如果第一面为真,则将满足条件并执行该条件。

您需要在循环开始时将repeat设置为Yy以外的任何其他值(例如: repeat = NULL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM