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,335
    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
     
  4. sarasara

    sarasara New Member

    Joined:
    Jan 19, 2021
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Female
    Location:
    tehran
    Home Page:
    https://www.mmsiz.com/
    I had same problem friends thanks for solving
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice