1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Beginner Problems

Discussion in 'C++' started by Summer, Nov 14, 2020.

Tags:
  1. Summer

    Summer New Member

    Joined:
    Nov 1, 2020
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    HI I have a question to ask. I wrote this program to check if a user imput "Hello World" correctly, but when I tested it out, correct input is also marked as wrong. Can someone help me with this?

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
        string s;
        cin >>\ s;
        getline (cin, s);
        if (s == "Hello World"){
            cout << "OK";
        }
        else {
            cout << "Wrong Answer";
        }
     
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,334
    Likes Received:
    377
    Trophy Points:
    83
    You don't need these two lines

    cin >>\ s;
    getline (cin, s);

    cin is enough but it should be cin >> s;
     
  3. Summer

    Summer New Member

    Joined:
    Nov 1, 2020
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    I tried that, but the program still doesn't run correctly. When I enter Hello World, it outputs wrong answer
     

Share This Page