Thread: trying to write a code where I can enter a name and then it says hi, your_name?

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    46

    trying to write a code where I can enter a name and then it says hi, your_name?

    Here is the code and codeblocks is saying cout was not declared in this scope. What does that mean?

    Code:
    #include <iostream>int
    main()
    {
    
    
    
    
    cout<<"Please enter your name:\n";
    string first_name;
    cin>> first_name;
    cout<<"Hello"<<first_name<<"!\n";
    
    
    return 0;
    
    
    
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,857
    cout, cin, and string belong to the std namespace, so instead of just writing cout, cin, or string, you're supposed to write std::cout, std::cin, and std::string e.g.,
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::cout << "Please enter your name:\n";
        std::string first_name;
        std::cin >> first_name;
        std::cout << "Hello" << first_name << "!\n";
        return 0;
    }
    Notice that I placed the #include <string> for std::string. I also indented the code properly and removed blank lines that do not contribute to readability.

    Because all three names are from the std namespace, you could also use a using directive within a local scope to bring them into scope and hence be able to use them without qualifying them with the namespace name, e.g.,
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
        cout << "Please enter your name:\n";
        string first_name;
        cin >> first_name;
        cout << "Hello" << first_name << "!\n";
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2019
    Posts
    46
    Quote Originally Posted by laserlight View Post
    cout, cin, and string belong to the std namespace, so instead of just writing cout, cin, or string, you're supposed to write std::cout, std::cin, and std::string e.g.,
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::cout << "Please enter your name:\n";
        std::string first_name;
        std::cin >> first_name;
        std::cout << "Hello" << first_name << "!\n";
        return 0;
    }
    Notice that I placed the #include <string> for std::string. I also indented the code properly and removed blank lines that do not contribute to readability.

    Because all three names are from the std namespace, you could also use a using directive within a local scope to bring them into scope and hence be able to use them without qualifying them with the namespace name, e.g.,
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        using namespace std;
        cout << "Please enter your name:\n";
        string first_name;
        cin >> first_name;
        cout << "Hello" << first_name << "!\n";
        return 0;
    }
    Got a question for you. Deos that mean you only need to enter namespace std; once?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,857
    Quote Originally Posted by solidusMGS
    Deos that mean you only need to enter namespace std; once?
    Yes, within the scope in which it was used.

    You can use it at file scope, but unless there are only a few functions, it usually is not a good idea because it means that all the names from all the standard headers included become injected into the global namespace throughout the file after the using directive.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enter and write out words into a tree of nodes
    By Joe1903 in forum C++ Programming
    Replies: 49
    Last Post: 12-02-2016, 06:04 PM
  2. How do I write to a file without hitting enter? (windows)
    By BinaryProgamer in forum C Programming
    Replies: 9
    Last Post: 09-26-2016, 12:37 PM
  3. what code to enter for subset function?
    By ismee in forum C Programming
    Replies: 8
    Last Post: 12-04-2008, 11:19 AM
  4. If( false == true ) ... C++ enter in the code
    By bibiteinfo in forum C++ Programming
    Replies: 7
    Last Post: 02-07-2006, 01:54 PM
  5. Replies: 4
    Last Post: 03-22-2002, 05:30 PM

Tags for this Thread