What means expected unqualified-id before string constant on C++?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Elohim
    New Member
    • Dec 2010
    • 2

    What means expected unqualified-id before string constant on C++?

    Hi to everybody, I'm a learner of C++. I'll really appreciate if you can help me or teach me something.

    Thank you in advance.

    Code:
    #include<iostream>
    #include<string>
    
    int main()
    {
        std::cout << "Introduce your first name:";
        std::string name;
        std::cin >> name;
        
        //build the message that I intend to write.
        const std::string greeting = "Hello, "+name+"!";
        
        //build the space line.
        const std::string spaces(greeting.size()." ");
        
        //build the frame of the top/down.
        const std::string frame(greeting.size()."*");
        
        //printing the result.
        std::cout << std::endl;
        std::cout << "*"+frame+"*" << std::endl;
        std::cout << "*"+spaces+"*" << std::endl;
        std::cout << "*"+greeting+"*" << std::endl;
        std::cout << "*"+spaces+"*" << std::endl;
        std::cout << "*"+frame+"*" << std::endl;
        
        return 0;
    }
    Errors
    14 C:\Users\eq1001 2\Documents\Unt itled1.cpp expected unqualified-id before string constant
    17 C:\Users\eq1001 2\Documents\Unt itled1.cpp expected unqualified-id before string constant
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    Look at your code on lines 14 and 17.

    Code:
    //build the space line. 
    const std::string spaces(greeting.size()." "); 
      
    //build the frame of the top/down. 
    const std::string frame(greeting.size()."*");
    This syntax is really wrong...

    greeting.size() returns a value of size_t

    size_t for a string I believe is an unsigned integer type. Using a ."*" has no meaning on a size_t nor any class or struct for that manner.

    The correct syntax would be...
    Code:
    //build the space line. 
    const std::string spaces(greeting.size() , " "); 
      
    //build the frame of the top/down. 
    const std::string frame(greeting.size() , "*");
    Commas instead of a period.

    Comment

    • Elohim
      New Member
      • Dec 2010
      • 2

      #3
      Thanks a lot for the answer, I don't know what happens to me with the comma ^^... The Problems was that I'm reading on a Kindle and is hard to distinguish the . with the , and the " with the '. I don't know how this part works (greeting.size( ),' ') I just get the Idea from Accelerated C++: Practical Programming by Example. But now is done.

      Thanks again.

      Fixed code:

      Code:
      #include<iostream>
      #include<string>
      
      int main()
      {
          std::cout << "Introduce your first name:";
          std::string name;
          std::cin >> name;
          
          //build the message that I intend to write.
          const std::string greeting = "Hello, "+name+"!";
          
          //build the space line.
          const std::string spaces(greeting.size(),' ');
          
          //build the frame of the top/down.
          const std::string frame(greeting.size(),'*');
          
          //printing the result.
          std::cout << std::endl;
          std::cout << "*"+frame+"*" << std::endl;
          std::cout << "*"+spaces+"*" << std::endl;
          std::cout << "*"+greeting+"*" << std::endl;
          std::cout << "*"+spaces+"*" << std::endl;
          std::cout << "*"+frame+"*" << std::endl;
          
          return 0;
      }

      Comment

      • hype261
        New Member
        • Apr 2010
        • 207

        #4
        Here is a website that I use for looking up information on the STL.



        The constructor you are using is this one...

        string ( size_t n, char c );

        which does the following...

        Content is initialized as a string formed by a repetition of character c, n times.

        Comment

        Working...