Thread: Moving on
View Single Post
Old
legolizard
Senior Member
 
legolizard's Avatar
 
Status: Offline
Posts: 242
Join Date: Jan 2011
Location: Aboard the Hyperion escaping the zerg.
Default 08-07-2011, 09:28 PM

Now Microsoft and that book are getting on my nerves. First they use void main() and anyone who uses that should be shot. Now they are using a C library and not even using the STANDARD NAMESPACE. They even forgot the@#$&*^!!! semicolon at the end of the cout statement!!!! RAWR!!!!! *thorws something from desk*

Sorry one of the few things that annoys me is when experts have no clue on what they are doing.

Okay well firstly instead of

Code:
#include <iostream.h>
use

Code:
#include <iostream>
iostream.h is for C not C++.

Secondly cout and endl and a few other things are part of the library iostream within the standard namespace. Now there are two ways to address this you can

A)
Code:
#include <iostream>


using namespace std;
int main()
{

cout << "Hello! "<<endl;
cout<<"Congrats on making your first C++ program!"<<endl;

return 0; 

}
or

B)
Code:
#include <iostream>


int main()
{

std::cout << "Hello! "<<std::endl;
std::cout<<"Congrats on making your first C++ program!"<<std::endl;

return 0; 

}
Matter of personal preferance. Also one other thing to note. Both programs above will compile fine but you will notice that your prompt will open and then close immediately. To counter act this you can do a variety of things such as:

Code:
#include <iostream>


using namespace std;
int main()
{

cout << "Hello! "<<endl;
cout<<"Congrats on making your first C++ program!"<<endl;

cin.get();//What I generally use; just press the enter key.

return 0; 

}
or


Code:
#include <iostream>
#include <conio.h>//Technically a C header file but it is not as bad as iostream.h


using namespace std;
int main()
{

cout << "Hello! "<<endl;
cout<<"Congrats on making your first C++ program!"<<endl;

getch();

return 0; 

}
or


Code:
#include <iostream>


using namespace std;
int main()
{

cout << "Hello! "<<endl;
cout<<"Congrats on making your first C++ program!"<<endl;

system("PAUSE");//This method however is very resourse costly do not use for big programs as it is bad programming pracitce.

return 0; 

}
One final thing endl = end line or enter.


"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe."-Albert Einstein

Last edited by legolizard; 08-07-2011 at 09:42 PM. Reason: few syntax errors
   
Reply With Quote