What's wrong with my "Hello World" source code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// testing123.cpp : Testing purposes

#include "stdafx.h"


#include <iostream>
 
	int main()
	
	{
    std::cout << "Hello world!" << std::endl;

cin.clear();
cin.ignore(255, '\n');
cin.get();

	return 0;
}


I added

1
2
3
cin.clear();
cin.ignore(255, '\n');
cin.get();


Before the return function to pause the CMD from closing. When compiling, it returns one fail.
Last edited on
What's the error it returns?
your code isn't at fault it must be your compiler
I am using VC++ 2010

This is the error message:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
1>Build started 9/19/2010 8:09:58 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\testing123.unsuccessfulbuild".
1>ClCompile:
1>  All outputs are up-to-date.
1>  testing123.cpp
1>c:\users\quickcode\desktop\test projects\testing123\testing123\testing123.cpp(13): error C2065: 'cin' : undeclared identifier
1>c:\users\quickcode\desktop\test projects\testing123\testing123\testing123.cpp(13): error C2228: left of '.clear' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\quickcode\desktop\test projects\testing123\testing123\testing123.cpp(14): error C2065: 'cin' : undeclared identifier
1>c:\users\quickcode\desktop\test projects\testing123\testing123\testing123.cpp(14): error C2228: left of '.ignore' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\quickcode\desktop\test projects\testing123\testing123\testing123.cpp(15): error C2065: 'cin' : undeclared identifier
1>c:\users\quickcode\desktop\test projects\testing123\testing123\testing123.cpp(15): error C2228: left of '.get' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\quickcode\desktop\test projects\testing123\testing123\testing123.cpp(20): error C2513: 'int' : no variable declared before '='
1>c:\users\quickcode\desktop\test projects\testing123\testing123\testing123.cpp(21): error C2065: 'x' : undeclared identifier
1>c:\users\quickcode\desktop\test projects\testing123\testing123\testing123.cpp(21): fatal error C1004: unexpected end-of-file found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.34
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

You must prefix cin with std:: or add using namespace std;
I included namespace underneath iostream and attempted to compile, this error came up.


1
2
3
4
5
6
7
8
1>c:\users\\desktop\test projects\testing123\testing123\testing123.cpp(21): error C2513: 'int' : no variable declared before '='
1>c:\users\\desktop\test projects\testing123\testing123\testing123.cpp(22): error C2065: 'x' : undeclared identifier
1>c:\users\\desktop\test projects\testing123\testing123\testing123.cpp(22): fatal error C1004: unexpected end-of-file found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.28
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Hmm, i don't get why that's happening..
Post your whole code again.
I see that you're using VC++ and that you mistakenly started a "New Console Project". This is not a good idea because it causes more issues than a "Blank Project".

Start a "Blank Project" (you may have to change which tree you are viewing on the left) and then right click the "Source Files" on the left and click "Add" -> "New Item" and add a .cpp file (you can name it anything).

Then you can start your program in there. Just this code will do:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
	cout << "Hello, World!" << endl;
	cin.get();

	return 0;
}


Screenshots: (yours may differ a little)
http://www.LB-Stuff.com/Temp/VC_NewBlankProj.png
http://www.LB-Stuff.com/Temp/VC_NewItem.png
http://www.LB-Stuff.com/Temp/VC_NewItem2.png
http://www.LB-Stuff.com/Temp/HelloWorld.png
Last edited on
Coming back to the original problem.
The original post showed only 18 lines:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// testing123.cpp : Testing purposes

#include "stdafx.h"


#include <iostream>
 
	int main()
	
	{
    std::cout << "Hello world!" << std::endl;

cin.clear();
cin.ignore(255, '\n');
cin.get();

	return 0;
}


But the build was showing errors for lines 21 and 22
1>c:\users\\desktop\test projects\testing123\testing123\testing123.cpp(21): error C2513: 'int' : no variable declared before '='
1>c:\users\\desktop\test projects\testing123\testing123\testing123.cpp(22): error C2065: 'x' : undeclared identifier
1>c:\users\\desktop\test projects\testing123\testing123\testing123.cpp(22): fatal error C1004: unexpected end-of-file found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.28
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


So to the original poster HitCode337 - What does your full source code look like?? There must be more than you posted.
Does this work? Sorry, I'm a newbie too but I figured that if you are not using namespace std then you will have to put std:: for each of the cin too.


#include <iostream>

int main()

{
std::cout << "Hello world!" << std::endl;

std::cin.clear();
std::cin.ignore(255, '\n');
std::cin.get();

return 0;
}
@L B - Thank you, your method worked.
Topic archived. No new replies allowed.