1

现在会发生什么(在编辑代码之后),每当我输入多个单词时,它就会出现痉挛。有什么修复吗?谢谢。对不起,如果我似乎什么都不知道。昂贵的书籍和没有人教我让我在线阅读教程。(编辑后的代码如下。)

#include <iostream>
#include <string>
using namespace std;

string qu;

int y;

int main()
{
y = 1;
while (y == 1)
{
    cout << "Welcome to the magic 8-ball application." <<"\nAsk a yes or no question, and the 8-ball will answer." << "\n";
    cin >> qu;
    cout << "\nProccessing...\nProccessing...\nProccessing...";
    cout << "The answer is...: ";
    int ans = (int)(rand() % 6) + 1;
    if (ans == 1)
        cout << "Probably not.";
    if (ans == 2)
        cout << "There's a chance.";
    if (ans == 3)
        cout << "I don't think so.";
    if (ans == 4)
        cout << "Totally!";
    if (ans == 5)
        cout << "Not a chance!";
    if (ans == 6)
        cout << "75% chance.";
    system("CLS");
    cout << "\nWant me to answer another question?" << "(1 = yes, 2 = no.)";
    cin >> y;
    }

return 0;

}
4

2 回答 2

5
 while (y = 1);

应该

 while (y == 1)

你有额外的;,应该使用==.

于 2013-06-01T19:44:00.010 回答
0

这里有无限循环:

while (y = 1);

删除;。还有y == 1,不是y = 1


为避免将来出现这些错误:

1)这种方式的反向比较:

while (1 == y)

现在,如果您键入=而不是==then 您的代码将无法编译,因为1 = y它无效。

尽管大多数编译器会在您进行条件赋值时警告您。你不应该忽略编译器警告。

2)将左大括号放在同一行:

while (1 == y) {
// ...... some code
}

现在,如果您;在行尾键入,那么您的代码仍然正确:

while (1 == y) {;
// ...... some code
}
于 2013-06-01T19:56:30.353 回答