Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Keyboard input (newbie question)

Reply
Thread Tools

Keyboard input (newbie question)

 
 
Tarique
Guest
Posts: n/a
 
      11-08-2007
Hello all.

How can i clear the input buffer before i accept a user input?Further
what is the correct way to clear the buffer in case of an *invalid*
input.The following naive program will show up the error.
any help will be greatly appreciated.


#include<iostream>
#include<stdlib.h>
using namespace std;

class Publication
{
protected:
char title[30];
float price;

public:
void getdata(void)
{
cout<<"Title :"<<endl;
gets(title);
cout<<"Price"<<endl;
cin>>price;
}

void putdata(void)
{
cout<<"Title :"<<title<<endl;
cout<<"Price :"<<price<<endl;
}
};

class Bookublic Publication
{
private:
int page_count;
public:
void getdata(void)
{
Publication::getdata();
cout<<"No of pages"<<endl;
cin>>page_count;
}

void putdata(void)
{
Publication:utdata();
cout<<"No Of Pages"<<page_count<<endl;
}
};

class Tapeublic Publication
{
private:
int play_time;
public:
void getdata(void)
{
Publication::getdata();
cout<<"Playtime"<<endl;
}

void putdata(void)
{
Publication:utdata();
cout<<"Play time"<<play_time<<endl;
}
};

int main(void)
{
Book B1;
Tape T1;
int ch;

cout<<"...........B1........."<<endl;
B1.getdata();
B1.putdata();
cout<<"...........T1........."<<endl;
//while (('\n' != (ch = getchar())) && (EOF !=ch));
T1.getdata();
T1.putdata();
return 0;
}


--
C is quirky, flawed, and an enormous success.
—Dennis Ritchie
 
Reply With Quote
 
 
 
 
osmium
Guest
Posts: n/a
 
      11-08-2007
"Tarique" wrote:

> How can i clear the input buffer before i accept a user input?Further
> what is the correct way to clear the buffer in case of an *invalid*
> input.The following naive program will show up the error.
> any help will be greatly appreciated.
>
>
> #include<iostream>
> #include<stdlib.h>
> using namespace std;
>
> class Publication
> {
> protected:
> char title[30];
> float price;
>
> public:
> void getdata(void)
> { cout<<"Title :"<<endl;
> gets(title);
> cout<<"Price"<<endl;
> cin>>price;
> }
>
> void putdata(void)
> {
> cout<<"Title :"<<title<<endl;
> cout<<"Price :"<<price<<endl;
> }
> };
>
> class Bookublic Publication
> {
> private:
> int page_count;
> public:
> void getdata(void)
> {
> Publication::getdata();
> cout<<"No of pages"<<endl;
> cin>>page_count;
> }
>
> void putdata(void)
> {
> Publication:utdata();
> cout<<"No Of Pages"<<page_count<<endl;
> }
> };
>
> class Tapeublic Publication
> {
> private:
> int play_time;
> public:
> void getdata(void)
> {
> Publication::getdata();
> cout<<"Playtime"<<endl;
> }
>
> void putdata(void)
> {
> Publication:utdata();
> cout<<"Play time"<<play_time<<endl;
> }
> };
>
> int main(void)
> {
> Book B1;
> Tape T1;
> int ch;
>
> cout<<"...........B1........."<<endl;
> B1.getdata();
> B1.putdata();
> cout<<"...........T1........."<<endl;
> //while (('\n' != (ch = getchar())) && (EOF !=ch));
> T1.getdata();
> T1.putdata();
> return 0;
> }


See if this thread will solve your problem.

http://groups.google.com/group/comp....e6cd78834c9602


 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      11-09-2007
On Nov 8, 6:56 pm, Tarique <peo_...@yahoo.com> wrote:

> How can i clear the input buffer before i accept a user input?


You can't, at least not in standard C++. You'll have to use
something specific to your implementation.

> Further what is the correct way to clear the buffer in case of
> an *invalid* input.


Read ahead, ignoring input, until the next synchronization
point. If the next synchronization point is a new line (a
frequent case), then std::cin.ignore() can be used. Just don't
forget to clear the error first.

If the input is line oriented (and thus, new lines aren't just
"white space") the usual idiom is to read it line by line, using
getline, and then use an istringstream to extract the data from
the line.

> The following naive program will show up the error. any help
> will be greatly appreciated.


> #include<iostream>
> #include<stdlib.h>
> using namespace std;


> class Publication
> {
> protected:
> char title[30];
> float price;


> public:
> void getdata(void)
> {
> cout<<"Title :"<<endl;
> gets(title);


Don't ever do this!!! There is NO correct use of gets. (Also,
mixing FILE* input and istream isn't generally a good idea,
either.)

If you want to read a complete line, use getline().

> cout<<"Price"<<endl;
> cin>>price;


If you're inputing by line (which would seem to be the case),
something like:

std::string line ;
std::cin >> line ;
std::istringstream s( line ) ;
s >> price >> std::ws ;
if ( ! s || s.get() != EOF ) {
// error...
}

is preferable. (Of course, you should also check for an error
after reading the line.) It reads a complete line, leaving the
stream is a good state if there is a line, regardless of what it
contains. It then converts the double, eats any trailing white
space, and checks for errors...including extra garbage at the
end of the line. And error or not, you're automatically
synchronized for the next line.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-09-2007
On Nov 8, 7:51 pm, "osmium" <r124c4u...@comcast.net> wrote:

[...]
> See if this thread will solve your problem.


> http://groups.google.com/group/comp....ead/thread/5cc...


Two of the three responses in it were technically wrong. Only
Jon Bell's response is actually usable.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


 
Reply With Quote
 
Tarique
Guest
Posts: n/a
 
      11-09-2007
osmium wrote:
> "Tarique" wrote:
>
>> How can i clear the input buffer before i accept a user input?Further
>> what is the correct way to clear the buffer in case of an *invalid*
>> input.The following naive program will show up the error.
>> any help will be greatly appreciated.


...snip...


> See if this thread will solve your problem.
>
> http://groups.google.com/group/comp....e6cd78834c9602


Thanks .The link along with a few more searches of the group's archive
gave me a fairly good idea.


--
C is quirky, flawed, and an enormous success.
—Dennis Ritchie
 
Reply With Quote
 
Tarique
Guest
Posts: n/a
 
      11-09-2007
Tarique wrote:
> Hello all.
>
> How can i clear the input buffer before i accept a user input?Further
> what is the correct way to clear the buffer in case of an *invalid*
> input.The following naive program will show up the error.
> any help will be greatly appreciated.



...snip...


Found this reply in the group's archive.How good or bad is the method
described below?



#include <iostream>
#include <cstdio>
using namespace std;

void rightTriangle(void);
void rightT2(void);

int main(void)
{
rightTriangle();
rightT2();
return 0;
}

void rightTriangle(void)
{

double base = 0.0, altitude = 0.0, areacalcrt = 0.0;
do
{
cin.clear(); cin.ignore(cin.rdbuf()->in_avail());
cout << "Please enter the base of your triangle\
(Value 1.0 - 15.0): ";

cin >> base;
if (!cin.good()) continue;
if (base < 1.0 || base > 15.0) continue;
cin.ignore(cin.rdbuf()->in_avail()); break;
} while (true);


do
{
cin.clear(); cin.ignore(cin.rdbuf()->in_avail());
cout << "Please enter the height of your triangle\
(Value 1.0 - 15.0): ";
cin >> altitude;
if (!cin.good()) continue;
if (altitude < 1.0 || altitude > 15.0) continue;
cin.ignore(cin.rdbuf()->in_avail()); break;
} while (true);


areacalcrt = (0.5) * base * altitude;
cout << "\nThe area of your right triangle is: " << areacalcrt
<< endl;

}

void rightT2(void)
{
// Buffer for input (Could also use a 'string' type)
static char buf[64];
double base = 0.0, altitude = 0.0, areacalcrt = 0.0;

do
{
cin.clear();

cout << "Please enter the base of your triangle\
(Value 1.0 - 15.0): ";
cin.getline(buf, sizeof buf);

if (!cin.good()) continue;
if (sscanf(buf, "%lf", &base) <= 0) continue;
if (base < 1.0 || base > 15.0) continue;
break;

} while (true);

do
{
cin.clear();

cout << "Please enter the height of your triangle\
(Value 1.0 - 15.0): ";
cin.getline(buf, sizeof buf);

if (!cin.good()) continue;
if (sscanf(buf, "%lf", &altitude) <= 0) continue;
if (altitude < 1.0 || altitude > 15.0) continue;
break;

} while (true);

areacalcrt = (0.5) * base * altitude;
cout << "\nThe area of your right triangle is: " << areacalcrt
<< endl;
}

--
C is quirky, flawed, and an enormous success.
—Dennis Ritchie
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-10-2007
On Nov 9, 10:59 am, Tarique <peo_...@yahoo.com> wrote:
> Tarique wrote:


> > How can i clear the input buffer before i accept a user
> > input?Further what is the correct way to clear the buffer in
> > case of an *invalid* input.The following naive program will
> > show up the error. any help will be greatly appreciated.


> ..snip...


> Found this reply in the group's archive.How good or bad is the
> method described below?


It's probably useless.

[...]
> cin.clear(); cin.ignore(cin.rdbuf()->in_avail());


What is the second statement supposed to do. You ignore a
random amount of data (possibly 0).

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
 
 
Reply

Thread Tools

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to receive keyboard input? FabFreddy ASP .Net 3 11-25-2005 03:18 PM
can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)? santa19992000@yahoo.com C Programming 4 09-09-2005 03:38 AM
Keyboard error or no keyboard present??? Bud Light Computer Support 2 01-22-2005 04:00 AM
Unbuffered keyboard input??? Starbase Commander Perl 1 09-10-2004 11:20 PM
detect keyboard input without reading it? Scott Shaw Perl 1 11-10-2003 04:33 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57