Getting input from a string?

Hello, I am working on a problem where I need to get user input for a string. I have below posted an example as to understand why I keep getting an error that says string is not declared in this scope and at times that it was not initialized.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
String str1;
String str2;

std::cout << "What would you like to type for str1?";
std::cin >> str1;

std::cout << "What would you like to type for str2?";
std::cin >> str2;

std::cout << str1 + str2;


}


I don't understand why I can get user input for integers or doubles but not allow them to enter a string, thanks for any input
lines 5 and 6 std::string
Thanks! Do you know why this requires a std::string but other variable types like a double or integer wouldnt?
std::strings as a type are a specialized class, hence the #include <string> whereas int etc are 'built in' fundamental data types

http://www.cplusplus.com/reference/string/
http://www.cplusplus.com/doc/tutorial/variables/
double and integer are about 99% directly convertible to CPU registers. There isn't a block of code in the compiler libraries to add integers or multiply doubles; those things are done on a circuit unless you are on some exotic platform. The exotic platform compiler would have to hide this issue to be compatible with standard c++. For example, in 1990 on a 286 you had a PC with no floating point support, but even then, c++ did not need you to #include<double> (no such thing); it allowed you to use doubles normally and the software implementation of the data type was silently added by the compiler. There are still devices without floating point support, and it still works that way.

string, vector, etc DO have library code behind them, and that is what you are pulling in. It could be done the other way (c++ compiler could silently give us string without having an include statement, and the linker can be smart enough to not add unused library code to the final product, all this very doable) but for whatever reasons the c++ standards committee has not felt the urge to do this for us. I personally would love to see the STL no longer require include files, but its a lot of work for compiler writers and tool support, for very minor aggravation to the coder. The cost benefit isnt there to justify the change now.

depending on how you look at it, then, the reason is 'because the standards committee said so' which is a poor answer, but its tied to 'typical use of the language' decisions, historical reasons, and so on.
Last edited on
Topic archived. No new replies allowed.