简体   繁体   中英

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:

 #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

Your logic is ok but you forgot to read the variable Entscheidung2 ;

The problems was that you did declare the variable Entscheidung2 but was never initialized/read, therefore the condition

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

was never executed...

QuickFix:

add the 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; you must not attempt to read its value, yet that's what you do in the switch statement.

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:

    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

No. "Code::Blocks" is not a compiler.

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.

First of all you don't even need the if functions. Using case for comparison is enough. Otherwise you would be comparing the variable with 1 and 2 twice, which makes no sense. Then you should also get the input from console with cin whuch which is already mentioned in the answer below.

 #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;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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