-1

I'm about to pull my hair out working on this program. I'm close! I think....I just have a stack overflow error to work around. Here is the code:

using namespace std;

struct runner{
public:
    int position;
    string time;
    int age;
    string sex;
    string gender;
    string firstName;
    string lastName;
    string city;
    string state; 


runner(){
    runner r1;
    string dataChunk;
    int ageTotal = 0;
    double ageAverage = 0.0;
    int ageCount = 0;
    int femaleAlabama = 0;
    int femaleOverForty = 0;
    int femaleHuntsville = 0;
    int femaleCount = 0;
    double femaleAgeAverage = 0.0;

    ifstream inFile("C:\\Users\\Anthony\\Desktop\\cmarathon.csv");


        getline(inFile, dataChunk, ',');
        r1.position = atoi(dataChunk.c_str());

        getline(inFile, dataChunk, ',');
        r1.time = dataChunk;

        getline(inFile, dataChunk, ',');
        r1.age = atoi(dataChunk.c_str());
        ageTotal = +age;
        ageCount = +1;

        getline(inFile, dataChunk, ',');
        r1.sex = dataChunk;
        if(sex == "f" || "F")
            femaleCount++;
            femaleAgeAverage++;

        getline(inFile, dataChunk, ',');
        r1.gender = dataChunk;

        getline(inFile, dataChunk, ',');
        r1.firstName = dataChunk;

        getline(inFile, dataChunk, ',');
        r1.lastName = dataChunk;

        getline(inFile, dataChunk, ',');
        r1.city = dataChunk;

        getline(inFile, dataChunk, ',');
        r1.state = dataChunk;

        if(sex == "f" || "F" && age > 40)
            femaleOverForty++;

        if(sex == "f" || "F" && city == "Huntsville")
            femaleHuntsville++;

        if(sex == "f" || "F" && state == "Al" || "AL")
            femaleAlabama++;
        cout<<r1.position<<" "<<r1.time<<" "<<r1.age<<" "<<r1.sex<<" "
            <<r1.gender<<" "<<r1.firstName<<" "<<r1.lastName<<" "<<r1.city
            <<" "<<r1.state<<endl;}

};




int main(){

int i;
    for(i = 1; i <1343; i++){
        runner();
    }







    system("PAUSE");
    return 0;

}

The goal here is to loop through a .csv sheet and pull the data into a structure. Then I can use that data to calculate various things like average age of females etc etc. Any suggestions?

EDIT:

Here is a snippet of the error code I receive when I try to run the program My file name got cut off, but that's it for the dialogue

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
  • `#include "vector";` should be `#include ` and similar for all the others. – John Zwinck Dec 10 '14 at 08:33
  • Please add any error messages, logs or similar. Did you try a debugger? – Victor Sand Dec 10 '14 at 08:33
  • You have a struct named runner and a function in that struct, also named runner. Your variables are declared inside the function, so they will be destroyed and re-created every time you run it. Where does the number 1343 come from? Does this run at all? Try a debugger or adding some console outputs, because I don't think this does what you think it does. – Victor Sand Dec 10 '14 at 08:37
  • sex == "f" || "F" is not going to work, this will be read as (sex == "f") || "F": http://en.cppreference.com/w/cpp/language/operator_precedence – David Sykes Dec 10 '14 at 08:38
  • You can't just make up syntax and expect it to work. – David Schwartz Dec 10 '14 at 08:41
  • 1343 refers to the number of records inside the excel spreadsheet that I'm pulling from. I don't need to store the variables once they've been output, so I figured I could do this to hopefully keep the size of the program down. Thanks for pointing that out, David. I'll be sure to fix that as well. – Anthony Ferrante Dec 10 '14 at 08:41
  • 1
    "femaleAgeAverage++;" will run every time. You need brackets around the lines you want to run following the IF statement. – Dave Dec 10 '14 at 08:42

1 Answers1

1

PROBLEM
Don't create runner r1; inside constructor it would lead to infinite recursion.

Solution
You can make r1 as static or a reference to already existing object. runner &r1 = *this; for example.

Gyapti Jain
  • 4,056
  • 20
  • 40
  • 1
    Both classes and structs create objects. For a Q&A on the differences there is [this](http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c) SO question. – Avi Ginsburg Dec 10 '14 at 08:53
  • This helped a bunch. I think I'm on my way to fixing it now. Thanks – Anthony Ferrante Dec 10 '14 at 08:55