473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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;
  10.  
  11.             case 90:
  12.                  if (lastChosenBook == -1) {
  13.                     cout << "Choose a book first!" << endl;
  14.                  } else {
  15.  
  16.                         int n;//THERE'S A FLAW IN MY CODE HERE, A CHAR CREATES AN INFINITE LOOP, AN INTEGER DOES NOT???
  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;
  23.  
  24.                  }
  25.                  break;
  26.  
  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
2 1122
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
JosAH
11,448 Expert 8TB
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
Aug 12 '07 #3

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

Similar topics

18
by: dover | last post by:
Instantiation of classes is to create objects in areas of memory. As a side effect of that object creation, the constructor is called. Three steps can be identified in an instantiation of a class:...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.