After I compile and try to run a code, it'd usually run and then almost immediatly shut off on me. It shows for nearly a millisecond and just turns off by itself. Can't really find a setting that would make it stay longer or until chosen to close.
After I compile and try to run a code, it'd usually run and then almost immediatly shut off on me. It shows for nearly a millisecond and just turns off by itself. Can't really find a setting that would make it stay longer or until chosen to close.
try using cin
// ask for a person's name, and greet the person
#include <iostream>
#include <string>
int main()
{
// ask for the person's name
std::cout << "Please enter your first name: ";
// read the name
std::string name; // define name
std::cin >> name; // read into
// write a greeting
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
That's the code I tried this time and it turns as soon as the Hello "Name"! comes up on the screen, just have like a millisecond to see it.
Put a cin after the output.
insert this at the end of main() previous return 0;
Code:system("PAUSE");
I hacked 127.0.0.1
Things to Avoid in C/C++ -- system("pause"), Part 4 - GIDNetwork
std::cin.get();
return 0;
You may want to put this between the #include and int main()
Now you can leave std:: awayCode:using namespace std;
Try:
Code:// ask for a person's name, and greet the person #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter your first name: "; string name; // define name cin >> name; // read into cout << "Hello, " << name << "!" << endl; cin.ignore(cin.rdbuf()->in_avail() + 1); return 0; }
std::cin.get() isn't working because cin already has a value (from cin >> name; )
See below...
Code:// ask for a person's name, and greet the person #include <iostream> #include <string> int main() { // ask for the person's name std::cout << "Please enter your first name: "; // read the name std::string name; // define name std::cin >> name; // read into // write a greeting std::cout << "Hello, " << name << "!" << std::endl; //wipe buffer std::cin.clear(); std::cin.sync(); //get cin - prevent closing std::cin.get(); return 0; }
Here, this may be some better, and possible clearer example...
Code:/* These are called libraries. They are used to gather a bunch of premade functions and use them for yourself */ #include <iostream> #include <string> /* This uses the standard namespace (so you can excluse std::cout, and things of the like */ using namespace std; int main() { /* This initalizes a string called "first_name" */ string first_name; /* This outputs text onto the screen */ cout << "Hello, please enter your first name: "; /* This reads the users input and puts the data into the string */ cin >> first_name; /* Outputs text onto the screen and places the data for the first_name string onto the screen as well */ cout << "Welcome, " << first_name << "!" << endl; /* Stops the window from automatically closing */ cin.clear(); cin.sync(); cin.get(); /* Returns a value of 1 if the program exited successfully */ return 1; }
Last edited by skatercam11; 05-31-2009 at 11:40 PM.
Or just scram the c function getchar(); before return 0 and you'll be fine
*cough* getchar sometimes requires an extra include.
And skatercam11, a link was previously posted in this thread as to why system("PAUSE") should not be used as it cannot be ported and it's a resource hog.
Putting a while(1); before the return wouldn't require any includes and is platform independant.