C++ 陷入无限循环
C++ Stuck in infinite loop
好吧,伙计们,我是编程新手,需要一点帮助。我有一个程序,它接受输入的句子并显示单词和元音的数量。如果用户愿意,我想重复该程序,但是当我使用 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 << "nnWould 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');
尝试将 repeat == 'y' && repeat == 'Y'
替换为 repeat == 'y' || repeat == 'Y')
,因为代码中的条件永远不会为真。
将(repeat == 'y' && repeat == 'Y');
更改为(repeat == 'y' || repeat == 'Y');
编辑:您也没有大括号来打开或关闭您的环,请尝试此操作。
while (repeat == 'y' || repeat == 'Y')
{
_getche();
}
因为循环没有主体,它不知道要执行什么。
编辑2 为什么不这样做?
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
do while (repeat == 'y' || repeat == 'Y') {
Enter()
cout << "nnWould 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 = ' ';
}
要记住的主要事情是C++进行所谓的短路评估。如果 && 条件的一侧是假的,那么一切都是假的。举个例子,
int y = 1;
int x = 2;
if (y == 0 && x ==2) {
....
}
它只是要检查第一部分。由于 y = 1,它将返回一个假布尔值,并且此 if 语句将永远不会被执行。
像wise一样,使用or,||,如果条件的一侧为真,则条件将返回一个True Boolean,然后条件将被执行。
对于您的情况,正确的方法是:
(repeat == 'Y' || repeat == 'y');
这样,如果第一端为真,则将满足条件并执行。
您需要在
)
循环开始时将repeat
设置为Y
或y
以外的任何内容(例如:repeat = NULL;
相关文章:
- 过载'operator new'如何导致无限循环?
- C++正则表达式无限循环
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 遍历链表时的无限循环
- 循环链表:无限循环
- 比较两个字符串后卡在无限循环中
- 在做一段时间内检查字符的无限循环
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 可能的无限循环
- 超出 int 的值范围后的无限循环
- 为什么这段代码会导致无限循环?
- 运行无限循环的最小二叉树问题
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- 连接线程时的无限循环
- 为什么这种方法会进入无限循环?
- 由于QTextStream的无限循环
- Qt 在另一个线程中无限循环
- 从文件 C++ 读取时的无限循环
- 试图找出为什么我的代码保持无限循环