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.
You have a few issues with your code. Let’s take things one at a time, shall we?
What does the last line do?
inDataisn’t open and yet you’re trying to read from it.Here (and in a few other places) you are using the
+=operator incorrectly. You should either use+=like this:or use
+like this:You then do this:
Now,
CCis anstd::stringin your code, which you have not initialized (which means that it is empty). So it will never match, therefore theifbody will never execute. The same thing happens withUN.Further down, you have this line inside the loop:
What this does is recalculate the average for males every time through the loop. That’s not necessarily wrong (you will get the right result) but if the first person in the file is a female, your program will crash with a division by zero, since
maleCountwill be 0. The same thing will happen if no females are in the input file, or if there are no scores from a University or a College.Lastly, using
iostream::eof()inside a loop is not a good idea. You can read more about that right here on StackOverflow: Why is iostream::eof inside a loop condition considered wrong?With all that said, your mistakes aren’t really serious and are typical of new programmers, so don’t get discouraged. Think of this as an opportunity to learn and to understand how to examine your code to find these sort of bugs. So without further ado, let’s see an improved version of this program: