C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 8th, 2003, 07:08 PM
DAIALOS DAIALOS is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 1 DAIALOS User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Arrow "easy" C++ help plz

ok my problem is when i write all the code and then press execute the program work great but when i enter the number to make the program works it close automatically what i coud do to fix this? here is my code:
(sorry for my english)





#include <iostream.h>

int main()

{

int age;

cout<<"Please input your age: ";

cin>>age;

if(age<100)

{

cout<<"You are pretty young!";

}

else if(age==100)

{

cout<<"You are old";

}

else

{

cout<<"You are really old"; //Executed if no other statement is executed

}

return 0;

}

Reply With Quote
  #2  
Old January 8th, 2003, 09:12 PM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 618 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 13 h 50 m
Reputation Power: 26
the program runs and finishes so it exits.

try pausing the program before the return 0 (if this works in your compiler)

sleep(5000);//in milliseconds so five seconds

Better, try putting a while loop and setting up a condition (value ) that the user can enter to leave the program

Code:

do
{
     cout<<"Please input your age (enter zero to exit):  "; 
      cin>>age;
      //chack to see if the user tried to exit
      if(0 != age)
      {
                //all your code here
      }
}while(0 != age)
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

Frank Zappa

Reply With Quote
  #3  
Old January 11th, 2003, 12:47 PM
Ratt Ratt is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 6 Ratt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You could either do that or you can simply run your program from a DOS prompt or console NOT from Windows.

I made the same mistake, when I first started teaching myself C++ programming.

Reply With Quote
  #4  
Old January 15th, 2003, 03:55 PM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
When I was teaching my students quickly learned to keep a command shell open and they did all runs from the command line, never the Execute button. That button is really only useful for Windows applications.
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/

Reply With Quote
  #5  
Old January 31st, 2003, 06:48 AM
vanniella vanniella is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 4 vanniella User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
just pause...

Hi,

This looks like one of the program I gave to my students...

we are using Dev-C++ software (which works great on Windows platform apart from Windows XP) and the easiest way for your particular problem is to include this line just before the return 0:

system ("PAUSE");

so your program will look like this:

.
.
.
.
cout<<"You are really old";
}

system ("PAUSE");
return 0;
}

when you use this your string will be displayed and everything will stand still until you press any key (and will display"Press any key to continue...")

I know that you posted this quite a while ago but just in case...

Enjoy learning C++!

Annie.

Reply With Quote
  #6  
Old February 1st, 2003, 11:14 PM
CStrauss CStrauss is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 37 CStrauss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 3 m 50 sec
Reputation Power: 11
Here is two simple lines of code that could help okay actually 3.

first #include<conio.h>

then right before your return 0 include the following:

cerr<<"Press any key to continue";
getch();

the cerr is part of the iostream header file similar to the cout statement allowing you to display something the to the string.

the getch(); is part of the conio.h header file it basicly allows you to store junk variable allowing you to pause the screen until as in the cerr statement says press any key to continue. then whatever key is pressed is stored then the program will go to the return 0 and then end the program

hope this helps

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > "easy" C++ help plz

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap