1

I'm having troubles opening the my text file in this bit of code. Am I doing it the correct way? Just started C++ this week.

I'm having troubles writing to the output file now. The only output I'm getting is this. libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion (lldb)

Thanks in advance guys.

Here is my employeesIn.txt

<123>,<John>,<Brown>,<125 Prarie Street>,<Staunton>,<IL>,<62088>
<124>,<Matt>,<Larson>,<126 Hudson Road>,<Edwardsville>,<IL>,<62025>
<125>,<Joe>,<Baratta>,<1542 Elizabeth Road>,<Highland>,<IL>,<62088>
<126>,<Kristin>,<Killebrew>,<123 Prewitt Drive>,<Alton>,<IL>,<62026>
<127>,<Tyrone>,<Meyer>,<street>,<999 Orchard Lane>,<Livingston>,<62088>

And here is my code

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct Person {
string first;
string last;

};

struct Address {
string street;
string city;
string state;
string zipcode;
};

struct Employee {
Person name;
Address homeAddress;
int eid;
};

void readEmployee(istream& in, Employee& e);
void displayEmployee(ostream& out, const Employee& e);

int main(int argc, const char * argv[])
{
Employee e[50];

ifstream fin;
ofstream fout;

fin.open("employeesIn.txt");

if (!fin.is_open()) {
    cerr << "Error opening employeesIn.txt for reading." << endl;
    exit(1);
}
fout.open("employeesOut.txt");
if (!fout.is_open()) {
    cerr << "Error opening employeesOut.txt for writing." << endl;
    exit(1);
}
int EmployeePopulation = 0;
readEmployee(fin, e[EmployeePopulation]);
while (!fin.eof()) {
    EmployeePopulation++;
    readEmployee(fin, e[EmployeePopulation]);
}
fin.close();
for (int i = 0; i <= EmployeePopulation - 1; i++) {
    displayEmployee(fout, e[i]);
}

fout.close();

cout << endl;

return 0;
}

void readEmployee(istream& in, Employee& e)
{
string eidText;
if ( getline(in, eidText, ',') ) {
    e.eid = stoi(eidText);

    getline(in, e.name.first, ',');
    getline(in, e.name.last, ',');

    getline(in, e.homeAddress.street, ',');
    getline(in, e.homeAddress.city, ',');
    getline(in, e.homeAddress.state, ',');

    string zipcodeText;
    getline(in, zipcodeText, ',');
    e.homeAddress.zipcode = stoi(zipcodeText);
}


}
void displayEmployee(ostream& out, const Employee& e)
{
out << "Customer Record: " << e.eid
<< endl
<< "Name: " << e.name.first << " " << e.name.last
<< endl
<< "Home address: " << e.homeAddress.street
<< endl
<< e.homeAddress.city << ", " << e.homeAddress.state << " " << e.homeAddress.zipcode
<< endl
<< endl
<< endl;
}
MatthewTingle
  • 355
  • 2
  • 3
  • 12
  • Your error message explains what is wrong. Your program cannot open given file. Check out if file "employeesIn.txt" and your binary file are in the same directory. – Dakorn Aug 24 '14 at 23:40
  • I have it in both folders, so I'm not sure what is going wrong. – MatthewTingle Aug 25 '14 at 00:00
  • 1
    Alright, I figured that out, now the question is why is it not writing it to my employeesOut.txt file? I'm getting no error. Other than this in my output box. libc++abi.dylib: terminating with uncaught exception of type std::invalid_argument: stoi: no conversion (lldb) – MatthewTingle Aug 25 '14 at 00:11
  • Use intermediate temp vars, turn all optimization off, do a debug build, use the debugger to breakpoint/step/inspect stuff. Fix the problems found. – Martin James Aug 25 '14 at 21:38

1 Answers1

3

It's precisely what it says: stoi failed.

You only have that twice, so it's clear that either your eidText is not numeric, or your ZIP code is not numeric.

We don't know whether this is due to a problem with your input file or a problem with your parsing, so output eidText and zipcodeText to the console before you stoi them, so you can see what their values are, then continue your diagnosis accordingly.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I've changed my original post to include what my employeesIn.txt file looks like. Do I need to change my zipcode declaration from string zipcode; to int zipcode; ? – MatthewTingle Aug 25 '14 at 00:29
  • So just change anywhere it says fout, to cout so I can see the values in the console? – MatthewTingle Aug 25 '14 at 00:43
  • @MatthewTingle No.... Which book are you using? Now that you've added your input, it's clear what's going on. You read the text `"<62025>"` into `zipcodeText` and then use `std::stoi` to convert it to an integer. But `<62025>` is not an integer. You need to handle those `<`/`>` somehow. Simply writing `zipcodeText` to console using a temporary line of code would have revealed this to you, trivially! – Lightness Races in Orbit Aug 25 '14 at 01:47
  • So to output zipcodeText to the console, so I can see it. I simply write << cout zipcodeText; before my main correct? – MatthewTingle Aug 25 '14 at 21:10
  • C++ Programming, Program Design Including Data Structures by D.S. Malik Sixth Edition – MatthewTingle Aug 25 '14 at 21:24
  • A C++ book should teach you how to write to console, and where to write imperative statements. Here's [some good ones](http://stackoverflow.com/q/388242/560648). – Lightness Races in Orbit Aug 25 '14 at 21:25
  • A book on general debugging/troubleshooting techniques my be even better. – Martin James Aug 25 '14 at 21:34