Cout of a string is giving an error and a hard time some insight help pls?

前端 未结 3 561
南笙
南笙 2021-01-27 14:36

I cant find the error in this piece of code could anyone please insight me? I ran the debugging but the errors are un-understandable..

#include \"stdafx.h\"
#in         


        
3条回答
  •  旧巷少年郎
    2021-01-27 15:22

    Problems:

    1. You have some missing #includes, which probably caused your initial compiler errors.
    2. You have a simple syntax error with your if statement.
    3. Using the stream extraction operator will never yield a string with whitespace inside of it.

    The following should work as you expect:

    #include "stdafx.h"
    #include 
    #include 
    #include 
    
    using namespace std;
    
    int main()
    {
        cout << "Input your name please?" << endl;
    
        string name;
        getline(cin, name);
        if (name == "Bart Simpson")
        {
            cout << "You have been very naughty" << endl;
        }
    
        return 0;
    }
    

    (You need to include string for std::string and std::getline, and ostream for std::endl.)

提交回复
热议问题