Large code Simple Question need help

This is a discussion on Large code Simple Question need help within the C++ Programming forums, part of the General Programming Boards category; I have this program that is a publishing company. Im doing the programs from my book. Everything is working fine ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    230

    Large code Simple Question need help

    I have this program that is a publishing company. Im doing the programs from my book. Everything is working fine except one thing. When the user inputs a title or tape name longer than one letter it goes crazy and basically crashes. heres the line i think needs help the char title part

    Code:
    class Publication  //base class
    {
    	private:
    	float price;           //price of book and tape
    	char title;          //title of book or tape  -- i think this part needs help
    	
    	public:
    	
    	Publication () : price(0)  // construct price to 0,
    	{}                        
    	
        
        void getdata()            // get user info
        {
        cout << "Enter title "; 
        cin>>title;
        
        cout << "Enter price "; 
        cin>>price;
        }

    -- If you need the whole program let me know which you probably will need. I just dont know how to post attatchments on this thing.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    while (cin.fail())
    {
        cin.clear();
        while (cin.rdbuf()->in_avail() > 0) cin.get();
        // Notify the user of an error and get input again
    }
    Put that underneath all your attempts to get the input. Itt'l flush the input and allow you to continue.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You are correct that is the problem. You say

    char title;

    What this means is that you are allowed to store 1 byte ( a character ) of information there. You need more than one character! What you want is a series of bytes. You need an array. So change the line to something like this:

    char title[ 256 ];

    Now this means you can store up to 256 values in title. You also will need to read in the input from user differently now. No longer can you use cin >> title. You need to use

    cin.getline( title, 256, '\0' );

    If you need furhter assistance let me know.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Mr. Wizard thanks for the help that really should have worked but when i do it my screen crashes maybe its something else in my code. Its kind of long like four headers, broke it up so its easy to read, and main cpp file . Ill check it over again. i would give you entire code but dont know how to attatch

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by gamer4life687
    Mr. Wizard thanks for the help that really should have worked but when i do it my screen crashes maybe its something else in my code. Its kind of long like four headers, broke it up so its easy to read, and main cpp file . Ill check it over again. i would give you entire code but dont know how to attatch
    Let me know if you pin point the problem.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    ok ill let you know. If you want a copy of the code let me know how to attatch on this thing and ill give u the .zip. However i probalby wont get on until tomorrow another 12 hours. Talk to you later.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  2. Hopefully simple question, input streams
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2006, 01:59 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. simple input and string manipulation question
    By Stig in forum C Programming
    Replies: 1
    Last Post: 12-15-2001, 01:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21