简体   繁体   中英

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. After I enter 'Y' to repeat, it loops back to show the vowels and number of words I entered for the previous sentence.

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' .

You might have meant:

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

Change (repeat == 'y' && repeat == 'Y'); to (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?

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') , because the condition in your code can never be true.

The main thing to remember is that C ++ does what is called Short-Circuit evaluation. 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.

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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