GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Jul-2007, 05:53
doom1992 doom1992 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 98
doom1992 is on a distinguished road

Console Will Not Stay Open


hi there people, for some strange reason my code will not stay open what do i add in to get the console to stay open. Here is my code

CPP / C++ / C Code:
#include <iostream>

int age;

int main () {
    std::cout <<"What is your age";
    std::cin >> age;
    
    if (age >= 18) {
            std::cout <<"You can vote";
            }
            else {
                 std::cout <<"You are to you to vote";
                 }
                 
                 std::cout <<"Press Enter To Exit";
                 std::cin.get();
                 return 0;
}                
  #2  
Old 22-Jul-2007, 06:10
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 450
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Console Will Not Stay Open


Did you read at all what I said in your other similar thread?

http://www.gidforums.com/t-14967.html

CPP / C++ / C Code:
std::cin >> age;
std::cin.get();
You input "18[ENTER]". The first statement reads 18, [ENTER] left. The second statement reads [ENTER].

Run console programs from the console. And by running from the console I mean "open the console, navigate to the directory that contains your program, execute." Or put system("PAUSE") at the end, but I find that to be useless complicating of matters.

Now if there are things you don't get in this, ask them in this thread. Someone around here most probably has the answers.

EDIT

If you don't want to run from console, you can add something like this at the end
CPP / C++ / C Code:
while (std::cin.get() != '\n')        // reads up until and including newline
   continue;
std::cin.get();                         // waits for input
  #3  
Old 22-Jul-2007, 06:26
doom1992 doom1992 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 98
doom1992 is on a distinguished road

Re: Console Will Not Stay Open


yhanks the system puase worked, and btw i did open it from the root
  #4  
Old 22-Jul-2007, 06:46
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 450
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Console Will Not Stay Open


Quote:
Originally Posted by doom1992
yhanks the system puase worked, and btw i did open it from the root
I don't know what you mean by opening it from the root.

But if you run the programs from the console, then why would you want to put something like system("PAUSE") at the end of the program? What is the use of keeping the program open?
  #5  
Old 22-Jul-2007, 07:04
doom1992 doom1992 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 98
doom1992 is on a distinguished road

Re: Console Will Not Stay Open


hi there, i want to use a if else statment to make a application. I would like to make an application where i can ask the user for a password and if the password is correct there will be a console out saying well done, if it is not correct it will say better look next time, could you please quickly write me this little application and reply to this post.

Thanks
  #6  
Old 22-Jul-2007, 22:47
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,432
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Console Will Not Stay Open


Quote:
Originally Posted by doom1992
yhanks the system puase worked, and btw i did open it from the root
Wrong solution. Here's why...
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #7  
Old 22-Jul-2007, 23:55
BentFX's Avatar
BentFX BentFX is offline
New Member
 
Join Date: Jul 2007
Location: Pacific NW, USA
Posts: 9
BentFX is on a distinguished road

Re: Console Will Not Stay Open


I'm no pro, but some searching came up with this... http://forums.devarticles.com/c-c-help-52/cin-get-9130.html

Two solutions that seemed to be offered were... send cout an << endl; or call cin.ignore(); to flush out buffers before your cin.get();

Hope that is helpful


Skip
  #8  
Old 11-Aug-2007, 14:01
GrapeApe GrapeApe is offline
New Member
 
Join Date: Aug 2007
Posts: 7
GrapeApe is on a distinguished road

Re: Console Will Not Stay Open


Add this below your includes :


CPP / C++ / C Code:
void exit() 
{ 
  std::cout<<"Press [Enter] to Exit . . .";
  std::cin.get();
}




Remove this from you code:

CPP / C++ / C Code:
std::cout <<"Press Enter To Exit";
                 std::cin.get();
                 return 0;

And add this in their place:


CPP / C++ / C Code:
   exit();

   std::cin.get();
I am very new to c++ and it took me a awhile to find this solution but it works for me.
Last edited by admin II : 12-Aug-2007 at 19:43. Reason: Please surround your C++ code with [cpp] ... [/cpp]
  #9  
Old 11-Aug-2007, 14:36
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 450
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Console Will Not Stay Open


Quote:
Originally Posted by GrapeApe
it works for me.
Actually it doesn't. The function call is actually pretty pointless. It's equivalent to just writing
CPP / C++ / C Code:
std::cout<<"Press [Enter] to Exit . . .";
std::cin.get();
std::cin.get();
cin.get() reads ONE char. One char. If you have one extra char in input stream waiting, then you're lucky. If you have two, then you're out of luck.

Did you try doing some simple testing like entering "5.7"?

Take a look at this thread: http://www.gidforums.com/t-15133.html

There is also a bit more advanced stringstream stuff which Dave explained, but also other useful hints. Dave also explains some basics of cin functionality, which every (decent, beginner-level) C++ book should cover. Problem nowadays, though, seems to be that no one buys books. Except us oldies.
  #10  
Old 12-Aug-2007, 10:28
GrapeApe GrapeApe is offline
New Member
 
Join Date: Aug 2007
Posts: 7
GrapeApe is on a distinguished road

Re: Console Will Not Stay Open


I apologize I didn't understand the question and my answer was SLOPPY.
This is what he is looking for.

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>

using namespace std;


void getinput ( int &age)
   {
    std::cout <<"What is your age you:";
    std::cin >> age;
    cin.ignore(100, '\n');  
   } 
    


int main () {  

     int age = 0;

   do

        {      
          getinput(age);
        
         if ( age < 18 )
              {
            std::cout <<"You are to young to vote.\n";
              }
           
        } while ( age < 18 );



   if (age >= 18 ) {
            std::cout <<"You can vote! \n";
            }

        
  std::cout<<"Press [Enter] Exit...";
std::cin.get();
std::cin.get();


}
Last edited by admin II : 12-Aug-2007 at 19:42. Reason: Please surround your C++ code with [cpp] ... [/cpp]
 
 

Recent GIDBlogConfiguring iptables for Webmin Servers Index Module by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bloodshed Dev C++ Project Options JdS C++ Forum 7 14-Jan-2013 08:09
The 100 prison doors problem ruby_booby C Programming Language 7 06-Feb-2012 02:54
Apache default page can't be visited satimis Apache Web Server Forum 0 05-Apr-2007 04:40
how to keep the console window from opening tajjyarden C++ Forum 1 06-Feb-2007 13:40

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:11.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.