I am new to Ubuntu and its terminal. I am trying to code C++ using nano something.cpp
a general "Hello World" program at first.
First I got some error about iostream.h
and cout
not recognised. I searched the web and found that it was a newer C++ and we had to use iostream
instead of iostream.h
and std::cout
instead of cout
. It worked well after making those changes (compiled using gcc).
Now I tried creating the second program. Here I had to input my name and it would show "Hello Myname".
The code was:
#include <string>
#include <iostream>
int main()
{
string g;
std::cout << "What is your name?\n";
std::cin >> g;
std::cout << "Hello\n" << g << std::endl;
return 0;
}
When I tried to compile (using gcc myprog.cpp -o hello
); I got the following errors:
prog.cpp: In function ‘int main()’:
prog.cpp:5:1: error: ‘string’ was not declared in this scope
prog.cpp:5:1: note: suggested alternative:
/usr/include/c++/4.6/bits/stringfwd.h:65:33: note: ‘std::string’
prog.cpp:5:8: error: expected ‘;’ before ‘g’
prog.cpp:7:13: error: ‘g’ was not declared in this scope
I searched the web but did not find any help. How do I get rid of these errors?
I would also be happy if somebody would suggest me a book on this "NEWER" C++.