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,375
    Likes Received:
    388
    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
     
  5. sakshijn

    sakshijn New Member

    Joined:
    Jun 25, 2022
    Messages:
    2
    Likes Received:
    1
    Trophy Points:
    3
    Gender:
    Female
    simply use
    cin>> s;
    no need to use getline if you are taking string as a datatype
    and take care that C++ is case sensitive so in input, it should be Hello World only
     
  6. JayAdams

    JayAdams New Member

    Joined:
    Dec 22, 2021
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Home Page:
    https://top10seosoftware.com/alternatives/webceo/
    Got similar problems, thanks for information, hope it will help me
     

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