Input/Output Read Loop Crash
Nov 4, 2017 at 10:04pm UTC
Hello, I am doing an assignment that requires me to read names and numbers from a text file. Everything I've written so far compiles, but for some reason my program is crashing at a certain point and I am not sure why.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
struct manager
{
int ID;
int salary;
string fname;
string lname;
};
struct customer
{
double cash;
string fname;
string lname;
};
struct seller
{
int ID;
int numCust;
double totalCash;
string fname;
string lname;
string gender;
customer custArrs[];
void sumUp();
};
void seller::sumUp()
{
totalCash = 0;
for (int x = 0; x < numCust; x++)
{
totalCash = totalCash + custArrs[x].cash;
}
}
void youreFired(seller sellArr[], int & fired, int numSellers)
{
for (int y = 0; y < numSellers; y++)
{
for (int x = 0; x < numSellers; x++)
{
if (sellArr[y].totalCash < sellArr[x].totalCash)
{
fired = y;
}
}
}
// for(int b = 1; b < sellArr[fired].numCust; b++)
// {
// sellArr[numSellers + b].custArrs[b - 1].name = sellArr[fired].custArrs[b - 1].name;
// sellArr[numSellers + b].custArrs[b - 1].cash = sellArr[fired].custArrs[b - 1].cash;
// }
}
void printScam(manager man1, seller sellArr[], int numSellers, int fired)
{
cout << "Manager:" << endl << man1.ID << endl << man1.fname
<< endl << man1.lname<< endl << man1.salary << endl;
for (int x = 0; x < numSellers; x++)
{
if (x != fired)
{
cout << "/tSeller:" << endl << "/t" << sellArr[x].fname
<< endl << "/t" << sellArr[x].ID << endl << "/t"
<< sellArr[x].totalCash;
for (int y = 0; y < (sizeof (sellArr[x].custArrs)); y++)
{
cout << "/t/tCustomers:" << endl << "/t/t" <<
sellArr[x].custArrs[y].fname << endl << "/t/t" <<
sellArr[x].custArrs[y].cash << endl;
}
}
}
}
int main()
{
ifstream infile;
manager man1;
int numSellers = 0;
int fired;
seller sellArr[50];
infile.open("Assg7.txt" );
infile >> man1.ID >> man1.fname >> man1.lname >> man1.salary;
cout << endl << man1.ID << endl << man1.fname << endl << man1.lname << endl << man1.salary;
while (numSellers < 5)
{
infile >> sellArr[numSellers].ID >> sellArr[numSellers].fname >> sellArr[numSellers].lname >> sellArr[numSellers].gender
>> sellArr[numSellers].numCust;
cout << endl << sellArr[numSellers].ID << endl << sellArr[numSellers].fname << " " << sellArr[numSellers].lname << endl << sellArr[numSellers].gender
<< endl << sellArr[numSellers].numCust;
for (int x = 0; x < sellArr[numSellers].numCust; x++)
{
infile >> sellArr[numSellers].custArrs[x].fname >> sellArr[numSellers].custArrs[x].lname >> sellArr[numSellers].custArrs[x].cash;
cout << endl << sellArr[numSellers].custArrs[x].fname << endl << sellArr[numSellers].custArrs[x].lname << endl << sellArr[numSellers].custArrs[x].cash;
}
Around about here is where the program crashes. The above for loop will execute once and crash. Any help would be appreciated. Here is the rest of the code.
1 2 3 4 5 6 7 8 9 10 11
sellArr[numSellers].sumUp();
numSellers++;
}
printScam(man1, sellArr, numSellers, fired);
youreFired(sellArr, fired, numSellers);
printScam(man1, sellArr, numSellers, fired);
}
and the text file looks like this:
1039547
Jabob Smith
120000
1033547
Gwenn Mccloy
Male
2
Hildegard Rutz
39.46
Lydia Feng
58.21
1478021
Hildegard Rutz
Female
1
Noreen Encarnacion
1.23
1098576
Deirdre Stayer
Female
4
Wade Peffer
5.24
Ronda Ord
89.99
Lacresha Lawrence
22.54
Zackary Schiro
19.95
122354
Deirdre Stayer
Female
1
Marcus Curl
2.22
187213
Tyesha Overton
Male
2
Celine Mccutchan
52.29
Debra Widger
2.55
Last edited on Nov 4, 2017 at 10:04pm UTC
Nov 4, 2017 at 11:17pm UTC
This:
1 2 3 4 5 6
struct seller
{
//...
customer custArrs[];
//...
};
doesn't give you a variable-sized array of customers, it gives you an uninitialized pointer. If you need a growing sequence then you should use std::vector.
http://www.cplusplus.com/reference/vector/vector/
Also the way you're using sizeof is wrong. It doesn't work that way. Read the documentation and use what you learn to rewrite that part.
Topic archived. No new replies allowed.