Keyboard input (newbie question)

Discussion in 'C++' started by Tarique, Nov 8, 2007.

  1. Tarique

    Tarique Guest

    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;
    }


    --
    C is quirky, flawed, and an enormous success.
    —Dennis Ritchie
    Tarique, Nov 8, 2007
    #1
    1. Advertising

  2. Tarique

    osmium Guest

    "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 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
    osmium, Nov 8, 2007
    #2
    1. Advertising

  3. Tarique

    James Kanze Guest

    On Nov 8, 6:56 pm, Tarique <> 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
    James Kanze, Nov 9, 2007
    #3
  4. Tarique

    James Kanze Guest

    On Nov 8, 7:51 pm, "osmium" <> wrote:

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


    > http://groups.google.com/group/comp.lang.c /browse_thread/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
    James Kanze, Nov 9, 2007
    #4
  5. Tarique

    Tarique Guest

    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...?lnk=gst&q=in_avail() osmium#a5e6cd78834c9602


    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
    Tarique, Nov 9, 2007
    #5
  6. Tarique

    Tarique Guest

    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
    Tarique, Nov 9, 2007
    #6
  7. Tarique

    James Kanze Guest

    On Nov 9, 10:59 am, Tarique <> 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
    James Kanze, Nov 10, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Shaw
    Replies:
    1
    Views:
    817
    J. Romano
    Nov 10, 2003
  2. Replies:
    4
    Views:
    645
    Walter Roberson
    Sep 9, 2005
  3. Mike Barnard
    Replies:
    34
    Views:
    1,012
    Arne Vajhøj
    Jul 20, 2010
  4. Replies:
    6
    Views:
    116
  5. Mad Scientist Jr
    Replies:
    5
    Views:
    375
    Mad Scientist Jr
    Apr 17, 2006
Loading...

Share This Page