Thread: Need a little help with my code.

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    5

    Need a little help with my code.

    Hye there. Im working on a program, but it seems not working like i wanted to. Below is the code
    Code:
    class Books
    {
    	string isbnNo, description, title, author;
    	float price, discountedprice, discountperc;
    
    public :
    	void set_Data()
    	{
    		cout<<"Enter ISBN \t\t: ";
    		getline(cin,isbnNo);
    
    		cout<<"Enter Title \t\t: ";
    		getline(cin,title);
    		cin.ignore();
    
    		cout<<"Enter Description \t: ";
    		getline(cin,description);
    
    		cout<<"Enter Author's name \t: ";
    		getline(cin,author);
    
    		cin.ignore();
    		cout<<"Enter price \t\t: RM ";
    		cin>>price;
    
    		cout<<"Enter discount (%) \t: ";
    		cin>>discountperc;
    	}
    
    	void calcDiscountedPrice()
    	{
    		discountedprice = price - (discountperc / 100 * price);
    	}
    
    	void print()
    	{
    		cout<<endl;
    		cout<<"ISBN             : "<<isbnNo<<endl;
    		cout<<"Title            : "<<title<<endl;
    		cout<<"Description      : "<<description<<endl;
    		cout<<"Author           : "<<author<<endl;
    		cout<<"Original Price   : RM "<<price<<endl;
    		cout<<"Discounted Price : RM "<<discountedprice<<endl;
    	}
    
    };
    
    void func(Books*);
    
    void main()
    {
    	Books B1;
    	
    	func(&B1);
    }
    
    void func(Books *add)
    {
    	add->set_Data();
    	add->calcDiscountedPrice();
    	add->print();
    }
    The first few lines work well, but until enter price and enter discount, it skip and i can not type in the price and discount. It straightly printed out everything and the original price and discounted price showed sumthing like -1.(numbers)e+(numbers) When i compiled it, it didnt state any errors. So ima lil bit confuse here. Hope someone can help me with this, and so sorry if my question seems stupid. I am new in this C++ programming..
    Thank you..

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Worked fine for me, didn't skip those sections (price/discount).

    What compiler do you have? Is it MS Visual Studio 5/6? If so you could be a known issue with the getline function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> cin.ignore();
    Why is that there? It will ignore the first digit of the price. You should put that after all calls to cin >>, not before, and not after calls to getline.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    Quote Originally Posted by hk_mp5kpdw
    Worked fine for me, didn't skip those sections (price/discount).

    What compiler do you have? Is it MS Visual Studio 5/6? If so you could be a known issue with the getline function.
    Hurmm.. Worked fine for u? Sigh..
    Im using MS Visual C++ 6.0
    does it have something to do with my compiler?
    isnt getline function used for like a string?
    Hurmm..so sorry.. Im clueless..

    Quote Originally Posted by Daved
    >> cin.ignore();
    Why is that there? It will ignore the first digit of the price. You should put that after all calls to cin >>, not before, and not after calls to getline.
    I edited it but still the same output.
    Last edited by paradize; 08-23-2006 at 01:51 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Im using MS Visual C++ 6.0
    Have you applied Service Pack 6 yet?

    Some earlier releases had problems with the first getline call IIRC
    http://www.codeguru.com/forum/showthread.php?t=266029
    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.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    I juz checked out the link, and it seems too much info for a beginner like me. I couldnt understand it. a simple getline function has to fix with all that stuff..Wow.. Well .. n e way...thanx for the link. I'll get into it next time.
    For now, i juz need a simple little help.
    Any other ways?

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. Download and apply the service pack.


    2. Find and open the <string> header file (not the string.h file, but rather the one without any extenstion) in any text editor. On my machine, this file is found in the "Program Files\Microsoft Visual Studio\VC98\Include" directory.


    3. Try to find a section of code that looks like this:
    Code:
    else if (_Tr::eq((_E)_C, _D))
         {_Chg = true;
          _I.rdbuf()->snextc();
         break; }

    4. If that section exists as written above, change it to this:
    Code:
    else if (_Tr::eq((_E)_C, _D))
         {_Chg = true;
          _I.rdbuf()->sbumpc();
         break; }
    5. Save the file and exit.


    6. Recompile/link and run your program.


    7. You should also go here and try to apply the other suggested fixes to several other header files.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    Thanx all.. now it works..
    thank you..

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    What did you expect? This is a programming forum Friend of newbies, regulars and kings alike.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    BTW, consider upgrading your IDE, VC++ 6 is quite old and the Express version 8 (2005) is available for free.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM