Hey guys, I just typed out a program and was having a bit of a problem. This is a simple program that asks for a name and then types "hello, " plus the name..
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <string>
int main()
{
string name;
std::cout << "What is your name? " << std::endl;
std::cin >> name;
std::cout << "Hello " << name << ", welcome to my program!" << std::endl;
system("pause");
return 0;
}
When I try to compile this, I get errors such as "'string' was not declared in this scope. expected ';' before 'name'. 'name' was not declared in this scope."
The weird thing is, when I change the code to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string name;
cout << "What is your name? " << endl;
cin >> name;
cout << "Hello " << name << ", welcome to my program!" << endl;
system("pause");
return 0;
}
everything seems to work, except for one error, "warning: no newline at end of file."
In my understanding, both codes are equivalent, and my teacher actually encourages us not to do the "using namespace std" part of it. Am I doing something wrong in the first example? Thanks!
When I try to compile this, I get errors such as "'string' was not declared in this scope. expected ';' before 'name'. 'name' was not declared in this scope."
But I have another question now. What does the '#include <string>" do then? Doesn't that include statement allow me to use strings as references(I'm not even sure if "reference" is the correct word to use here)? Why does it have to call from the standard library? I'm sure I'm missing a fundamental part of C++ programming here, so any further help would be greatly appreciated..