在c++中调试程序时,我得到一个空白屏幕

I get a blank screen when debugging a program in c++

本文关键字:一个 屏幕 空白 c++ 调试程序      更新时间:2023-10-16

现在发生的是(在编辑代码之后)每当我输入多个单词时,它就会痉挛。修复吗?谢谢。抱歉我看起来好像什么都不知道。昂贵的书,没有人教我,让我在网上阅读教程。(编辑后的代码如下)

#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;
}
 while (y = 1);
应该

 while (y == 1)

您有多余的;,应该使用==

这里有一个无限循环:

while (y = 1);

删除;。也是y == 1,不是y = 1


为了避免以后出现这些错误:

1)反向比较:

while (1 == y)

现在如果你输入=而不是==,那么你的代码不会编译,因为1 = y无效。

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

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

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

现在,如果你在行尾输入;,那么你的代码仍然是正确的:

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