Calling a Function within a function

Pages: 12
I want to call the function addActivity within processData, but it does not work. The problem is most likely activityDistance- I get warnings that tell me it is an unused variable. How can I call it within addActivity?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  void User::processData(string fileName){
string activityType;
double activityDistance;
string distance;
ifstream megaFile;
megaFile.open(fileName);
while (getline(megaFile, activityType)){
getline(megaFile, activityType, ',');
getline(megaFile, distance);
activityDistance = stod(distance);
void addActivity( string activityType, double activityDistance);
}


megaFile.close();
}
> The problem is most likely activityDistance- I get warnings that tell me it is an unused variable

Simply, just initialize activityDistance with a (0) :
double activityDistance = 0;
Last edited on
I tried that, but the function still isn't activating.
void addActivity( string activityType, double activityDistance);
Could you please let us see it?
Also, your function main() is also helpful too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void addActivity(string activityType, double activityDistance){
    cout<<"hithere"<<endl;
    if(activityType=="run"){
        runDistances[numRuns]=activityDistance;
        numRuns++;
    }
    else if(activityType=="skierg"){
        skiergDistances[numSkiErgs]=activityDistance;
        numSkiErgs++;
    }
    else if(activityType=="erg"){
        ergDistances[numErgs]=activityDistance;
        numErgs++;
    }

}

It converts an Excel File to an Array.
addActivity is set within the Private part of a Class, while processData is outside both the Class and the Main function. Could that be related?
my main() function goes:
int main()
{
User UserTest;
UserTest.processData("activitydata.csv");

UserTest.getActivityDetails("all", "avg");


return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void User:: ProcessData(string fileName)
{
string activityType;
double activityDistance = 0;
string distance;
ifstream megaFile;

megaFile.open(fileName);
while (getline(megaFile, activityType))
{
cout << "Hooray!!!" << endl;
getline(megaFile, activityType, ',');
getline(megaFile, distance);
activityDistance = stod(distance);

addActivity(activityType, activityDistance);
}
megaFile.close();
}


Let us know the output that the program shows you.
Alternating Hoorays and hitheres. The arrays are being filled, but only partly- there are 5 pieces of data in the arrays, while the original file had 10 pieces of information. But the function is clearly being called.
So "the function" is addActivity() you are referring to?

Also, how many "Hooray" msg appeared in your program output just now?
Lol I mistook "hitheres" for another word. You should separate them with a space.
The words appear 6 times each, reading an Excel File that is 11 rows (10 data and one Header.)
Can you show us the context of the file activitydata.csv by using notepad?
Sorry if this isn't the greatest format to read.

activity, distance
run, 5.3
skierg, 1
erg, 2
run, 7
skierg, 0.54
skierg, 0.34
run, 2
erg, 1
erg, 0.75
run, 9
My output for the contents of the Arrays are:
RUN(5.3, 2)
SKIERG (.54)
ERG (2, .75)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void User:: ProcessData(string fileName)
{
string activityType;
double activityDistance = 0;
string distance;
ifstream megaFile;

megaFile.open(fileName);
while (getline(megaFile, activityType, ','))
{
cout << "Hooray!!!" << endl;

getline(megaFile, distance);
activityDistance = stod(distance);

addActivity(activityType, activityDistance);
}
megaFile.close();
}


Your program is doing a little too much. A redundant getline() would very likely disrupt the reading order you know.
It returns:
Hooray!
terminate called after throwing an instance of 'std::invalid_argument'
what(): stod
Aborted (core dumped)

I think it tries to convert the text to a double, which won't let it work. Also, that way would leave me unable to read the activityType.
If the stod is the curpit try manual debugging
1
2
3
getline(megaFile, distance);
cout << "Type : " << activityType << endl;
cout << "Distance : \"" << distance << "\"" << endl << endl;
Last edited on
Also let us know the output outcome
Pages: 12