By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,881 Members | 2,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,881 IT Pros & Developers. It's quick & easy.

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

P: 2
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.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3.  
  4. int main()
  5. {
  6.     std::cout << "Introduce your first name:";
  7.     std::string name;
  8.     std::cin >> name;
  9.  
  10.     //build the message that I intend to write.
  11.     const std::string greeting = "Hello, "+name+"!";
  12.  
  13.     //build the space line.
  14.     const std::string spaces(greeting.size()." ");
  15.  
  16.     //build the frame of the top/down.
  17.     const std::string frame(greeting.size()."*");
  18.  
  19.     //printing the result.
  20.     std::cout << std::endl;
  21.     std::cout << "*"+frame+"*" << std::endl;
  22.     std::cout << "*"+spaces+"*" << std::endl;
  23.     std::cout << "*"+greeting+"*" << std::endl;
  24.     std::cout << "*"+spaces+"*" << std::endl;
  25.     std::cout << "*"+frame+"*" << std::endl;
  26.  
  27.     return 0;
  28. }
Errors
14 C:\Users\eq10012\Documents\Untitled1.cpp expected unqualified-id before string constant
17 C:\Users\eq10012\Documents\Untitled1.cpp expected unqualified-id before string constant
Dec 16 '10 #1

✓ answered by hype261

Look at your code on lines 14 and 17.

Expand|Select|Wrap|Line Numbers
  1. //build the space line. 
  2. const std::string spaces(greeting.size()." "); 
  3.  
  4. //build the frame of the top/down. 
  5. const std::string frame(greeting.size()."*");
  6.  
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...
Expand|Select|Wrap|Line Numbers
  1. //build the space line. 
  2. const std::string spaces(greeting.size() , " "); 
  3.  
  4. //build the frame of the top/down. 
  5. const std::string frame(greeting.size() , "*");
  6.  
  7.  
Commas instead of a period.

Share this Question
Share on Google+
3 Replies


100+
P: 207
Look at your code on lines 14 and 17.

Expand|Select|Wrap|Line Numbers
  1. //build the space line. 
  2. const std::string spaces(greeting.size()." "); 
  3.  
  4. //build the frame of the top/down. 
  5. const std::string frame(greeting.size()."*");
  6.  
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...
Expand|Select|Wrap|Line Numbers
  1. //build the space line. 
  2. const std::string spaces(greeting.size() , " "); 
  3.  
  4. //build the frame of the top/down. 
  5. const std::string frame(greeting.size() , "*");
  6.  
  7.  
Commas instead of a period.
Dec 16 '10 #2

P: 2
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:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<string>
  3.  
  4. int main()
  5. {
  6.     std::cout << "Introduce your first name:";
  7.     std::string name;
  8.     std::cin >> name;
  9.  
  10.     //build the message that I intend to write.
  11.     const std::string greeting = "Hello, "+name+"!";
  12.  
  13.     //build the space line.
  14.     const std::string spaces(greeting.size(),' ');
  15.  
  16.     //build the frame of the top/down.
  17.     const std::string frame(greeting.size(),'*');
  18.  
  19.     //printing the result.
  20.     std::cout << std::endl;
  21.     std::cout << "*"+frame+"*" << std::endl;
  22.     std::cout << "*"+spaces+"*" << std::endl;
  23.     std::cout << "*"+greeting+"*" << std::endl;
  24.     std::cout << "*"+spaces+"*" << std::endl;
  25.     std::cout << "*"+frame+"*" << std::endl;
  26.  
  27.     return 0;
  28. }
Dec 17 '10 #3

100+
P: 207
Here is a website that I use for looking up information on the STL.

http://www.cplusplus.com/reference/s...string/string/

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.
Dec 18 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.