Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 12-05-2004, 02:14 AM   #1
Solid State Member
 
Join Date: Dec 2004
Posts: 17
Default C++ Beginner

I have just started programming in C++ tonight. When I run the program "Hello World" it comes up real fast and it disapears. It says it compiled right and I can see the text "Hello World" in the box before it closes. What syntax should i use to keep the box from closing.

thx in advance

Here is the code that I am using

#include <iostream.h>

void main()
{
cout <<"Hello World!\n";
}
__________________

Cangacosh is offline   Reply With Quote
Old 12-06-2004, 05:39 PM   #2
Beta Member
 
Join Date: Nov 2003
Posts: 2
Default Re: C++ Beginner

If you are using vc++, make sure you are clicking run, not debug, because with run it will automayically make you press enter at the end.
__________________

imaginieus is offline   Reply With Quote
Old 12-06-2004, 09:13 PM   #3
Solid State Member
 
Join Date: Dec 2004
Posts: 17
Default Re: C++ Beginner

I am using Dec C++
Cangacosh is offline   Reply With Quote
Old 12-06-2004, 09:59 PM   #4
Baseband Member
 
Join Date: Dec 2004
Posts: 53
Default Re: C++ Beginner

Quote:
Originally Posted by Cangacosh
I have just started programming in C++ tonight. When I run the program "Hello World" it comes up real fast and it disapears. It says it compiled right and I can see the text "Hello World" in the box before it closes. What syntax should i use to keep the box from closing.

thx in advance

Here is the code that I am using

#include <iostream.h>

void main()
{
cout <<"Hello World!\n";
}
The reason it's closing like that is because it's openeing the program running it and when it's finished closes the window because the program has gone through the code and done, you haven't put a anything in there to pause it.

The best way I was taught to pause the hello world program was to use cin.get();

so it would go in the code like this:
#include <iostream.h>

void main()
{
cout <<"Hello World!\n";
cin.get();
}

That should pause the program so you can look at it. If it doesn't work you could always try getch(); but that is not really recommened because it only works in certain compilers.

you would type it like this:
#include <iostream.h>
#include <conio.h>

void main()
{
cout <<"Hello World!\n";
getch();
}
Eyes0nly is offline   Reply With Quote
Old 12-06-2004, 10:25 PM   #5
Solid State Member
 
Join Date: Dec 2004
Posts: 17
Default Re: C++ Beginner

Thanks man....

cin.get() worked fine.
Cangacosh is offline   Reply With Quote
Old 12-16-2004, 11:17 PM   #6
Beta Member
 
Join Date: Dec 2004
Posts: 2
Default

I had a few questions first I noticed that in the book I have it says to use "int main()" however in the code above you guys use void main() is one different from the other? Also I have a problem similiar to cang however mine is when I run a program that includes cin. I tried using cing.get(); but it still just flashes after the input is put in. Here is what the code looks like now... any help is aprecaited


#include <iostream.h>

int main()
{
cout<<"Please enter a radius so I can solve for the area"<<endl;
double radius;
cin>> radius;
cout<<"Ok, with the value "<<radius<<" you entered the area is ";
cout<<3.14*radius*radius<<endl;

cin.get();
return(0);

}
ultrashawn18 is offline   Reply With Quote
Old 12-17-2004, 11:55 PM   #7
Baseband Member
 
Join Date: Dec 2004
Posts: 53
Default Re: C++ Beginner

I didn't even notice it said void, yeah void is shouldn't be used in C++ because main is requiried to return int so use int main() however it will still work but it's not recommended as void main is used for C.

As for your program not pausing try taking out the return(0);, by the way there shouldn't be a parenthesis around the 0 it should be return 0; infact you shouldn't even bother using it, as when it gets to the end it will return a value of 0 anyway. Unless you specify a different value. I can't really test it myself as I haven't got a compiler on my computer.
Eyes0nly is offline   Reply With Quote
Old 12-25-2004, 08:03 AM   #8
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: C++ Beginner

#include <iostream>
#include <cstdlib> // used to pause
using namespace std; /* use this ppl!!! then you won't have to write them annoying .h's and use old libraries*/

int main ()
{
cout << "this program SHOUDLN'T flash\n";
system ("pause");
return (0);
}
RewtGuy is offline   Reply With Quote
Old 12-25-2004, 08:05 AM   #9
Daemon Poster
 
RewtGuy's Avatar
 
Join Date: Dec 2004
Posts: 595
Send a message via AIM to RewtGuy
Default Re: C++ Beginner

it compiled and worked for me, i'm using Dev-C++ i dunno for some weird reason i can't get system ("pause"); to work on linux though

enjoy
RewtGuy is offline   Reply With Quote
Old 12-26-2004, 12:52 AM   #10
Baseband Member
 
Join Date: Dec 2004
Posts: 53
Default Re: C++ Beginner

system ("pause"); is a very bad practice to get into because it is so system intensive and is highly unrecommended so i would use cin.get(); there is never a good reason to use pause function so don't do it!

A good analogy I read was using system pause is like opening your door with a bull dozer sure it works but the key is much cleaner and more efficient.
__________________

Eyes0nly is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 09:07 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0