1. Boogieboo6
    OP

    Boogieboo6 @realDonaldTrump

    Member
    942
    1,276
    Jul 30, 2015
    United States
    I have to make a project for school. The program is supposed to let me write zip codes and cities to a file, display the contents of the file, search for a city via zip code, and search for a zip code via city. All of them work, except for search for a zip code via city. If I put in data that's in the file, it works. But if I put in data that's not in the file, like Cihcago rather than Chicago, the program breaks. Here's my code:
    Code
    Why doesn't this work? Thanks for the help!
     
  2. Erfg1

    Erfg1 Advanced Member

    Newcomer
    50
    21
    Jan 3, 2016
    United States
    If you're using Visual Studio you can Debug this easily. Run in Debug mode and when it crashes you should see exactly where it crashed at and what information was given to those variables.
     
  3. nIxx

    nIxx GBAtemp Advanced Maniac

    Member
    1,528
    78
    Sep 30, 2007
    Gambia, The
    Germany
  4. Boogieboo6
    OP

    Boogieboo6 @realDonaldTrump

    Member
    942
    1,276
    Jul 30, 2015
    United States
    I think I did that. A string went out of range or something.
    A requirement of the exercise is to use files.
     
  5. nIxx

    nIxx GBAtemp Advanced Maniac

    Member
    1,528
    78
    Sep 30, 2007
    Gambia, The
    Germany
    Okay i see ^^.

    Anyway check if you lines are empty. If you add something you alway use endl at the end that creates an empty line at the end of the Textfile ;)
    If you then try to use substring on it there is nothing there btw. it goes out of range.
    That would work
    Code:
    getline(inFile, line);
    if(line.empty())
        continue;
    or you can use try and catch to do with the exception whatever you want.
     
    Last edited by nIxx, Apr 24, 2017
  6. Boogieboo6
    OP

    Boogieboo6 @realDonaldTrump

    Member
    942
    1,276
    Jul 30, 2015
    United States
    Awesome, I'll try that out when I get home today. Thanks!
     
  7. nIxx

    nIxx GBAtemp Advanced Maniac

    Member
    1,528
    78
    Sep 30, 2007
    Gambia, The
    Germany
    I removed the while (getline(inFile,line)) stuff since you will have the same issue with the last line just so you know ;)
     
    Boogieboo6 likes this.