-2

Even you answer is correct, it will still indicate it is incorrect .

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main() {
    string play;

    cout << "Do you want to play? ";
    cin >> play;

    if (play == "yes") {
        cout << "Ok, Let's Get Started! \n";
    } else {
        cout << "Let's play next time!";
    }

    string test1;

    cout << "What does CPU stand for? ";
    cin >> test1;

    if (test1 == "central processing unit") {
        cout << "Correct!\n";
    } else {
        cout << "Incorrect!\n";
    }
    return 0;
}

I try to redo/recode the if else statement but it didn't run. I'm expecting that there some expert can help me.

rturrado
  • 7,699
  • 6
  • 42
  • 62
  • 2
    Change your `cin >>` for `std::getline(cin, `. Otherwise, the second `cin >>` will only read one string, e.g. `central`: https://godbolt.org/z/aYeEqzaPW – rturrado Dec 13 '22 at 15:39
  • 1
    Reopened as the suggested dupe referred to the char array version of getline, not the string version that is probably needed here. – john Dec 13 '22 at 15:44
  • 1
    By the way, don't mix `cin >>` and `std::getline(cin, `. If you use `cin >> play;` and then `std::getline(cin, test1);`, the `std::getline` will read a whitespace or a newline that the `cin >> play;` left without reading: https://godbolt.org/z/j1EzabP77 – rturrado Dec 13 '22 at 15:46
  • A good first step when the output is mysterious is to verify that the input is what you expect instead of assuming that it is. Add `cout << test1;` before the conditional. – molbdnilo Dec 13 '22 at 15:56
  • `cin >> test1;` only reads the first word of `central processing unit` so that `if (test1 == "central processing unit") {` can never be true regardless of what the user typed. – drescherjm Dec 13 '22 at 16:17
  • FYI, execution continues if `play != "yes"`. You may want to put a `return` statement inside the `else` clause so the program ends. – Thomas Matthews Dec 13 '22 at 16:22

1 Answers1

3

This statement

cin >> test1;

reads a single word, so it can never read the three words of 'central processing unit'. If you want to read lines of text then use getline

getline(cin, play);

and

getline(cin, test1);
john
  • 85,011
  • 4
  • 57
  • 81
  • its not running #include #include #include using namespace std; int main() { string play; cout << "Do you want to play? "; getline(cin, play); if (play == "yes"){ cout << "Ok, Let's Get Started! \n"; }else{ cout << "Let's play next time!"; } getline(cin, test1); cout << "What does CPU stand for? "; cin >> test1; if (test1 == "centralprocessingunit"){ cout << "Correct!\n"; } else{ cout << "Incorrect!\n"; } return 0; } – Arvin Calinog Dec 13 '22 at 15:54
  • 1
    @ArvinCalinog Your code is still says `cin >> test1;` and `getline` has been added in the wrong place. – john Dec 13 '22 at 15:55
  • can some body show me where i will put the getline? my mind is confused rightnow – Arvin Calinog Dec 13 '22 at 16:05
  • Instead of `cin >> test1;`. You did the right thing when you replaced `cin >> play;` with `getline(cin, play);`, you did the wrong then when you added `getline(cin, test1);` before your prompt and left `cin >> test1;` in place. – Nathan Pierson Dec 13 '22 at 16:13
  • There should be a `return` statement in the first `else` clause; otherwise execution keeps going when `play != "yes"`. – Thomas Matthews Dec 13 '22 at 16:25