Verschachtelte if-else-Anweisungen nur die Ausführung einer Bedingung (C++)

Ich bin sehr neu zu codieren und in einer ungeraden Straßensperre mit einer if-else-Programm. Es gibt eine Menge Kommentare in meinem code zu erklären, was Los ist, so werde ich nur nach dem code, aber das grundlegende problem ist, dass wenn ich versuche zu vereinfachen, werden die geschachtelten Anweisungen, bekomme ich nur eine Bedingung ausgeführt (überschreibt alle anderen Zustände). Zum Beispiel, wenn ich will, dass etwas passiert, wenn "x = 4", nur dass diese Bedingung ausgeführt wird-auch wenn es falsch ist! Wenn ich aber den Zustand, den er als "x < 5 && x > 3", dann funktioniert das Programm einwandfrei (entsprechende Ausführungen für die true-und false-Bedingungen), aber das scheint chaotisch-vor allem, da ich möchte, dass bestimmte Antworten für mehrere Eingänge.

Ich suchte die Website und nicht finden konnte, die dieses spezielle Problem, und die "Ähnliche Fragen" scheinen nicht zu bewerben. So eine Ahnung, was Los ist??? Ist es etwas, das Sie können das Programm ausführen, nur eine if-else-Anweisung und ignorieren alle anderen, auch wenn diese Aussage falsch ist?

Code: Das problem ist mit den beiden elseif-Anweisungen in der Nähe des Ende (gekennzeichnet mit Kommentaren).

#include <iostream>

using namespace std;

int main()
{//This a program using loop functions to have the user
 //guess variable "guess" (which we'll make "5"), until the user gets it
 //right, and provides hints to help the user out
    cout<<"Can you guess the number I'm thinking? It's between 1 and 10.\n";
    //Prompts the user to input a guess
    int guess;
    //Declares the variable that the user will input
    cin>>guess;
    //Before the loop begins, the user input a guess
    cout<<"\n";
    //This gives us a line break after the answer is submitted
    while (guess != 5){//Guessing incorrectly starts the loop
                       //The program is going to keep asking the user to guess
                       //until the user guesses "5"
        if (guess < 1 ){//The program will warn the user if they're out of range, here it's too low
                cout<<"No, that's too low! Guess a number between 1 and 10.\n";
                cin>>guess; //Allow the user to guess again
                cout<<"\n"; //Add a line break after the input
    }
            else //Now, give responses for other conditions
                if(guess > 10){//Warn the user if they guess too high
                cout<<"Too high! Guess a number between 1 and 10.\n";
                cin>>guess;
                cout<<"\n";
            }
            else //Tell the user if they're getting close.
                 //Not sure why I can't simply say if "guess = 4,6"
                 //Doing so causes only this response to execute, ignoring
                 //the others, except the above ">" and "<" statements
                 //which is why I'm continung to use </> statements here
                if(guess > 5 && guess < 7 || guess < 5 && guess > 3){
                cout<<"VERY close! Keep trying.\n";
                cin>>guess;
                cout<<"\n";
            }
            else //Tell the user if they're sort of close
                 //Again, not sure why I can't just say "guess=3,7"
                if(guess > 2 && guess < 4 || guess > 6 && guess < 8){
                cout<<"Close, but no cigar. Try again.\n";
                cin>>guess;
                cout<<"\n";
            }
            else {//For all other responses, we output this
                cout<<"Guess again.\n";
                cin>>guess;
                cout<<endl; //We need to end the loop here, as these are all the conditions
                //This kept creating a line break. My assumption is that
                //by virtue of ending the loop, a line break was created
            }
    }
    if(guess = 5){//Outside the loop, we need to specify the correct answer
                  //because the loop is only working while the answer in incorrect
        cout<<"Good job. "<<guess<<" is right!\n";
    }
    cin.get();//This keeps the program from closing after completion
}
//Done
  • Schauen Sie den Unterschied zwischen = und ==.
  • Drehen Sie den level Warnung und dein compiler sollte etwas zu sagen haben, die wirklich hilft.
  • "wollen, dass etwas geschieht, wenn "x = 4", nur dass diese Bedingung wird ausgeführt,"... x = 4 weist den Wert 4 in x überschreiben alle vorherigen Wert, und überprüft dann, ob das true im booleschen Sinne - was immer es ist, wie nicht-null-zahlen sind als true. Ihr Zustand sollte x == 4.
  • Laut einem Kommentar auf eine der Antworten, das problem war nur, dass die Zuordnung wurde anstelle des Vergleichs. Die Abstimmung zu schließen, als zu lokalisierten.
InformationsquelleAutor D. Lowe | 2013-05-30
Schreibe einen Kommentar