Simple C++ Programming Help

Well in CPR (Computer Programming and Repair) we're doing a simple program and it includes this.........and I need some help in finding out with what's wrong with it because every time you input the number and it closes you out immediately.

#include <iostream>
#include <string>
using namespace std;

int main()

{
	int age;

	cout << "How old are you?" <<endl;
	cin >> age;

	cout << "Holy JEVUS you are " <<age<< " years old!" <<endl;

	return 0;

}

That didn't help at all
Create a Console Project. This code works fine.

On another note, just so your aware, the cin is in the wrong place. At least if you want it to look nicer. I mean it works, but it is bad practice to put the input on the next line. It should be immediately after the ? ( question ).

Although, if you knew this already, then no problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
using namespace std;

int main()

{
	int age;

	cout << "How old are you? ";       // Did you notice I took out <<endl
        cin >> age;

	cout << "Holy JEVUS you are " <<age<< " years old!" <<endl;

	return 0;

}


The only other problem I could see is making sure that ONLY numbers will be inputted. It will error out if you try to type in a letter or some other character other then a number.
Last edited on
Check blackout's link. It is relevant. What IDE are you using?
if you are using dev c++, try add system("PAUSE"); before return 0;
Yeah, grax is right, your program is doing exacly what you told it to, you just forgot to tell it to wait for you te read what it wrote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
   This is the header file that has the function that
   will wait forus to read the text on the console
*/
#include <cstdlib>// YOU NEED TO DO THIS
#include <iostream>
#include <string>
using namespace std;

int main() {
	int age;
	cout << "How old are you? ";
        cin  >> age;
	cout << "Wow! You are " << age << " years old!" << endl;
	system("pause");// AND THIS
	return 0;
}
Last edited on
Using system("pause") is a bad habit to get into. Don't worry about why right now, has to do with security and stability. Depending on the IDE you are using, there should be an option to run without debugging, that will leave your window open. Or you could just add a cin << something at the end of your program, that will wait for some input before it closes.
@NanoBytes
You really shouldn't use system("pause"), try using cin.ignore(). It waits for the user to press enter before continuing.
it depends on the error it's showing and how you're running your program .. ?
I realize the implications that system("pause") brings, I was simply helping the guy out, and as a newbie he doesn't need to worry about anything like that right now
He also shouldn't be getting used to using system("pause"). There are better and safer ways of accomplishing the same thing
Using Dev-C++ is an even worse habit.
And why might that be? Dev-C++ is just as efficient and safe as any other C++ development environment.
Dev-C++ is just as efficient and safe as any other C++ development environment.


If you're referring to the recent continuation of Dev-C++ (V4.9.9.3 and above, found over here: http://orwellengine.blogspot.com/ ) then it is much improved and comes with a modern compiler.

If you mean the version found over at Bloodshed, then your statement is demonstrably false and dangerous to newcomers.

Unfortunately, a search for Dev-C++ by an enthusiastic newcomer tends to turn up the bad version first.
Last edited on
to stop the screen use function kbhit() function in <conio.h>


#include <iostream>
#include <string>
#include<conio.h>
using namespace std;

int main()

{
int age;

cout << "How old are you?" <<endl;
cin >> age;

cout << "Holy JEVUS you are " <<age<< " years old!" <<endl;
while(!kbhit());

return 0;

}



as I think u r using Visual studio 2008 or 2010
Last edited on
See but GNU-C++ is the all time best (Well at least in my opinion)
maybe you can add #include <conio.h> at the header and you can put getch(); before the last semi-colon of your main function.
Last edited on
Topic archived. No new replies allowed.