By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

Classes and constructors/anyone understand them first timer

P: 47
Let me post my code I'm having a problem with first and I'll comment following

Expand|Select|Wrap|Line Numbers
  1.                 showItem(book[choice-1]);   //displays the array address user choses less one
  2.                 break;
  3.             case 0:
  4.                 cout << "Here are all the books available :" << endl;
  5.                 for (int i=0; i < 8; i++) //my array loop, c++ array elements begin at zero!
  6.                                 {
  7.                    showItem(book[i]);    //displays memory allocation book[i] 0-7
  8.                 }
  9.                 break;
  11.             case 90:
  12.                  if (lastChosenBook == -1) {
  13.                     cout << "Choose a book first!" << endl;
  14.                  } else {
  17.                         cout << "Enter the number of books and then hit 'Enter': ";
  18.                         cin >> n;
  19.                         while(book[lastChosenBook-1].buy(n)==-1) {
  20.                              cout << "There are only " << book[lastChosenBook-1].getAmount() << "books available" <<endl;    
  21.                         }
  22.                         cout << "You order is accepted; Transmit $" <<  fixed << setprecision(2) << book[lastChosenBook-1].getPrice()*1.085 << " to our bank account XXX-XXXXX-XXXX" << endl;
  24.                  }
  25.                  break;
  27.             case 99: 
  28.                 exit = true;          //boolean condition met
  29.                 break;
  30.             default:
  31.                 cout << "Incorrect value entered; please follow directions explicitly," <<'\n'; ;
  32.                 cout << "this is not a crystal ball!!!!!" << endl; 
  33.                 cout<< "Now hit 'Enter' and try again!" << endl;
  34.         }
  35.     } 
  36.                         while (!exit); 
Aug 12 '07 #1
Share this Question
Share on Google+
2 Replies

P: 47
At line 17 in this snippet of my code if the user enters an integer, well it brings up the total for one book only and should price out for number of units, but I'll fix that once I rid it of the infinite loop that occurs. If the user enters a char character the program goes into an infinite loop and my
cin >> n; should not bring that about. Does anyone see where my err is? I've been staring at this thing for a long time now. The program executes, displays my menu, brings up the choice from my switch, but it don't compute!
Thanks Joe
Aug 12 '07 #2

Expert 10K+
P: 11,448
Note that when you do 'cin >> n' where n is an int, the istream 'cin' tries to
interpret the characters from the the input stream. When it fails, e.g. if you
typed "foo" instead of a number, nothing is assigned to variable 'n' but just
the 'failbit' of the istream is set and "foo" remains in the input stream.

If 'n' has type char just a single character is read by cin and assigned to variable
'n', but if you use that variable as an int (for your index value) you get extremely
strange results, e.g. if you typed "123" just the first character is read and variable
'n' is assigned the value 49 (the ASCII code for '1').

kind regards,

Aug 12 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.