First C++ Program

Started by
15 comments, last by Photonman 18 years, 3 months ago
This is definitely a FAQ.

You are writing a console program, and there is nothing wrong with the code. Just run the program from the console and get in the habit of running console programs from the console.
Advertisement
Quote:
I don't think you read his post completely.



Ack... You're right. Sorry about that.
Quote:Original post by Photonman
As a side note, you should prolly use cin.get() instead of system("PAUSE"), since system("PAUSE") isn't available on every platform and it actually suspends your program and goes to the OS to find the function.... Not all that important, it's just style.

And put using namespace std; at the beginning...that will save you from typing std:: every third line...


So I could type it like this?

{
std;cout << "Game Over!" << std::endl;
cin.get();
return 0;
}



you can also try this. Sleep(2000) suspends the execution of the program for 2000 milisecs. :)

#include <iostream>#include <windows.h>using namespace std; //this allows you to not have to do this: std::cout, etc.int main () {   cout << "Game Over, Dude!" << endl;   Sleep(2000);     return 0;}
Quote:Original post by SKS
Quote:Original post by Photonman
As a side note, you should prolly use cin.get() instead of system("PAUSE"), since system("PAUSE") isn't available on every platform and it actually suspends your program and goes to the OS to find the function.... Not all that important, it's just style.

And put using namespace std; at the beginning...that will save you from typing std:: every third line...


So I could type it like this?

{
std;cout << "Game Over!" << std::endl;
cin.get();
return 0;
}


No, you would write it simply as cout, when you do "using namespace std" it pretty much just makes the typing easier on you.
Quote:Original post by SKS
I'm on the first C++ program and already got a problem. I am using Bloodshed Dev C++ I typed in the program as the book said to do

// Game Over
// A first C++ program

#include <iostream>

int main()
{
std::cout << "Game Over!" << std::endl;
return 0;
}

Went to compile and run and it did open, but only for a second then it closed.

Anyone know why?
Could it be a probloem with my computer?


Are u looking for _getch() ? (note: this site may be changed in the future)
Not exactly.... More like this:
// Game Over// A first C++ program#include <iostream>using namespace std;//This eliminates the need for the std:: before everythingint main(){cout<<"Game Over!"<<endl;//Notice no std:: before cout and endlcin.get();//Keeps the window open until enter is pressed.return 0;}
______________________________Stranger things have happened...The Following Sentence is True. The Above Sentence is False.

This topic is closed to new replies.

Advertisement