I am almost done with my assignment but am having problems with the spacing when I echo the file.
Any help please? this is what I have:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
//Declare variables to manipulate data
char reply;
string name;
string inputFileName;
int count = 0;
int totalScore = 0;
int maleScoreU7 = 0;
int maleScoreO7 = 0;
int femScoreU7 = 0;
int femScoreO7 = 0;
int maleCountU7 = 0;
int maleCountO7 = 0;
int femCountU7 = 0;
int femCountO7 = 0;
//Declare stream variables
ifstream inputFile;
ofstream outputFile;
// Prompt user for file location
cout << "Please enter the full pathname to the data file on disk: ";
getline (cin, inputFileName);
// open output file and run program exit failsafe command
inputFile.open (inputFileName.c_str()); //need to convert to C++
if(! inputFile.is_open())
{
cout << "Cannot open input file. "
<< "Program will now terminate." << endl;
return 1;
}
cout << "Reading data from '" << inputFileName << "':" << endl;
cout << fixed << showpoint << setprecision(2) << endl;
while(inputFile >> name)
{ // If we read a name, we can continue. Otherwise, we're done.
int age;
char sex;
int score;
inputFile >> age >> sex >> score;
// Echo
cout << name << age << sex << score << endl;
// Male and female calculations
if (sex=='M' && age > 7)
{
maleScoreU7 +=score;
totalScore +=score;
maleCountU7++;
count++;
}
else if(sex =='M' && age < 8)
{
maleScoreO7 +=score;
totalScore +=score;
maleCountO7++;
count++;
}
else if(sex =='F' && age < 7)
{
femScoreU7 +=score;
totalScore +=score;
femCountU7++;
count++;
}
else if(sex =='F' && age < 8)
{
femScoreO7 +=score;
totalScore +=score;
femCountO7++;
count++;
}
}
// Average Calculations
cout << fixed << showpoint << setprecision(2) << endl;
if(maleCountU7 != 0)
{
cout << "The average scores for male subjects 7 and under is: "
<< static_cast<double>(maleScoreU7) / maleCountU7 << endl;
}
else
cout << "There were no male subjects 7 and under" << endl;
if(maleCountO7 != 0)
{
cout << "The average score for male subjects over 7 is: "
<< static_cast<double>(maleScoreO7) / maleCountO7 << endl;
}
else
cout << "There were no male subjects over 7" << endl;
if(femCountU7 != 0)
{
cout << "The average score for female subjects 7 and under is: "
<< static_cast<double>(femScoreU7) / femCountU7 << endl;
}
else
cout << "There were no female subjects 7 and under" << endl;
if(femCountO7 != 0)
{
cout << "The average score for female subjects over 7 is: "
<< static_cast<double>(femScoreO7) / femCountO7 << endl;
}
else
cout << "There were no male subjects 7 and under" << endl;
cout << "The average test score for all sujects is: "
<< totalScore / count << endl;
// Stops the program from flashing off the screen.
cout << "Press q (or any other key) followed by 'Enter' to quit: ";
cin >> reply;
return 0;
}
Thanks!