简体   繁体   English

C++“按任意键继续”问题

[英]C++ "Press any key to continue" Issue

my question is about a little C++ program I wrote (kind of similar to Zork).I'm quite new to coding,so don't blame me if my question doesn't make any sense.Here's the code:我的问题是关于我写的一个小 C++ 程序(有点类似于 Zork)。我对编码很陌生,所以如果我的问题没有任何意义,请不要怪我。这是代码:

 #include <iostream>
using namespace std;
int main()
{
    cout << "Du befindest dich in einem dunklem Wald,um dich herum ist es dunkel.\n Vor dir liegt eine Fakel.\n Gebe 1 und Enter ein um die Fakel an dich zu nehmen \n oder 2 und Enter um sie in Ruhe zu lassen \n" ;
    int Entscheidung;
    cin>> Entscheidung;
    switch (Entscheidung) {
    case 1:
    cout <<"Du zündest die Fakel an,deine Umgeben ist nun sichtbar \n";
    break;
    case 2:
        cout <<"Du lässt die Fakel liegen,um dich herum bleibt es Dunkel \n";
        break;
        }
        if (Entscheidung==1) {
            cout << "Vor dir steht ein Elf,er scheint friedlich \n Gebe 1 ein um ihn anzusprechen oder 2 um ihn Anzugreifen \n";
        }
        if(Entscheidung==2){
            cout<< "Du hörst Schritte im dunkeln. \n Gebe 1 ein um die Fakel zu nehmen und den Geräuschen auf den Grund zu gehen \n oder 2 um dich von den Geräuschen weg zu bewegen \n";
            }
        int Entscheidung2;
        switch (Entscheidung2) {
        case 1:
        cout <<"Du redest mit dem Elfen.Er versucht dich anzugreifen \n";
        break;
         }
    return 0;
}

My problem is,that if i run the program,i can only put in one variable and then Press any key to continue appears,and from then,I can't put anything new in because it just closes.Hope someone can help me.我的问题是,如果我运行程序,我只能输入一个变量,然后按任意键继续出现,从那时起,我无法输入任何新内容,因为它刚刚关闭。希望有人可以帮助我。 Thx in advance提前谢谢

PS:I use Code::Blocks as an IDE AND Compiler PS:我使用 Code::Blocks 作为 IDE 和编译器

Your logic is ok but you forgot to read the variable Entscheidung2 ;您的逻辑没问题,但您忘记读取变量Entscheidung2

The problems was that you did declare the variable Entscheidung2 but was never initialized/read, therefore the condition问题是您确实声明了变量Entscheidung2但从未初始化/读取,因此条件

switch (Entscheidung2) {
        case 1:
            cout <<"Du redest mit dem Elfen.Er versucht dich anzugreifen \n";
            break;
     }

was never executed...从未被处决...

QuickFix:快速解决:

add the cin>> Entscheidung2;添加cin>> Entscheidung2; after the declaration of the variable for example.例如,在变量声明之后。

and everything will work:一切都会好起来的:


 int Entscheidung2; switch (Entscheidung2) {

This is undefined behaviour .这是未定义的行为 Entscheidung2 is neither initialised nor set; Entscheidung2既未初始化也未设置; you must not attempt to read its value, yet that's what you do in the switch statement.你不能试图读取它的值,但这就是你在switch语句中所做的。

With a sufficiently high warning level, you can make your compiler detect such errors.使用足够高的警告级别,您可以让编译器检测到此类错误。 Examples of such warnings:此类警告的示例:

warning: 'Entscheidung2' is used uninitialized in this function

warning C4700: uninitialized local variable 'Entscheidung2' used

Apparently, the intention was to read a value into Entscheidung2 before using it:显然,目的是在使用它之前将一个值读入Entscheidung2

    int Entscheidung2;
    cin >> Entscheidung2;
    switch (Entscheidung2) {

This removes the undefined behaviour and matches the supposed intention.这将删除未定义的行为并匹配假设的意图。

Press any key to continue appears出现按任意键继续

Which has nothing to do with your program.这与您的程序无关。 It's a message from the environment which runs your program.这是来自运行程序的环境的消息。

I use Code::Blocks as an IDE AND Compiler我使用 Code::Blocks 作为 IDE 和编译器

No. "Code::Blocks" is not a compiler.不。“Code::Blocks”不是编译器。

And please don't use non-English identifiers.并且请不要使用非英语标识符。 To anyone not speaking German, "Entscheidung" reads like a completely randomised series of characters, and it's not friendly to post code with indecipherable identifiers.对于不会说德语的人来说,“Entscheidung”读起来就像是一个完全随机的字符系列,发布带有无法辨认的标识符的代码是不友好的。

First of all you don't even need the if functions.首先,您甚至不需要 if 函数。 Using case for comparison is enough.使用 case 进行比较就足够了。 Otherwise you would be comparing the variable with 1 and 2 twice, which makes no sense.否则,您会将变量与 1 和 2 进行两次比较,这是没有意义的。 Then you should also get the input from console with cin whuch which is already mentioned in the answer below.然后,您还应该使用cin从控制台获取输入,这在下面的答案中已经提到。

 #include <iostream>
using namespace std;
 int main()
{
    cout << "Du befindest dich in einem dunklem Wald,um dich herum ises dunkel.\n Vor dir liegt eine Fakel.\n Gebe 1 und Enter ein um die Fakel an dich zu nehmen \n oder 2 und Enter um sie in Ruhe zu lassen \n" ;
int Entscheidung;
cin>> Entscheidung;
switch (Entscheidung) {
case 1:
cout <<"Du zündest die Fakel an,deine Umgeben ist nun sichtbar \n";
cout << "Vor dir steht ein Elf,er scheint friedlich \n Gebe 1 ein um ihn anzusprechen oder 2 um ihn Anzugreifen \n";

break;
case 2:
    cout <<"Du lässt die Fakel liegen,um dich herum bleibt es Dunkel \n";
   cout<< "Du hörst Schritte im dunkeln. \n Gebe 1 ein um die Fakel zu nehmen und den Geräuschen auf den Grund zu gehen \n oder 2 um dich von den Geräuschen weg zu bewegen \n";
    break;
    }

    int Entscheidung2;
    cin>>Entscheidung2;
    switch (Entscheidung2) {
    case 1:
    cout <<"Du redest mit dem Elfen.Er versucht dich anzugreifen \n";
    break;
     }
return 0;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM