C++ problem

Boogieboo6

@realDonaldTrump
OP
Member
Joined
Jul 30, 2015
Messages
965
Trophies
1
Age
23
XP
807
Country
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:
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;
int Menu(int choice);
void addRecords();
void dispRecords();
void dispCity();
void dispZIP();
int main()
{
int choice = 0;
cout << "Welcome to the program!\n";
while (choice != 5)
{
cout << "1.)Add Records\n2.)Display Records\n3.)Display City\n4.)Display ZIP\n5.)Exit\n";
cin >> choice;
cin.ignore();
Menu(choice);
}
}

int Menu(int choice)
{
system("CLS");
switch (choice)
{
case 1: addRecords();
break;
case 2: dispRecords();
break;
case 3: dispCity();
break;
case 4: dispZIP();
break;
case 5:
{
return 0;
}
}
}

void addRecords()
{
ofstream outFile;
outFile.open("Chapter14Lab26.txt");
if (outFile.is_open())
{
string city = " ";
string zip = " ";
while (zip != "0")
{
cout << "Enter a ZIP code. 0 to end.\n";
getline(cin, zip);
if (zip != "0")
{
outFile << zip << "#";
cout << "Enter a city\n";
getline(cin, city);
transform(city.begin(), city.end(), city.begin(), toupper);
outFile << city << endl;
}
}
outFile.close();
}
else
cout << "Chapter14Lab26.txt could not be opened. . .\n";
}

void dispRecords()
{
string line = " ";
ifstream inFile;
inFile.open("Chapter14Lab26.txt");
if (inFile.is_open())
{
while (!inFile.eof())
{
getline(inFile, line);
cout << line << endl;
}
inFile.close();
}
else
cout << "Chapter14Lab26.txt could not be opened . . .\n";
}

void dispCity()
{
bool found = false;
string line = " ";
string city = " ";
string zip = " ";
string compare = " ";
ifstream inFile;
inFile.open("Chapter14Lab26.txt");
if (inFile.is_open())
{
cout << "Enter a ZIP code: ";
getline(cin, zip);
while (!inFile.eof() && found == false)
{
getline(inFile, line);
compare = line.substr(0, 5);
if (compare == zip)
{
cout << "ZIP: " << zip << " City: " << line.substr(6) << endl;
found = true;
}
}
if (found == false)
{
cout << "No city found for ZIP " << zip << endl;
}
inFile.close();
}
else
cout << "Chapter14Lab26.txt could not be opened . . .\n";
}

void dispZIP()
{
bool found = false;
string line = " ";
string city = " ";
string zip = " ";
string compare = " ";
ifstream inFile;
inFile.open("Chapter14Lab26.txt");
if (inFile.is_open())
{
cout << "Enter a city: ";
getline(cin, city);
transform(city.begin(), city.end(), city.begin(), toupper);
while (!inFile.eof() && found == false)
{
getline(inFile, line);
compare = line.substr(6);
if (compare == city)
{
cout << "ZIP: " << line.substr(0,5) << " City: " << city << endl;
found = true;
}
}
if (found == false)
{
cout << "No ZIP found for city " << city << endl;
}
inFile.close();
}
else
cout << "Chapter14Lab26.txt could not be opened . . .\n";
}
Why doesn't this work? Thanks for the help!
 

Erfg1

Well-Known Member
Member
Joined
Jan 3, 2016
Messages
105
Trophies
0
Age
37
XP
509
Country
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.
 

Boogieboo6

@realDonaldTrump
OP
Member
Joined
Jul 30, 2015
Messages
965
Trophies
1
Age
23
XP
807
Country
United States

nIxx

Well-Known Member
Member
Joined
Sep 30, 2007
Messages
1,544
Trophies
0
Location
Germany
Website
Visit site
XP
337
Country
Gambia, The
A requirement of the exercise is to use files.
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,

Boogieboo6

@realDonaldTrump
OP
Member
Joined
Jul 30, 2015
Messages
965
Trophies
1
Age
23
XP
807
Country
United States
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.
Or maybe even better just use soemthing like
Code:
 while (getline(inFile,line)) { //only while there is something to read

}
Awesome, I'll try that out when I get home today. Thanks!
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
  • K3Nv2 @ K3Nv2:
    They should've just made it a movie at 50 minutes
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    No from Paramount Plus or whatever it is
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    Psi now has access to every streaming and cable channel out
  • K3Nv2 @ K3Nv2:
    Eh I'd rather just download and delete is it already up on paramount?
  • Psionic Roshambo @ Psionic Roshambo:
    Yeah it's on now
  • Xdqwerty @ Xdqwerty:
    @Psionic Roshambo, why are most new south park episodes half hour specials?
    +1
  • K3Nv2 @ K3Nv2:
    @Psionic Roshambo, let me get a Netflix account I've always been nice to you
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    I made a honey pepper glazed turkey breast for dinner turned out pretty bomb
  • K3Nv2 @ K3Nv2:
    We can Netflix and chill
  • Xdqwerty @ Xdqwerty:
    Nvm not half hour, but hour long
  • Xdqwerty @ Xdqwerty:
    Normal south park episodes are already half hour
  • Psionic Roshambo @ Psionic Roshambo:
    Got 2 free Xumo boxes they work pretty good
  • K3Nv2 @ K3Nv2:
    I'm happy to get anything most series don't make it pass 6 seasons
  • Psionic Roshambo @ Psionic Roshambo:
    Except Stars and Encore those are being a bitch lol
  • K3Nv2 @ K3Nv2:
    I may consider that onn pro box finally a media box with type A ;O
    +1
  • Xdqwerty @ Xdqwerty:
    @K3Nv2, cuz the companies don't want em to
  • K3Nv2 @ K3Nv2:
    No it's revenue and demand south park could argue has a bigger audience than family guy about the same
    +1
  • K3Nv2 @ K3Nv2:
    Family guy is worth 300mill south parks worth 1Bill according to the interwebs
  • Xdqwerty @ Xdqwerty:
    @K3Nv2, probably cuz family guy is more disliked compared to south park
  • K3Nv2 @ K3Nv2:
    It just does the same formula south park can press buttons without going overboard
  • Xdqwerty @ Xdqwerty:
    And bc most of the family guy budget is spent on the voice actors rather than on the animation
  • K3Nv2 @ K3Nv2:
    Southpark could spend 30k on a animated dick and people would laugh at it
    +2
  • K3Nv2 @ K3Nv2:
    lol one prescription to Lizzo
  • Xdqwerty @ Xdqwerty:
    who is lizzo?
  • Xdqwerty @ Xdqwerty:
    yawn
    Xdqwerty @ Xdqwerty: yawn