Question: [C++ URGENT] Please fix my code. I'm trying to find the average of the year's data, have it output a single year with its average

[C++ URGENT] Please fix my code. I'm trying to find the average of the year's data, have it output a single year with its average.

The text file starts at 1947q2 so if I were to take the average it would be divided by three. The text file ends at 2009, which also only has 3 data(q1-q3). Everything else has 4 data (from q1 to q4)

TEXT FILE:

1947q2 -0.6 1947q3 -0.3 1947q4 6.2 1948q1 6.5 1948q2 7.6 1948q3 2.2 1948q4 0.6 1949q1 -5.4 1949q2 -1.4 1949q3 4.6 1949q4 -3.7 1950q1 17.2 1950q2 12.7 1950q3 16.6 1950q4 7.2

//GDP.cpp source #include #include #include #include #include"GDP.h" using namespace std; void GDP::get(string filename){ int index = 0; int temp, year; double data; double average; string q; string line; fstream file; file.open(filename.c_str(),ios::in); file.seekg(0,ios::beg); if (!file){ cout > temp; //While we are in the file... while (file){ //reading file data if(file >> year >> q >> data){ //reading data from file double sum = 0; if (temp == year) { //takes the sum of data from the years repeating sum += data; //increments every time year repeats int countOfYear; countOfYear++; //takes the average average = sum / countOfYear; cout //My average doesn't come out right so I thought //I had to change the temp to fall out of the if. temp + 1; } //I don't want the year to repeat... //I want it to display the year along with its average cout

HEADER FILE IF NEEDED..:

//Class GDP definition #ifndef GDP_H_INCLUDED #define GDP_H_INCLUDED #include #include #include using namespace std; typedef struct fileData{ // stores data from file "Gdp.txt" int year; string q; double data; double average; } structData; class GDP { private: structData dataArray[50]; //fixed length but not binary int total; //Total amount of data, needed for average public: void get(string);//reads the file void show();//displays the file }; #endif // GDP_H_INCLUDED MY ERROR:

[C++ URGENT] Please fix my code. I'm trying to find the average

Enter a year from 1948 - 2009: 1950 year 1947 average 1.7881e-307 year 1947 average 1.7881e-307 year 1947 average 1.7881e-307 year 1948 average 1.7881e-307 year 1948 average 1.7881e-307 year 1948 average 1.7881e-307 year 1948 average 1.7881e-307 year 1949 average 1.7881e-307 year 1949 average 1.7881e-307 year 1949 average 1.7881e-307 year 1949 average 1.7881e-307 average1: 3.64496e-006 sum 17.2 year 1950 average 3.64496e-006 average1: 2.69133e-006 sum 12.7 year 1950 average 2.69133e-006 average1: 3.5178e-006 sum 16.6 year 1950 average 3.5178e-006 average1: 1.52579e-006 sum 7.2 year 1950 average 1.52579e-006 year 1951 average 1.52579e-006 year 1951 average 1.52579e-006 year 1951 average 1.52579e-006 year 1951 average 1.52579e-006 year 1952 average 1.52579e-006 year 1952 average 1.52579e-006

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!