0
#include<iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int optionChosen=0;

struct bookStruct
{
    string bookTitle;
    int bookPageN;
    int bookReview;
    float bookPrice;
};

const int MAX_BOOKS=10;

int main()
{
    bookStruct books[10]={};


    do
    {
        cout << "Please Select an Option from the Menu:\n\n" << endl;
        cout << "1. Display List of Books.\n" << "2. Find Book.\n" << "3. Add New Book.\n";
        cout << "4. Delete Book.\n"<<"5. Save List to File.\n"<<"6. Load List from File.\n";
        cout << "7. Sort List.\n"<<"8. Exit.\n\n";
        cin >> optionChosen;
        switch(optionChosen)
        {
            case 1:
            {
                for (int i=0;i<MAX_BOOKS;i++)
                {
                    if(books[i].bookTitle!="\0")
                    {
                    cout << "Book Title: " << books[i].bookTitle << endl;
                    cout << "Total Pages: " << books[i].bookPageN << endl;
                    cout << "Book Review: " << books[i].bookReview << endl;
                    cout << "Book Price: " << books[i].bookPrice<< "\n\n" << endl;
                    }
                }
                break;
            }
        case 2:
        {

        }
        case 3:
        {
            for(int i=0;i<MAX_BOOKS;i++)
            {
                if(books[i].bookTitle=="\0")
                {
                cout << "\nPlease Enter the Title: ";
                cin >> books[i].bookTitle ;
                cout << "\nPlease Enter Total Number of Pages: ";
                cin >> books[i].bookPageN ;
                cout << "\nPlease Enter Rating (stars): ";
                cin >> books[i].bookReview ;
                cout << "\nPlease Enter Price: ";
                cin >> books[i].bookPrice;
                cout << "\n\nBook Added.\n\n";
                break;
                }
            }break;

        }
        case 4:
        {

        }
        case 5:
        {

        }
        case 6:
        {

        }
        case 7:
        {

        }
        default:
        {
            if(optionChosen!=8)
            {
                cout << "Wrong Input Chosen\n";
                break;
            }
        }

        }



    }
    while(optionChosen<=8);
    return 0;
}

Here is my code. 2 Question.....1. When I press anything that is not a number (a,b,c,abc) as an option, the program executes an infinite loop (maybe) and doesnt stop. Q#2. When I am adding a book, if I use space, the code does the same thing (executes possible infinite loop) and never stops. What am I doing wrong here?

  • BTW, the cases that are Empty will be used later, just telling you so you dont wonder what they are for. –  Feb 05 '13 at 02:47
  • Check the result of any input operation you do. – chris Feb 05 '13 at 02:48
  • @chris I didnt quite get what you said? –  Feb 05 '13 at 02:50
  • 1
    The return value can be used to check for success. These might help: http://www.parashift.com/c++-faq/istream-and-ignore.html – chris Feb 05 '13 at 02:51
  • Side note: compiler does not "execute infinite loop" - it compile your code into executable and than you run it... – Alexei Levenkov Feb 05 '13 at 02:55
  • @AlexeiLevenkov I am saying the program enters an infinite loop. –  Feb 05 '13 at 02:59
  • [Here is a duplicate question](http://stackoverflow.com/questions/257091/how-do-i-flush-the-cin-buffer) – David D Feb 05 '13 at 03:01
  • Mokammel Hossain Sanju, I don't want to sound rude, but have you read your question/title? – Alexei Levenkov Feb 05 '13 at 03:01
  • @AlexeiLevenkov oh sorry, my bad, it should have been why does the compiler enters in an infinite loop. Sorry about that. –  Feb 05 '13 at 03:35

2 Answers2

0

The first one is easy. When you cin >> some_int; and there's no int in the input stream, it simply fails and leaves the input stream in its current state.

That means the the next time you go back to get another int, it will still have the non-integer ready for reading.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • ok, I get what you are saying so this is not a used error rather a deadlock were nothing can be done with the basic operations so the used must input an Integer, am I right? –  Feb 05 '13 at 02:53
  • ok, I get what you are saying so this is not a used error rather a deadlock were nothing can be done with the basic operations so the used must input an Integer, am I right? –  Feb 05 '13 at 02:59
  • @Mokammel, it's not really deadlock, all that happens is that the input stream containing `a`, and the int variable you're trying to `>>` to, are left untouched. When you get round to `>>`ing to a string, it will pick the `a` up. More than likely it seems like an infinite loop but it's really a sequence issue. – paxdiablo Feb 05 '13 at 03:03
0

As paxdiablo mentioned, the non-integer inputs are still in the input stream. You need to call cin.ignore() to extract and discard the invalid characters.

Shawnone
  • 850
  • 5
  • 17