I am new to the stackoverflow communiy and new to coding so apologies in advance whie I learn the ropes of posting here as well as the rules of coding. I am using C++ and am in a CS161 beginner Computer Science class.
I am currently working on an assignment which asks me to read from a data file stored on my computer and sort the data in order to do some calculations, which, in this assignment is finding the average test scores based on sex and type of school. everything compiles and the program runs but there are a few problems.
The first problem is with my echo.
// echo the data file
while (inData)
{
inData >> name >> sex >> school >> score;
cout << name << sex << school << score << endl;
The program does echo the data but it ends up echoing the last name on the list, twice for some reason. Also, (and I don't know if this matters) when it does echo, it does not skip spaces inbetween name, sex, school, and score.
The second problem is that it isn't executing the calculations and I think it is because I am missing a "count" related instruction of some sort but I am stumped as far as what I can do.
Here is my code, let me know what you think:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//Declare variables to manipulate data
char sex;
string name;
string school;
string fileSource;
string CC;
string UN;
int maleScore = 0;
int femScore = 0;
int unScore = 0;
int ccScore = 0;
double maleAvg;
double femAvg;
double unAvg;
double ccAvg;
double sumAvg = 0;
int femCount = 0;
int maleCount = 0;
int ccCount = 0;
int unCount = 0;
int score;
int sum;
//Declare stream variables
ifstream inData;
ofstream outData;
inData >> name >> sex >> school >> score;
// Promt user for file location
cout << "Please input file location: ";
cin >> fileSource;
// open output file and run program exit failsafe command
inData.open(fileSource);
if (!inData)
{
cout << "Cannot open input file. "
<< "Program will now terminate." << endl;
return 1;
}
outData << fixed << showpoint << setprecision(2);
// echo the data file
while (inData)
{
inData >> name >> sex >> school >> score;
cout << name << sex << school << score << endl;
// while reading incoming data from file, execute the conditions
// Male and female calculations
if(sex=='M')
{
maleScore = maleScore +=score;
++maleCount;
}
else if(sex =='F')
{
femScore = femScore +=score;
++femCount;
}
// Community college and University calculations
if(school == CC)
{
ccScore = ccScore +=score;
++ccCount;
}
else if(school == UN)
{
unScore = unScore +=score;
++unCount;
}
maleAvg = maleScore/maleCount;
}
// Male average output
cout << maleAvg;
femAvg = femScore/femCount;
// Female average output
cout << femAvg;
ccAvg = ccScore/ccCount;
// Community College average output
cout << ccAvg;
unAvg = unScore/unCount;
// University average output
cout << unAvg;
sum = maleScore + femScore + ccScore + unScore;
sumAvg = sum/12;
cout << sumAvg;
return 0;
}
Also, my compiler keeps running the program and does not stop. I took a pic of my compiler window but don't know how to post it.