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:
    I'll reformat and have a 3tb raid0 m. 2 at least
    +1
  • K3Nv2 @ K3Nv2:
    Lmao that sold out fast
    +1
  • Veho @ Veho:
    Yeet the cat.
    +1
  • K3Nv2 @ K3Nv2:
    Good idea
    +1
  • The Real Jdbye @ The Real Jdbye:
    i thought everybody knew cocktails are like 75% ice
  • Veho @ Veho:
    Yeah but not like this.
  • Veho @ Veho:
    It's not like they're complaining that their Slurpee is 99% ice or something, but if the cocktail calls for "shot of vodka, shot of vermouth, shot of gin, shot of Campari, three shots of juice, squirt of lemon" and ends up being a thimbleful of booze, that's a problem.
  • The Real Jdbye @ The Real Jdbye:
    the funny thing is cocktails in norway are only allowed to have 1 20ml shot of booze
  • The Real Jdbye @ The Real Jdbye:
    so..... yeah
  • The Real Jdbye @ The Real Jdbye:
    we're used to only having a thimbleful of booze
  • Veho @ Veho:
    Booo.
  • The Real Jdbye @ The Real Jdbye:
    same thing if you want whisky on the rocks or something, you can't get a double
  • The Real Jdbye @ The Real Jdbye:
    but you could buy as many shots of whisky (or anything else) as you want and ask for a glass of ice and pour them in
  • The Real Jdbye @ The Real Jdbye:
    it's dumb
  • Veho @ Veho:
    Maybe.
  • Veho @ Veho:
    There was a comparison of the number of Ibuprofen poisonings before and after they limited the maximum dosage per box or per pill (i'll look that up). No limit on the number of boxes you can still buy as many as you want, so people argued it was pointless.
  • Veho @ Veho:
    But the number of (accidental) poisonings dropped because drinking an entire package of ibuprofen pills went from "I need a new liver" to "I need a new box of Ibuprofen".
  • Veho @ Veho:
    Here we have ketoprofen that used to be prescription-only because of the risk of toxic dosages, but then they halved the dose per pill and sell them in bottles of six pills apiece instead of twenty and it doesn't need a prescription any more. Yes you can buy more than one bottle but people simply don't.
  • Psionic Roshambo @ Psionic Roshambo:
    Usually accidentally overdose of ibuprofen here is from people taking like cold medicine then ibuprofen for a headache and the combination is over what they need
    Veho @ Veho: https://imgur.com/gallery/QQkYnQu