C++ problem

  • Thread starter Thread starter Boogieboo6
  • Start date Start date
  • Views Views 1,221
  • Replies Replies 6

Boogieboo6

@realDonaldTrump
Member
Joined
Jul 30, 2015
Messages
965
Solutions
1
Reaction score
1,319
Trophies
1
Age
25
XP
837
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!
 
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.
 
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,
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