Posted August 4, 200519 yr Hi, I am very new to programming its something ive always been meaning to do and ive started following tutorials. I have written a little test code for input/output. But everytime I run the program and I type in the age it closes the window. Could you help? #include <iostream> using namespace std; void main(void) { int Age = 0; /* We are setting the value of the variable 'Age' to '0' */ cout << "How old are you?: "; cin >> Age; cout << "You are " << Age << "years old" << endl; } It isnt like I am too bothered if I can get this working or not but I would want to know what is wrong for future programs I create. Thanks for any help in advance.
August 4, 200519 yr First off, make your main return int (Noone argue about this, it's STANDARD). Then, at the bottom (just before '}'), add the following: cin.sync(); cin.get(); return 0; That will make it look like this, with some more small modifications: #include <iostream> using namespace std; int main(int argc, char* argv[], char* envp[]) { int Age = 0; /* We are setting the value of the variable 'Age' to '0' */ cout << "How old are you?: "; cin >> Age; cout << "You are " << Age << " years old" << endl; cin.sync(); cin.get(); return 0; }
August 4, 200519 yr Author This is the code now: #include <iostream> using namespace std; int main(int argc, char* argv[], char* envp[]) { int Age = 0; /* We are setting the value of the variable 'Age' to '0' */ cout << "How old are you?: "; cin >> Age; cout << "You are " << Age << " years old" << endl; cin.sync(); cin.get(); return 0; } and it still doesnt work :(
August 4, 200519 yr Author well can you give me a link to the program you use please, to compile and write your program, cheers
August 4, 200519 yr Yeah Dev-Cpp owns. I've never seen a second char pointer in the main function before... I know the first refers to the arguments given in the command line, but what's the second one for?
August 5, 200519 yr Quote from a C++ reference: envp contains a pointer to a nil-terminated array of null-terminated strings, containing the environment variables passed to the program by the OS. It is not available on all systems.
August 5, 200519 yr #include <iostream> using namespace std; int main() { int Age = 0; /* We are setting the value of the variable 'Age' to '0' */ cout << "How old are you?: "; cin >> Age; cout << "You are " << Age << " years old" << endl; } i quickly looked over this and nothing is wrong with this code, it does wat its suppose to and then quits, thats y its hard to see the "You are <AGE> years old" do wat ViperX said and add in cin.sync(); and cin.get() i dont know wat sync() does but cin.get() waits for u to press enter
August 5, 200519 yr #include <iostream> using namespace std; int main() { int Age = 0; /* We are setting the value of the variable 'Age' to '0' */ cout << "How old are you?: "; cin >> Age; cout << "You are " << Age << "years old" << endl; system("PAUSE"); return EXIT_SUCCESS; } try that