delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/07/24/13:23:43

Message-ID: <3B5DAECE.69DE4BF5@crbkpc.com>
Date: Tue, 24 Jul 2001 13:22:23 -0400
From: Chris Smith <csmith AT crbkpc DOT com>
X-Mailer: Mozilla 4.7 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: cin.sync() & blue screen
Reply-To: djgpp AT delorie DOT com

Two questions:

1.)  I'm using the function below in my program to get input from the
user. Everything compiles ok, but if the user enters a char instead of
int, or any other invalid input, the program goes into an infinite loop.
Presumably, this is because cin.sync() is not flushing the standard
input buffer.

2.)  Also, if the user attempts to exit the program by clicking on the
close window button on the upper righthand corner of the MS DOS window
(instead of entering data), the program crashes with the blue-screen of
death.

Any insight into what I'm doing wrong would be greatly appreciated.

// function to retrieve the input from the user
void getnumber(char sentence[], int* input){
        cout<<sentence;
        while(!(cin>>*input)){
                cin.sync();
                cin.clear();
                cerr<<"Invalid Input"<<endl
                      <<"Try again. "<<sentence;
        }
        cin.sync();
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019