Thread: Why does it crash?

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Why does it crash?

    when i ran this, it exits after i entered input and press enter, but it's supposed to move to new line for more input.
    only when i type end-of-file should it exit.
    can anyone help?

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::ios;
    using std::cerr;
    using std::endl;
    
    #include <fstream>
    
    using std::ofstream;
    
    #include <cstdlib>
    
    int main()
    {
       // ofstream constructor opens file
       ofstream outClientFile( "clients.dat", ios::out );
    
       // exit program if unable to create file
       if ( !outClientFile ) {  // overloaded ! operator
          cerr << "File could not be opened" << endl;
          exit( 1 );
       }
    
       cout << "Enter the client number, firstname, surname, street name,"
            << "street number, D.O.B," << '\n' << "and allowance" << endl
            << "Enter end-of-file to end input.\n? ";
    
       int      clientNumber;
       char     firstName[ 30 ];
       char     surName [ 30 ];
       char     streetName[ 30 ];
       int      streetNumber;
       int      dateofBirth;
       double   allowance;
    
       // read account, name and balance from cin, then place in file
       while ( cin >> clientNumber >> firstName >> surName >> streetName >>
       streetNumber >> dateofBirth >> allowance ) {
          outClientFile << clientNumber << ' ' << firstName << ' ' << surName
                        << ' ' << streetName << ' ' << streetNumber << ' '
                        << dateofBirth << ' ' << allowance << endl;
          cout << "? ";
    
       } // end while
    
       return 0;  // ofstream destructor closes file
    
    } // end main

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    What input are you giving it? I inputted:
    Code:
    1 Jane Doe Elm 21 05271980 5.00
    2 Joe Schmoe Eighth 8 12081957 42.08
    Ctrl-D
    and it worked perfectly for me.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    i inputted something like this -

    239 610 456 - John - Doe - White Lane - 15 - 01/04/80 - $152.67

    so, client id - first name - last name - street name - street number - date of birth - weekly student allowance

    when i enterred the details and press enter, it just exits, what part is causing it to do that?

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Originally posted by Guti14
    i inputted something like this -

    239 610 456 - John - Doe - White Lane - 15 - 01/04/80 - $152.67

    so, client id - first name - last name - street name - street number - date of birth - weekly student allowance

    when i enterred the details and press enter, it just exits, what part is causing it to do that?
    Maybe it's the spaces in the client id you entered, I'm not sure though.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    maybe it's my compiler. sometimes i can enter any character and make keep entering new lines, and the next time i just enter a name, press enter, and it exits. really frustrating, but i don't know why it does this.

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What compiler are you using?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    161
    You have to remember that input streams parse on white space. Here are the problems with your input:

    239 610 456 - John - Doe - White Lane - 15 - 01/04/80 - $152.67
    (I'm assuming for the sake of sanity that your hyphens represent spaces)

    1. "239 610 456" is separated by whitespace and will be parsed as three tokens
    2. "White Lane" is separated by whitespace and will be parsed as two tokens
    3. "01/04/80" is not an integer
    4. "$152.67" is not a double

    You'll have to either enter the input as jlou did or grab a whole line and parse it yourself.

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    well see the declaration of your variables:

    int with spaces -> not good
    use array of integers ( int number[2]; // place for 3 numbers

    or just use char //remember to read about --=reading embedded blanks=--

    float variable takes . // exp. price 1000.45

    just read a bit more about variable declaration (what kind, how much memory they use and so on)

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    assuming that line is exactly as it would be in a data file:

    239 610 456 - John - Doe - White Lane - 15 - 01/04/80 - $152.67
    use some code like this:
    Code:
    ...
    int id1,id2,id3;
    char*fname=new char[25];
    char*lname=new char[25];
    char*address=new char[256];
    int strtnum;
    int month,date,year;
    float price;
    char null;
    
    infile>>id1>>id2>>id3>>null>>fname>>null>>lname>>null;
    infile.getline(address,256,'-');
    infile>>strtnum>>null>>month>>null>>date>>null>>year>>null>>null>>price;
    
    ...
    delete[]fname;
    delete[]lname;
    delete[]address;
    ...
    something to that effect...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    First, if you can use strings, I would use strings. They make error checking a heck of a lot easier.

    Separate from that, you have to decide whether you want to assume that the user will enter the input perfectly, or whether you want to try to code to allow a variation in user input.

    What I mean is, you decided to make your street name "White Lane" which is really two words. Do you want to force the user to always put two words for the street, or do you want to allow the user to type whatever they want? If the answer is that you want to be able to handle one word, two words, etc., then you can't use the input method you are using, because it is based on whitespace separated tokens.

    If you want to continue to separate the input with '-', then major_small has a good idea to use getline with '-' as the separator. You could do this for each input.

    To use getline with C-style strings as you are currently using try:

    char inputLine[256];
    std::cin.getline(inputLine, 256, '-');


    If you use std::string, try:

    std::string inputLine;
    std::getline(std::cin, inputLine, '-');

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that's how i do input when i have a user enter a string or read it in from a file... alternatively, you can have them seperate it on lines, like this:

    Code:
    239 610 456
    John  Doe
    White Lane
    15
    01/04/80
    $152.67
    and that way you can seperate by return chars ('\n').
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    I think you should allow one thing at a time, and after each thing is inputted, do some kind of validation checks on it to make sure the user entered the correct format you want.
    IMO, assuming a user will enter what you tell them to enter 100% of the time will always lead to problems down the road. Provide some kind of error checking in your code, and I'm betting your program will no longer crash. And yeah...you should use std::strings for user input to provide more flexibility in your code.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This thread is almost seven years old.

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Oh sorry...don't always check the dates on everything.
    I noticed that someone was viewing this thread in the Who's Online list, so I went here, read it, and responded.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Yeah, I figured it was something like that. You've got to be careful when going by what people are viewing, since bots and people doing searches can find some pretty old topics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Hooking Crash?
    By Elysia in forum Windows Programming
    Replies: 9
    Last Post: 03-15-2008, 01:13 PM
  3. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  4. Dynamic array sizing causes crash
    By Mithoric in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2003, 07:46 AM
  5. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM