C++ programming in VS2008

Pantlegz

Diamond Member
Jun 6, 2007
4,627
4
81
I'm very new to programming, never done any real programming some scripting and stuff for games but nothing beyond that. I've been going through the reference links for C++ and I'm noticing most of them don't agree with VS :( I guess I could VM ubuntu but I was wondering where I could find a tutorial for programming C++ or C#(asp.net is the same thing?) in visual studio 2008 specifically.
 

tatteredpotato

Diamond Member
Jul 23, 2006
3,934
0
76
For C# I would recommend "Head First C#" it's a great beginners book to programming.

There are 2 schools of thought:
1) Start low (C++) and get a good foundation on low level stuff (memory management)
2) Start with something easier to digest (C#/Java) and then work towards something harder (C++).

I'd recommend starting with C#, it has similar enough syntax making a move to C++ wouldn't be too hard.
 
Oct 27, 2007
17,009
5
0
I agree with ObscureCaucasian, I'd start with C#. Having said that, both ways are fine.

I don't really understand the problem in the OP:
"I've been going through the reference links for C++ and I'm noticing most of them don't agree with VS"
What are the "reference links"? What do you mean "agree with VS"?

Programming C++ in Visual Studio is fairly straight forward. Create a new project, select a Win32 Console Project. In the popup dialog choose "Empty Project" in the Application Settings. This will set you up for learning 99% of what is in any introductory book or tutorial - a nice clean project to begin with. The compiler is built into Visual Studio so you won't need to use gcc or anything, although you can if you want (Cygwin, etc). Just press F5 to compile and run a VS project.

What is it you want to achieve with programming? Answering this question will help us give better advice on the direction you should take.
 

Pantlegz

Diamond Member
Jun 6, 2007
4,627
4
81
sorry should have been more clear, the sticky "Official Free References Thread" I just went to a few of the c++ as far as the code not agreeing, it just gives me errors while compiling and I have no clue as to why even if I copy/paste the code it wont compile. Since I really don't know what I'm doing I have no clue as to how I might be able to fix it.

I'm just trying to learn something new, I enjoyed scripting and I think I was good at it for having very limited programming experience. I'm also going to be in the IT field( get my AAS in december) soon and I know it would be helpful to know SQL and some php/html. I figured starting from the ground and working my way up would be the best idea. I may get a BS in comp engineering if IT is still in the dumps, so this may help with some of that. Really it's just something wanted to try.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
There could be quite a few reasons for compile errors with C++ code that you pasted in to the editor window. Post some of the errors and we can probably figure it out. As for whether to start with C# or C++, I don't think it matters. It's starting that's the important thing.
 

Pantlegz

Diamond Member
Jun 6, 2007
4,627
4
81
actually by just reading I got the one error figured out.. but that leads me to a question. the error was with

#include <stdio.h>
int main()
{
int x;
printf( "Declare x first" );

return 0;
}

and the issue was with the #include <stdio.h> it wanted <stdafx.h> how do I know what headers include what, and when to include what? Do I need both of them?

The only reason I copied the code was to make sure I didn't mistype something. I would rather type it out so it gets ingrained into my brain

and another issue I've run across is

#include <stdafx.h>

int main()
{
int this_is_a_number;

printf( "Please enter a number: " );
scanf( "%d", &this_is_a_number );
printf( "You entered %d", this_is_a_number );
getchar();
return 0;
}

I'm able to put the number in but then the program closes, why doesn't it finish the rest of the code? I'm not getting any errors, but it just has me enter the number and closes. From reading above it should say something like you entered whatever number, right? Sorry about all the stupid questions.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Are you hitting the Return/enter key after you type the number.

By not flushing the input buffer, the C/R may still be in the buffer and being picked up by the getchar().
 

Pantlegz

Diamond Member
Jun 6, 2007
4,627
4
81
yes I hit enter and the window closes.

Sorry, but I don't understand what you mean by not flushing the input buffer. How could I resolve this? I've kept working and I ran into a similar issue with this code...

int main() /* Most important part of the program!
*/
{
int age; /* Need a variable... */

printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
if ( age < 100 ) { /* If the age is less than 100 */
printf ("You are pretty young!\n" ); /* Just to show you it works... */
}
else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are old\n" );
}
else {
printf( "You are really old\n" ); /* Executed if no other statement is
*/
}
return 0;
}

I enter a number, press enter and the window just closes.
 

lambchops511

Senior member
Apr 12, 2005
659
0
0
I say start with C++, BUT, use GCC or command line compilers, you will learn a lot more and a lot faster

don't kid me, i love visual studio 2008, but i find its not the best choice for beginners, especially since the default options are Win32 GUI and not console, or pre-compilied headers that might confuse noobs
 

lambchops511

Senior member
Apr 12, 2005
659
0
0
Originally posted by: Pantlegz1
yes I hit enter and the window closes.

Sorry, but I don't understand what you mean by not flushing the input buffer. How could I resolve this? I've kept working and I ran into a similar issue with this code...

int main() /* Most important part of the program!
*/
{
int age; /* Need a variable... */

printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
if ( age < 100 ) { /* If the age is less than 100 */
printf ("You are pretty young!\n" ); /* Just to show you it works... */
}
else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are old\n" );
}
else {
printf( "You are really old\n" ); /* Executed if no other statement is
*/
}
return 0;
}

I enter a number, press enter and the window just closes.



Two options
1) add another scanf or getchar() to read another char before closing the window
2) open a dos / powershell window and launch your exe through there, your console won't close right away



 

Pantlegz

Diamond Member
Jun 6, 2007
4,627
4
81
Originally posted by: aznium
I say start with C++, BUT, use GCC or command line compilers, you will learn a lot more and a lot faster

don't kid me, i love visual studio 2008, but i find its not the best choice for beginners, especially since the default options are Win32 GUI and not console, or pre-compilied headers that might confuse noobs

it did a good job at that. I've also tried to add both the scanf and getchar and get the same results. Also DOS just opens the file in VS, the extension is .cpp I can't seem to save the project as a .exe.

I get the feeling that I'm not supposed to be doing this, it doesn't seem like it should be hard. I understand the tutorials but it just doesn't work! I'll try to ditch VS for the time being and VM ubuntu to see if I can get it to work there.

If I'm not using pre-compiled headers, how would I go about compiling them? If I don't how does it work? Sorry about all the questions again, I'm trying but I feel like I'm retarded...
 

Kirby

Lifer
Apr 10, 2006
12,028
2
0
i don't have VS in front of me now, but i'm pretty sure you can disable the precompiled headers. i think some of it is preformance stuff that may not mesh correctly with "non-MS" code. once you get that bs out of the way, you should have no problem doing regular c++ in VS.

the .cpp is the source file. it's the file you typed. you have to build it to get an executable you can run. and i wouldn't go through the trouble of having a nix VM, unless you just want to learn that too. there are command line compilers for windows.

btw, i'm in the c/c++ camp for beginners. memory management puts hair on your chest! :p
 
Oct 27, 2007
17,009
5
0
Originally posted by: nkgreen
i don't have VS in front of me now, but i'm pretty sure you can disable the precompiled headers. i think some of it is preformance stuff that may not mesh correctly with "non-MS" code. once you get that bs out of the way, you should have no problem doing regular c++ in VS.

the .cpp is the source file. it's the file you typed. you have to build it to get an executable you can run. and i wouldn't go through the trouble of having a nix VM, unless you just want to learn that too. there are command line compilers for windows.

btw, i'm in the c/c++ camp for beginners. memory management puts hair on your chest! :p

Memory management puts gray hairs on your head. Ever seen those pictures of US presidents before and after their terms, where they look like they've aged 20 years in their 4/8 year terms? I'm pretty sure I age 5 hours for every hour I spend debugging mallocs and chasing memory leaks :)
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Originally posted by: GodlessAstronomer
Originally posted by: nkgreen
i don't have VS in front of me now, but i'm pretty sure you can disable the precompiled headers. i think some of it is preformance stuff that may not mesh correctly with "non-MS" code. once you get that bs out of the way, you should have no problem doing regular c++ in VS.

the .cpp is the source file. it's the file you typed. you have to build it to get an executable you can run. and i wouldn't go through the trouble of having a nix VM, unless you just want to learn that too. there are command line compilers for windows.

btw, i'm in the c/c++ camp for beginners. memory management puts hair on your chest! :p

Memory management puts gray hairs on your head. Ever seen those pictures of US presidents before and after their terms, where they look like they've aged 20 years in their 4/8 year terms? I'm pretty sure I age 5 hours for every hour I spend debugging mallocs and chasing memory leaks :)

^ Truth.

Just as a suggestion, if you want to run your program without opening up a shell and typing the command, to get a pause you can use the "cin.get()" command. Please, despite whatever you read, do NOT use a System("Pause")!

it did a good job at that. I've also tried to add both the scanf and getchar and get the same results. Also DOS just opens the file in VS, the extension is .cpp I can't seem to save the project as a .exe.

I get the feeling that I'm not supposed to be doing this, it doesn't seem like it should be hard. I understand the tutorials but it just doesn't work! I'll try to ditch VS for the time being and VM ubuntu to see if I can get it to work there.

When you compile and build the .cpp, the compiler/assembler generates a .exe (When running in a Windows environment). A .exe file is a Windows Executable which runs those specific commands that were typed before you compiled it to assembly (and eventually binary opcode). It will be in your Debug or Release folder inside the project folder.

Make sure you use the Debug mode while working on the program. The memory address watches within Visual Studio give false information when working in release mode and trying to debug.

-Kevin
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Originally posted by: aznium
Originally posted by: Pantlegz1
yes I hit enter and the window closes.

Sorry, but I don't understand what you mean by not flushing the input buffer. How could I resolve this? I've kept working and I ran into a similar issue with this code...

int main() /* Most important part of the program!
*/
{
int age; /* Need a variable... */

printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
if ( age < 100 ) { /* If the age is less than 100 */
printf ("You are pretty young!\n" ); /* Just to show you it works... */
}
else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are old\n" );
}
else {
printf( "You are really old\n" ); /* Executed if no other statement is
*/
}
return 0;
}

I enter a number, press enter and the window just closes.



Two options
1) add another scanf or getchar() to read another char before closing the window
2) open a dos / powershell window and launch your exe through there, your console won't close right away

Just use a cin.get() command for C++.

-Kevin
 

lambchops511

Senior member
Apr 12, 2005
659
0
0
Originally posted by: nkgreen
i don't have VS in front of me now, but i'm pretty sure you can disable the precompiled headers. i think some of it is preformance stuff that may not mesh correctly with "non-MS" code. once you get that bs out of the way, you should have no problem doing regular c++ in VS.

the .cpp is the source file. it's the file you typed. you have to build it to get an executable you can run. and i wouldn't go through the trouble of having a nix VM, unless you just want to learn that too. there are command line compilers for windows.

btw, i'm in the c/c++ camp for beginners. memory management puts hair on your chest! :p

Yes you can, but most noobs dont know how to disable pre-compiled headers, and when they ask why - its really hard to explain to them when they just got a hello world working....
 

lambchops511

Senior member
Apr 12, 2005
659
0
0
Originally posted by: Gamingphreek
Originally posted by: aznium
Originally posted by: Pantlegz1
yes I hit enter and the window closes.

Sorry, but I don't understand what you mean by not flushing the input buffer. How could I resolve this? I've kept working and I ran into a similar issue with this code...

int main() /* Most important part of the program!
*/
{
int age; /* Need a variable... */

printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
if ( age < 100 ) { /* If the age is less than 100 */
printf ("You are pretty young!\n" ); /* Just to show you it works... */
}
else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are old\n" );
}
else {
printf( "You are really old\n" ); /* Executed if no other statement is
*/
}
return 0;
}

I enter a number, press enter and the window just closes.



Two options
1) add another scanf or getchar() to read another char before closing the window
2) open a dos / powershell window and launch your exe through there, your console won't close right away

Just use a cin.get() command for C++.

-Kevin

cin.get is for C++, he's using printf and scanf ... so only right to follow it up with a getchar or another scanf
 

lambchops511

Senior member
Apr 12, 2005
659
0
0
Originally posted by: Pantlegz1
Originally posted by: aznium
I say start with C++, BUT, use GCC or command line compilers, you will learn a lot more and a lot faster

don't kid me, i love visual studio 2008, but i find its not the best choice for beginners, especially since the default options are Win32 GUI and not console, or pre-compilied headers that might confuse noobs

it did a good job at that. I've also tried to add both the scanf and getchar and get the same results. Also DOS just opens the file in VS, the extension is .cpp I can't seem to save the project as a .exe.

I get the feeling that I'm not supposed to be doing this, it doesn't seem like it should be hard. I understand the tutorials but it just doesn't work! I'll try to ditch VS for the time being and VM ubuntu to see if I can get it to work there.

If I'm not using pre-compiled headers, how would I go about compiling them? If I don't how does it work? Sorry about all the questions again, I'm trying but I feel like I'm retarded...

It should work if put a getchar afterwards, do you call it right before your return statement?
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Originally posted by: aznium
Originally posted by: Gamingphreek
Originally posted by: aznium
Originally posted by: Pantlegz1
yes I hit enter and the window closes.

Sorry, but I don't understand what you mean by not flushing the input buffer. How could I resolve this? I've kept working and I ran into a similar issue with this code...

int main() /* Most important part of the program!
*/
{
int age; /* Need a variable... */

printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
if ( age < 100 ) { /* If the age is less than 100 */
printf ("You are pretty young!\n" ); /* Just to show you it works... */
}
else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are old\n" );
}
else {
printf( "You are really old\n" ); /* Executed if no other statement is
*/
}
return 0;
}

I enter a number, press enter and the window just closes.



Two options
1) add another scanf or getchar() to read another char before closing the window
2) open a dos / powershell window and launch your exe through there, your console won't close right away

Just use a cin.get() command for C++.

-Kevin

cin.get is for C++, he's using printf and scanf ... so only right to follow it up with a getchar or another scanf

He said it is C++, so it certainly wouldn't hurt anything.

As for the headers, I honestly don't see any logical reason why the "stdafx.h" header is included as printf and scanf should all be in the "stdio.h" header. There is no need to include everything that stdafx covers.

OP, if you are doing C++, you should be using the C++ overload I/O commands:

#include <iostream>
using namespace std;

void main()
{
int theNumber = 0;

cout << "Please enter a number" << endl;
cin >> theNumber;
cout << "You entered " << theNumber << endl;
cin.get();

return;
}

Right now you are using std C commands.

-Kevin

Edited: Fixed apparent brainlessness with regards to iostream.h -> iostream.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Originally posted by: GodlessAstronomer
Originally posted by: nkgreen
i don't have VS in front of me now, but i'm pretty sure you can disable the precompiled headers. i think some of it is preformance stuff that may not mesh correctly with "non-MS" code. once you get that bs out of the way, you should have no problem doing regular c++ in VS.

the .cpp is the source file. it's the file you typed. you have to build it to get an executable you can run. and i wouldn't go through the trouble of having a nix VM, unless you just want to learn that too. there are command line compilers for windows.

btw, i'm in the c/c++ camp for beginners. memory management puts hair on your chest! :p

Memory management puts gray hairs on your head. Ever seen those pictures of US presidents before and after their terms, where they look like they've aged 20 years in their 4/8 year terms? I'm pretty sure I age 5 hours for every hour I spend debugging mallocs and chasing memory leaks :)

I never ran into memory leaks when coding C++. There is just a simple rule I use, which I believe everybody should follow:

If you have a class which allocates memory, it's the responsibility of that class to clean up after itself.

I'm not sure why people don't follow that logic, as it's so easy to track down. Just look through the class and search for new's and if you don't see a delete, you have a problem.

Memory management is not difficult if you just follow simple rules. It makes me cringe when I see people complain about C++ as it has no memory management and it's so hard to deal with, blah blah blah. I think it forces good coding practices which I believe everybody needs to learn, C++ or not!
 

Pantlegz

Diamond Member
Jun 6, 2007
4,627
4
81
Originally posted by: Gamingphreek
Originally posted by: aznium
Originally posted by: Gamingphreek
Originally posted by: aznium
Originally posted by: Pantlegz1
yes I hit enter and the window closes.

Sorry, but I don't understand what you mean by not flushing the input buffer. How could I resolve this? I've kept working and I ran into a similar issue with this code...

int main() /* Most important part of the program!
*/
{
int age; /* Need a variable... */

printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
if ( age < 100 ) { /* If the age is less than 100 */
printf ("You are pretty young!\n" ); /* Just to show you it works... */
}
else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are old\n" );
}
else {
printf( "You are really old\n" ); /* Executed if no other statement is
*/
}
return 0;
}

I enter a number, press enter and the window just closes.



Two options
1) add another scanf or getchar() to read another char before closing the window
2) open a dos / powershell window and launch your exe through there, your console won't close right away

Just use a cin.get() command for C++.

-Kevin

cin.get is for C++, he's using printf and scanf ... so only right to follow it up with a getchar or another scanf

He said it is C++, so it certainly wouldn't hurt anything.

As for the headers, I honestly don't see any logical reason why the "stdafx.h" header is included as printf and scanf should all be in the "stdio.h" header. There is no need to include everything that stdafx covers.

OP, if you are doing C++, you should be using the C++ overload I/O commands:

#include <iostream.h>
using namespace std;

void main()
{
int theNumber = 0;

cout << "Please enter a number" << endl;
cin >> theNumber;
cout << "You entered " << theNumber << endl;
cin.get();

return;
}

Right now you are using std C commands.

-Kevin

Well this caused all sorts of problems... first and most confusing for me was "Warning 1 warning C4627: '#include <iostream.h>': skipped when looking for precompiled header"
why would it skip that? I also got an error saying that sdtafx.h wasn't included, why does it insist on using that header?

I think the errors that follow are issues with iosteam.h not being included. cout, cin and end1 are 'undeclared identifiers' and "left of '.get' must have class/struct/union"

Needless to say I'm confused as hell right now :(
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Originally posted by: brandonb
Originally posted by: GodlessAstronomer
Originally posted by: nkgreen
i don't have VS in front of me now, but i'm pretty sure you can disable the precompiled headers. i think some of it is preformance stuff that may not mesh correctly with "non-MS" code. once you get that bs out of the way, you should have no problem doing regular c++ in VS.

the .cpp is the source file. it's the file you typed. you have to build it to get an executable you can run. and i wouldn't go through the trouble of having a nix VM, unless you just want to learn that too. there are command line compilers for windows.

btw, i'm in the c/c++ camp for beginners. memory management puts hair on your chest! :p

Memory management puts gray hairs on your head. Ever seen those pictures of US presidents before and after their terms, where they look like they've aged 20 years in their 4/8 year terms? I'm pretty sure I age 5 hours for every hour I spend debugging mallocs and chasing memory leaks :)

I never ran into memory leaks when coding C++. There is just a simple rule I use, which I believe everybody should follow:

If you have a class which allocates memory, it's the responsibility of that class to clean up after itself.

I'm not sure why people don't follow that logic, as it's so easy to track down. Just look through the class and search for new's and if you don't see a delete, you have a problem.

Memory management is not difficult if you just follow simple rules. It makes me cringe when I see people complain about C++ as it has no memory management and it's so hard to deal with, blah blah blah. I think it forces good coding practices which I believe everybody needs to learn, C++ or not!

Well the 'new' command isn't the only command that deals with memory. Even though it is C++, you still have malloc, xalloc, palloc, realloc, etc... For strings you have a get and release buffer function. You could accidentally delete an array and orphan some pointers. There are plenty of ways to introduce a memory leak.

Yes, people should be aware of their code, but a lot of times people just plain forget. When you are in a Team Development environment, then it gets very hard to fix the other work.

-Kevin
 

Gamingphreek

Lifer
Mar 31, 2003
11,679
0
81
Originally posted by: Pantlegz1
Originally posted by: Gamingphreek
Originally posted by: aznium
Originally posted by: Gamingphreek
Originally posted by: aznium
Originally posted by: Pantlegz1
yes I hit enter and the window closes.

Sorry, but I don't understand what you mean by not flushing the input buffer. How could I resolve this? I've kept working and I ran into a similar issue with this code...

int main() /* Most important part of the program!
*/
{
int age; /* Need a variable... */

printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
if ( age < 100 ) { /* If the age is less than 100 */
printf ("You are pretty young!\n" ); /* Just to show you it works... */
}
else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are old\n" );
}
else {
printf( "You are really old\n" ); /* Executed if no other statement is
*/
}
return 0;
}

I enter a number, press enter and the window just closes.



Two options
1) add another scanf or getchar() to read another char before closing the window
2) open a dos / powershell window and launch your exe through there, your console won't close right away

Just use a cin.get() command for C++.

-Kevin

cin.get is for C++, he's using printf and scanf ... so only right to follow it up with a getchar or another scanf

He said it is C++, so it certainly wouldn't hurt anything.

As for the headers, I honestly don't see any logical reason why the "stdafx.h" header is included as printf and scanf should all be in the "stdio.h" header. There is no need to include everything that stdafx covers.

OP, if you are doing C++, you should be using the C++ overload I/O commands:

#include <iostream.h>
using namespace std;

void main()
{
int theNumber = 0;

cout << "Please enter a number" << endl;
cin >> theNumber;
cout << "You entered " << theNumber << endl;
cin.get();

return;
}

Right now you are using std C commands.

-Kevin

Well this caused all sorts of problems... first and most confusing for me was "Warning 1 warning C4627: '#include <iostream.h>': skipped when looking for precompiled header"
why would it skip that? I also got an error saying that sdtafx.h wasn't included, why does it insist on using that header?

I think the errors that follow are issues with iosteam.h not being included. cout, cin and end1 are 'undeclared identifiers' and "left of '.get' must have class/struct/union"

Needless to say I'm confused as hell right now :(

The bad thing is, it is all my fault for not explaining that code (and for not thinking before I typed).

First off, it is #include <iostream>

The .h is not necessary when using brackets with standard libraries.

That will solve cout, cin.

Next off (You are gonna laugh at this one), that is not an end1, it is an endl (L). ;)

See if that solves all of your problems - it should in my mind. As for stdafx error, that is certain weird. I don't have VS2008 in front of me or I would see what I can dig up on that.

At any rate, see if my corrections fix all my problems.
 

lambchops511

Senior member
Apr 12, 2005
659
0
0
Originally posted by: Gamingphreek
Originally posted by: Pantlegz1
Originally posted by: Gamingphreek
Originally posted by: aznium
Originally posted by: Gamingphreek
Originally posted by: aznium
Originally posted by: Pantlegz1
yes I hit enter and the window closes.

Sorry, but I don't understand what you mean by not flushing the input buffer. How could I resolve this? I've kept working and I ran into a similar issue with this code...

int main() /* Most important part of the program!
*/
{
int age; /* Need a variable... */

printf( "Please enter your age" ); /* Asks for age */
scanf( "%d", &age ); /* The input is put in age */
if ( age < 100 ) { /* If the age is less than 100 */
printf ("You are pretty young!\n" ); /* Just to show you it works... */
}
else if ( age == 100 ) { /* I use else just to show an example */
printf( "You are old\n" );
}
else {
printf( "You are really old\n" ); /* Executed if no other statement is
*/
}
return 0;
}

I enter a number, press enter and the window just closes.



Two options
1) add another scanf or getchar() to read another char before closing the window
2) open a dos / powershell window and launch your exe through there, your console won't close right away

Just use a cin.get() command for C++.

-Kevin

cin.get is for C++, he's using printf and scanf ... so only right to follow it up with a getchar or another scanf

He said it is C++, so it certainly wouldn't hurt anything.

As for the headers, I honestly don't see any logical reason why the "stdafx.h" header is included as printf and scanf should all be in the "stdio.h" header. There is no need to include everything that stdafx covers.

OP, if you are doing C++, you should be using the C++ overload I/O commands:

#include <iostream.h>
using namespace std;

void main()
{
int theNumber = 0;

cout << "Please enter a number" << endl;
cin >> theNumber;
cout << "You entered " << theNumber << endl;
cin.get();

return;
}

Right now you are using std C commands.

-Kevin

Well this caused all sorts of problems... first and most confusing for me was "Warning 1 warning C4627: '#include <iostream.h>': skipped when looking for precompiled header"
why would it skip that? I also got an error saying that sdtafx.h wasn't included, why does it insist on using that header?

I think the errors that follow are issues with iosteam.h not being included. cout, cin and end1 are 'undeclared identifiers' and "left of '.get' must have class/struct/union"

Needless to say I'm confused as hell right now :(

The bad thing is, it is all my fault for not explaining that code (and for not thinking before I typed).

First off, it is #include <iostream>

The .h is not necessary when using brackets with standard libraries.

That will solve cout, cin.

Next off (You are gonna laugh at this one), that is not an end1, it is an endl (L). ;)

See if that solves all of your problems - it should in my mind. As for stdafx error, that is certain weird. I don't have VS2008 in front of me or I would see what I can dig up on that.

At any rate, see if my corrections fix all my problems.

You need to disable pre-compiled headers ... this is what confuses noobs and why i think VS2008 is bad for beginners

goto project->properties->configuration properties->C/C++->precompiled header->Create/Use Precompiled Header->Select Not Using Precompiled Header