Premature stoppage type thingy

This is a discussion on Premature stoppage type thingy within the C++ Programming forums, part of the General Programming Boards category; this is my program (isn't it nice ) the problem i'm having is that the program stops after "search" is ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    3

    Question Premature stoppage type thingy

    this is my program (isn't it nice ) the problem i'm having is that the program stops after "search" is selected but there should be another option.....does anyone know why?



    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>

    char choice,type,choose,decide;
    int e,s,c,p,a;

    //-----------------------------------
    //base and derived classes
    class books {
    public:
    struct database {
    char title[256];
    char author[256];
    char isbn[11];
    char publishername[256];
    char cdrom[4];
    char yearofpub[5];
    char price[5];
    }np[20]; // end of struct
    //..........................
    void search(int);
    void enter(int);
    void remove(void);
    void first(void);
    void author(void);
    void pub(void);


    };
    //------------------------------------



    class store: public books{
    public:
    void save(void);
    void load(void);
    };

    //------------------------------------
    int main(int) {
    books bdata;
    store storage;
    storage.first();

    return(0);
    };

    //------------------------------------

    void books::first(void) {
    cout<<"-----------------------------------------------------------\n";
    cout<<"| |\n";
    cout<<"| Welcome to the Book |\n";
    cout<<"| Database |\n";
    cout<<"| |\n";
    cout<<"-----------------------------------------------------------\n\n\n\n";

    cout<<" What would you like to do? \n";
    cout<<" Select:\n\n";
    cout<<" 1 To add a book\n";
    cout<<" 2 To search for a book\n";
    cout<<" 3 To remove a book\n";
    cout<<" 4 Clear book database\n";
    cout<<" 5 Show all books\n";
    choice=getchar();
    choice=toupper(choice);
    switch(choice) {

    case '1': {
    cout<<"you selected 'Add'\n";
    enter(e);
    break;}

    case '2': {
    cout<<"you selected 'Search'\n";
    search(s);
    break;}


    case '3': {
    cout<<"you selected 'Remove'\n";
    break;}


    case '4': {
    cout<<"you selected 'Clear'\n";
    break;}

    case '5': {
    cout<<"you selected 'show all'\n";
    break;}

    };
    };

    //---------------------------------------
    //member functions
    void books::enter(int e) {

    cout<<"Enter the book title: ";
    cin>>(np[e].title);
    cout<<"Enter the author: ";
    cin>>(np[e].author);
    cout<<"Enter the ISBN number: ";
    cin>>(np[e].isbn);
    cout<<"Enter the publisher name: ";
    cin>>(np[e].publishername);
    cout<<"Is there a CDROM included (yes or no): ";
    cin>>(np[e].cdrom);
    cout<<"Year published: ";
    cin>>(np[e].yearofpub);
    cout<<"Enter price - exclude sign: ";
    cin>>(np[e].price);

    };

    //------------------------------------------

    void books::search(int s) {
    cout<<"-----------------------------------------------------------\n";
    cout<<"| |\n";
    cout<<"| Welcome to the Book |\n";
    cout<<"| Database |\n";
    cout<<"| |\n";
    cout<<"-----------------------------------------------------------\n\n\n\n";

    cout<<" What would you like to search by? \n";
    cout<<" Select:\n\n";
    cout<<" 1 Author.\n";
    cout<<" 2 Publisher\n";

    choose=getchar();
    choose=toupper(choose);
    switch(choose) {

    case 'A': {
    cout<<"you selected 'Search by Author'\n";
    author();
    break;}

    case 'B': {
    cout<<"you selected 'Search by Publisher'\n";
    pub();
    break;}


    };
    };

    void books::author() {

    cout<<"Enter the book title: ";
    cin>>(np[a].title);
    cout<<"Enter the author: ";
    cin>>(np[a].author);
    cout<<"Enter the ISBN number: ";
    cin>>(np[a].isbn);
    cout<<"Enter the publisher name: ";
    cin>>(np[a].publishername);
    cout<<"Is there a CDROM included (yes or no): ";
    cin>>(np[a].cdrom);
    cout<<"Year published: ";
    cin>>(np[a].yearofpub);
    cout<<"Enter price - exclude sign: ";
    cin>>(np[a].price);

    };

    void books:ub() {

    cout<<"Enter the book title: ";
    cin>>(np[p].title);
    cout<<"Enter the author: ";
    cin>>(np[p].author);
    cout<<"Enter the ISBN number: ";
    cin>>(np[p].isbn);
    cout<<"Enter the publisher name: ";
    cin>>(np[p].publishername);
    cout<<"Is there a CDROM included (yes or no): ";
    cin>>(np[p].cdrom);
    cout<<"Year published: ";
    cin>>(np[p].yearofpub);
    cout<<"Enter price - exclude sign: ";
    cin>>(np[p].price);

    };

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Welcome to the boards.

    First I'd like to say that when posting code, it would be very nice if you could use code tags, it helps people trying to help you out a ton and doesn't make our eyes hurt looking at the code.

    This should fix your problem though:

    instead of using the getchar function, replace it with something like this:

    Code:
     cout.flush();
     cin >> choice;
    that will make sure that everything in cout is flushed properly and then you will get a single letter input from the stream, also. If you want to make sure that people don't put multiple letters as options and overload the stream, use cin.ignore to ignore the extra letters

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Little Array Difficulty
    By G4B3 in forum C Programming
    Replies: 16
    Last Post: 03-19-2008, 12:59 AM
  4. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21