locked
What's wrong with my code? RRS feed

  • Question

  • //test program 0.1
    
    #include <iostream>
    #include <string>
    
    int main( void )
    {
      using std::cout;
      using std:: cin;
      using std::string;
    
      int menuNum;
      String name;
     
      cout << "Hello, what is your name?";
      cin >> name;
    
      //displays the users name
    
      cout << "hello, " << name.c_str();
    return 0;
    }

    I get the error:1>c:\users\admin\documents\visual studio 2008\projects\testpro1\testpro1\file1.cpp(20) : error C2228: left of '.c_str' must have class/struct/union
    1>        type is ''unknown-type''
    1>Build log was saved at "file://c:\Users\Admin\Documents\Visual Studio 2008\Projects\testPRO1\testPRO1\Debug\BuildLog.htm"

     

    What did I do wrong?

    Tuesday, June 8, 2010 8:43 PM

Answers

  • String should be string, with a lower case s. And there's no need for that c_str(), you can send a string object directly to cout.

     

    • Proposed as answer by ildjarn Tuesday, June 8, 2010 9:02 PM
    • Marked as answer by Nancy Shao Monday, June 14, 2010 8:47 AM
    Tuesday, June 8, 2010 9:01 PM
  • xen0r wrote:
    > //test program 0.1
    >
    > #include <iostream>
    > #include <string>
    >
    > int main( void )
    > {
    > using std::cout;
    > using std:: cin;
    > using std::string;
    >
    > int menuNum;
    > String name;
     
    string name;
     
    Lowercase 's'.
    --
    Igor Tandetnik
     
     
    • Marked as answer by Nancy Shao Monday, June 14, 2010 8:47 AM
    Tuesday, June 8, 2010 9:02 PM

All replies

  • String should be string, with a lower case s. And there's no need for that c_str(), you can send a string object directly to cout.

     

    • Proposed as answer by ildjarn Tuesday, June 8, 2010 9:02 PM
    • Marked as answer by Nancy Shao Monday, June 14, 2010 8:47 AM
    Tuesday, June 8, 2010 9:01 PM
  • xen0r wrote:
    > //test program 0.1
    >
    > #include <iostream>
    > #include <string>
    >
    > int main( void )
    > {
    > using std::cout;
    > using std:: cin;
    > using std::string;
    >
    > int menuNum;
    > String name;
     
    string name;
     
    Lowercase 's'.
    --
    Igor Tandetnik
     
     
    • Marked as answer by Nancy Shao Monday, June 14, 2010 8:47 AM
    Tuesday, June 8, 2010 9:02 PM
  • S tring should be s tring, with a lower case s. And there's no need for that c_str(), you can send a string object directly to cout.

     

    Thanks :D
    Tuesday, June 8, 2010 9:06 PM