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!

Recommended Answers

All 2 Replies

Care to describe what your problem is?
What should output look like, and what is you're actually getting.

From looking at your cout statements, e.g. line 58:
cout << name << age << sex << score << endl;
Your output is not being spaced because you aren't putting any spaces between any of the output values. You might want to consider inserting some whitespace between each variable you output. Either by using one or more space characters in a string or using "\t" for a tab e.g.:
cout << name << " " << age << " " << sex << " " << score << endl;

Another observation:
In lines 61 - 82, (your male/female calculations) you might want to take another look at the conditions used in your if statements:
e.g. line 61: if (sex=='M' && age > 7) - corresponds to males who are over the age of 7, NOT males under the age of 7.
Likewise, line 68: else if(sex =='M' && age < 8) - corresponds to males who are under 8. NOT males who are over 7!
Line 75 is OK; but again, line 82 is incorrect:
else if(sex =='F' && age < 8) - would correspond to females under the age of 8, NOT females over the age of 7.
So you should update the conditions of those if statements accordingly. I'll leave you to work out what values to put in there!

commented: Very helpful and supporting, not belittleing, what I like to see +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.