C++ C++;陷入无限循环

C++ C++;陷入无限循环,c++,loops,while-loop,infinite,C++,Loops,While Loop,Infinite,好了,伙计们,我是编程新手,需要一些帮助。我有一个程序,它接收输入的句子,并显示单词和元音的数量。如果用户愿意,我想重复这个程序,但是当我使用do-while循环时,in陷入了一个无限循环。在我输入“Y”进行重复之后,它会返回显示我为上一句输入的元音和单词数 这是我的密码: #include "stdafx.h" #include <iostream> #include <iomanip> #include <conio.h> using namespace

好了,伙计们,我是编程新手,需要一些帮助。我有一个程序,它接收输入的句子,并显示单词和元音的数量。如果用户愿意,我想重复这个程序,但是当我使用do-while循环时,in陷入了一个无限循环。在我输入“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;
}
#包括“stdafx.h”
#包括
#包括
#包括
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
半句[50];
int countA=0,countE=0,countI=0,countO=0,countU=0,countSP=0;
字符重复;
做{
cout表达式
(repeat='y'&&repeat='y')
将始终等于false,因为
repeat
不能同时等于
'y'
'y'

你的意思可能是:

(repeat == 'y' || repeat == 'Y');
表达式
(repeat=='y'&&repeat=='y')
将始终等于false,因为
repeat
不能同时等于
'y'
'y'

你的意思可能是:

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

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

while (repeat == 'y' || repeat == 'Y')
   {
      _getche();
   }
因为循环没有主体,它不知道执行什么

为什么不这样做

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 = ' ';
}
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
边做边做(重复='y'|重复=='y'){
输入()
不能重复;
}
}
返回0;
输入(){
半句[50];
int countA=0,countE=0,countI=0,countO=0,countU=0,countSP=0;
char repeat=Y;
无法将
(repeat='y'&&repeat='y');
更改为
(repeat='y'| | repeat='y');

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

while (repeat == 'y' || repeat == 'Y')
   {
      _getche();
   }
因为循环没有主体,它不知道执行什么

为什么不这样做

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 = ' ';
}
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
边做边做(重复='y'|重复=='y'){
输入()
不能重复;
}
}
返回0;
输入(){
半句[50];
int countA=0,countE=0,countI=0,countO=0,countU=0,countSP=0;
char repeat=Y;

请尝试用
repeat='y'| | repeat='y')
替换
repeat='y'
,因为代码中的条件永远不会为真。

尝试用
repeat='y'&repeat='y'
替换
repeat='y'
,因为代码中的条件永远不会为真。

需要在循环开始时将
repeat
设置为
Y
Y
(例如:
repeat=NULL;

您需要在循环开始时将
repeat
设置为
Y
Y
(例如:
repeat=NULL;

要记住的主要事情是C++进行所谓的短路评估。如果&&条件的一侧为假,则所有内容都为假。例如

int y = 1;
int x = 2;
if (y == 0 && x ==2) {
....
}
它只是检查第一部分,因为y=1,它将返回一个假布尔值,这个if语句将永远不会执行

与wise、with或、| |类似,如果条件的一侧为真,则该条件将返回真布尔值,然后将执行该条件

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

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

这样,如果第一方为真,则条件将得到满足并执行。

要记住的主要事情是C++执行所谓的短路评估。如果&&条件的一侧为假,则所有内容都为假。例如

int y = 1;
int x = 2;
if (y == 0 && x ==2) {
....
}
它只是检查第一部分,因为y=1,它将返回一个假布尔值,这个if语句将永远不会执行

与wise、with或、| |类似,如果条件的一侧为真,则该条件将返回真布尔值,然后将执行该条件

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

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

这样,如果第一方为真,那么条件将得到满足并执行。

(repeat='y'&&repeat='y');
将始终为假。它怎么能同时等于这两个呢?我想你的意思是
(repeat='y'| repeat='y');
查看我更新的答案
(repeat='y'&&repeat='y>)
总是错误的。它怎么能同时等于这两个呢?我想你的意思是
(repeat==“y”| repeat==“y”)
查看我的更新答案抱歉,是的,这应该是“或”,我在乱搞代码时更改了它,但在粘贴到该代码之前忘记了将其更改回去。这是因为我已经“或”了,它卡在了循环中。抱歉,是的,这应该是“或”,我在乱搞代码时更改了它,忘了更改在我把它粘贴到这个之前,我把它改回去了。当我有“或”的时候,它就卡在循环中了。对不起,是的,那应该是“或”,我在乱搞代码的时候改了,在我把它粘贴到这个之前忘了把它改回去。当我有“或”的时候,它就卡在循环中了。但是这是一个do-while循环?代码到b在do之后执行e?条件在while之后,如果repeat等于'Y'或'Y'。不?对不起,是的,这应该是'or',我在处理代码时更改了它,但在粘贴到该代码之前忘记了将其更改回去。当我有'or'时,它会卡在循环中。但这是一个do-while循环?要执行的代码在do之后?条件在while之后,如果repeat等于'Y'或'Y'。不?对不起,是的,应该是'or',我在处理代码时更改了它,但在粘贴到该代码之前忘记了将其更改回去。当我有'or'时,它会卡在循环中。当你y“无限厕所