Classes and constructors/anyone understand them first timer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joeschnell
    New Member
    • Apr 2007
    • 47

    Classes and constructors/anyone understand them first timer

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

    Code:
    				showItem(book[choice-1]);   //displays the array address user choses less one
    				break;
    			case 0:
    				cout << "Here are all the books available :" << endl;
    				for (int i=0; i < 8; i++) //my array loop, c++ array elements begin at zero!
                                    {
    				   showItem(book[i]);    //displays memory allocation book[i] 0-7
    				}
    				break;
    
    			case 90:
                     if (lastChosenBook == -1) {
                        cout << "Choose a book first!" << endl;
                     } else {
                        
                            int n;//THERE'S A FLAW IN MY CODE HERE, A CHAR CREATES AN INFINITE LOOP, AN INTEGER DOES NOT???
                            cout << "Enter the number of books and then hit 'Enter': ";
                            cin >> n;
                            while(book[lastChosenBook-1].buy(n)==-1) {
                                 cout << "There are only " << book[lastChosenBook-1].getAmount() << "books available" <<endl;    
                            }
                            cout << "You order is accepted; Transmit $" <<  fixed << setprecision(2) << book[lastChosenBook-1].getPrice()*1.085 << " to our bank account XXX-XXXXX-XXXX" << endl;
                       
                     }
                     break;
                     
                case 99: 
    				exit = true;	      //boolean condition met
    				break;
    			default:
    				cout << "Incorrect value entered; please follow directions explicitly," <<'\n'; ;
                    cout << "this is not a crystal ball!!!!!" << endl; 
                    cout<< "Now hit 'Enter' and try again!" << endl;
    	    }
    	} 
                            while (!exit);
  • joeschnell
    New Member
    • Apr 2007
    • 47

    #2
    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

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      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,

      Jos

      Comment

      Working...