Keyboard input (newbie question)

T

Tarique

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 Book:public Publication
{
private:
int page_count;
public:
void getdata(void)
{
Publication::getdata();
cout<<"No of pages"<<endl;
cin>>page_count;
}

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

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

void putdata(void)
{
Publication::putdata();
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;
}
 
O

osmium

Tarique said:
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 Book:public Publication
{
private:
int page_count;
public:
void getdata(void)
{
Publication::getdata();
cout<<"No of pages"<<endl;
cin>>page_count;
}

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

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

void putdata(void)
{
Publication::putdata();
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...?lnk=gst&q=in_avail()+osmium#a5e6cd78834c9602
 
J

James Kanze

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.
 
T

Tarique

Tarique said:
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;
}
 
J

James Kanze

Tarique wrote:

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top