2

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++.

1
  • instead gcc use g++. It is not a newer C++ it is a C++ language, that you mistaken with C. C++ is compatibilie with C but it doesn't work in oposite direction. I would also suggest using Code:Blocks to begin C++ adventure in Ubuntu.
    – Misery
    May 26, 2013 at 8:44

1 Answer 1

3

First of all use g++ to compile, second you forgot the namespace qualifier for the string g. Try std::string g;. And compile it with g++ myprog.cpp -o hello.

2
  • Thank You very much....But the name which i am giving is coming in the next line after "Hello". Is there any way i can put it in same line??
    – Anubhab
    May 29, 2013 at 6:43
  • Remove the \n character. Since \n stands for new line.
    – ortang
    May 29, 2013 at 8:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .