geany not working for basic c++ program

Asked by Corey Daley

Hi, I was trying to do some simple programs (learning) and I attempted to create, compile, and run this following program:

#include <iostream>
using namespace std;

int main ();
{
 cout ( "Hello World!" );
 getchar();
 return 0;
}
What I got when I attempted to compile it was this:
g++ -Wall -c "first.cpp" (in directory: /home/corey/Desktop)
first.cpp: In function 'int main()':
first.cpp:6:24: error: no match for call to ' (std::ostream) (const char [13])
first.cpp:7:10: error: 'getchar' was not declared in this scope
Compilation failed.

If anyone can help me that would be great!

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu geany Edit question
Assignee:
No assignee Edit question
Solved by:
Corey Daley
Solved:
Last query:
Last reply:
Revision history for this message
mycae (mycae) said :
#1

Your code is wrong -- gcc is telling you which lines are at fault.

the line

cout( "Hello World!");

is calling a constructor for the cout object... This is not valid use of the C++ standard library.

you want

cout << "Hello World!";

or maybe with a newline:

cout << "Hello World!" << endl;

Note that "<<" is the streaming operator in C++.

The last error is because getchar() is not provided by C++, but rather is part of the C library, and is contained in the header "cstdio", which you need to #include.

http://www.cplusplus.com/reference/clibrary/cstdio/getchar/

Revision history for this message
Corey Daley (bartor495) said :
#2

So how do i get the terminal to not blink and not even give me the opportunity to see the message if I can't use getchar?

Revision history for this message
Corey Daley (bartor495) said :
#3

I tried what you said, and here is the code I typed:

#include <cstdio>
using namespace std;

int main ();
{
 cout << "Hello World!";
 getchar();
 return 0;
}

I got another error message that said:

g++ -Wall -c "first.cpp" (in directory: /home/corey/Desktop)
first.cpp:5:1: error: expected unqualified-id before '{' token
Compilation failed.

What do I need to do?

Revision history for this message
Corey Daley (bartor495) said :
#4

Ok, I've done some research, and I've solved it by modifying it a little bit, here's the code:

#include <cstdio>
using namespace std;

int main()
{
 printf ("Hello World!");
 getchar();
 return 0;
}

I also don't get why the other thing worked on dev bloodshed, but not on gimp. Anyway, thanks for your help.

Revision history for this message
Corey Daley (bartor495) said :
#5

Solved on 9/21/11 on 9:07 A.M. Mountain Time.