gpt4 book ai didi

C++陷入死循环

转载 作者:太空宇宙 更新时间:2023-11-04 15:43:52 26 4
gpt4 key购买 nike

好的伙计们,我是编程新手,需要一些帮助。我有一个程序可以输入一个句子并显示单词和元音的数量。然后,如果用户需要,我想重复该程序,但是当我使用 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;
}

最佳答案

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

你的意思可能是:

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

关于C++陷入死循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19773033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com