473,413 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Data File input sorting question

heiro1
29
Hello everyone!
I am a computer science student taking my first C++ class and this is my first time posting a query online. So, I apologize in advance for my lack of savvy. Up until now, I have not had any problems whatsoever but my current assignment is throwing me for one heck of a loop so I was hoping one of you more experienced folk can point me in the right direction.

These are my assignment instructions:

1 - Your assignment is to prompt the user to enter the filename with the path on disk. If the file does not exist in the specified location, your program should exit with a suitable error message.

2 - The first thing your program should do is output to the screen a copy of the data read in from the disk file. This is known as “echoing” the input data.

3 - Your program should then calculate and display the average score for males, females, community college students, and university students. Display your results to two places of decimals, and write your program to automatically list your four calculated averages one to a line, along with a suitable label for each result.

4 - Finally, your program should calculate and display to two places of decimals the overall average score for all of the students surveyed.


Questions:

1 - I understand loops fairly well but I don't know how to get the while loop to read info from a data file, one "column" at a time. They are not allowing us to do arrays yet but even if they did, I think arrays work line by line instead of column by column.

I have a feeling though, that through the use of loops, I can potentially manipulate the data search to the point that it will bypass everything except the data I need but I have no idea if that is the direction I need to go or if it is a matter of figuring out how to get a loop to read data from specific columns.

2 - The provided text file is this:

Bailey M CC 68
Harrison F CC 71
Grant M UN 75
Peterson F UN 69
Hsu M UN 79
Bowles M CC 75
Anderson F UN 64
Nguyen F CC 68
Sharp F CC 75
Jones M UN 75
McMillan F UN 80
Gabriel F UN 62

Since I am not outputting data to the file but instead, querying for input from the file, I would guess that I do not need any outfile commands but I am not sure about that. I am guessing the teacher put a list of names in there to make it harder to access the data I need (like the gender column). I am happy to provide more details if needed and thank you for your time! Here is a short template of my approach, it is just the gender scores so far:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <fstream> 
  3. #include <string> 
  4. #include <iomanip>
  5. using namespace std;
  6. int main()
  7. {
  8.     //Declare variables to manipulate data
  9.     char sex;
  10.     string name;
  11.     string school;
  12.     string fileSource;
  13.     int maleScore;
  14.     int femScore;
  15.     int UniScore;
  16.     int commcollScore;
  17.     int maleTotal;
  18.     int femTotal;
  19.     int uniTotal;
  20.     int commcollTotal;
  21.     double sum = 0;
  22.     int femCount = 0;
  23.     int maleCount = 0;
  24.     int score;
  25.     //Declare stream variables
  26.     ifstream inData;
  27.     ofstream outData;
  28.     inData >> name >> sex >> school >> score;
  29.     // Promt user for file location
  30.     cout << "Please input file location: ";
  31.     cin >> fileSource;
  32.  
  33.     // open output file and run program exit failsafe command
  34.     inData.open(fileSource);
  35.     if (!inData)
  36.     {
  37.         cout << "Cannot open input file. "
  38.         << "Program will now terminate." << endl;
  39.         return 1;
  40.     }
  41.         outData << fixed << showpoint << setprecision(2);
  42.  
  43.     // echo the data file
  44.     while (inData)
  45.     {
  46.         cout << name << sex << school << score << endl;
  47.         inData >> name >> sex >> school >> score;
  48.     }
  49.  
  50.     // while reading incoming data from file, execute the conditions
  51.     while (inData)
  52.     {
  53.         if(sex=='M')
  54.         {
  55.             maleScore = maleScore + maleTotal;
  56.             ++maleCount;
  57.         }
  58.         else if(sex =='F')
  59.                 {
  60.                     femScore = femTotal;
  61.                     ++femCount;
  62.                 }
  63.     }
  64. }
Feb 12 '13 #1
12 4764
heiro1
29
Ok, I added an echo. The errors read on the echo and the pseudo code

Here is is a bit more clean but still super basic:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <fstream> 
  3. #include <string> 
  4. #include <iomanip>
  5. using namespace std;
  6. int main()
  7. {
  8.     //Declare variables to manipulate data
  9.     char m;
  10.     char f;
  11.     string commcoll;
  12.     string uni;
  13.     double malescore;
  14.     double maleaverage;
  15.     double femalescore;
  16.     double femaleaverage;
  17.     double commcollscore;
  18.     double commcollaverage;
  19.     double uniscore;
  20.     double uniaverage;
  21.     double overallsum = 0;
  22.     int count = 0;
  23.  
  24.     //Declare stream variables
  25.     ifstream inData;
  26.     ofstream outData;
  27.  
  28.     // open output file and run program exit failsafe command
  29.     inData.open("/Users/jasonrodriguez/Desktop/scores.txt");
  30.     if (!inData) {
  31.         cout << "Cannot open input file. "
  32.         << "Program will now terminate." << endl;
  33.         return 1; }
  34.  
  35.     while(infile.get()) // echo
  36.     {
  37.         cout << infile;
  38.     }
  39.         outData << fixed << showpoint << setprecision(2);
  40.  
  41.     inData >> malescore;   //give a value to male score
  42.  
  43.     while (inData) // while reading incoming data from file, execute the conditions
  44.     {
  45.         if (column 2 == m)
  46.             malescore = malescore + column 4 input;
  47.         else (column 2 == f)
  48.             femaleaverage = femaleaverage 
  49.     inData.close();
  50.     return 0;
  51. }
Feb 12 '13 #2
heiro1
29
Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <fstream> 
  3. #include <string> 
  4. #include <iomanip>
  5. using namespace std;
  6. int main()
  7. {
  8.     //Declare variables to manipulate data
  9.     char m;
  10.     char f;
  11.     string commcoll;
  12.     string uni;
  13.     double malescore;
  14.     double maleaverage;
  15.     double femalescore;
  16.     double femaleaverage;
  17.     double commcollscore;
  18.     double commcollaverage;
  19.     double uniscore;
  20.     double uniaverage;
  21.     double overallsum = 0;
  22.     int count = 0;
  23.  
  24.     //Declare stream variables
  25.     ifstream inData;
  26.     ofstream outData;
  27.  
  28.     // open output file and run program exit failsafe command
  29.     inData.open("/Users/jasonrodriguez/Desktop/scores.txt");
  30.     if (!inData) {
  31.         cout << "Cannot open input file. "
  32.         << "Program will now terminate." << endl;
  33.         return 1; }
  34.  
  35.     while(infile.get()) // echo
  36.     {
  37.         cout << infile;
  38.     }
  39.         outData << fixed << showpoint << setprecision(2);
  40.  
  41.     inData >> malescore;   //give a value to male score
  42.  
  43.     while (inData) // while reading incoming data from file, execute the conditions
  44.     {
  45.         if (column 2 == m)
  46.             malescore = malescore + column 4 input;
  47.         else (column 2 == f)
  48.             femaleaverage = femaleaverage 
  49.     inData.close();
  50.     return 0;
  51. }
  52.  
Feb 12 '13 #3
weaknessforcats
9,208 Expert Mod 8TB
Each record contains 4 data elements: name, sex, type of school, and score.

You can read these in using cin >> name >> sex >> etc...

Then you test the sex and type of school to add to some variables that are your totals. I would also count the records read in.

Place the cin>> and the totaling logic onside a loop that runs until you run out of input data.

You now have all of the totals you need and if you have kept a inut record count you an divide your totals by the record count to get the averages.

From what I see, your program just needs chnages to the input reading and totallng logic which appear overly complicated.

Also, I see a lot of doubles but only integer values in the data. I expect you want to make the average of 3 to be 1.5. If that's the case, you are violating sigificant figures rules: derived data cannot be more accurate than the source cdata. That is, and averge of 1.5 requires source data of 3.0.

I think you should be using int variables.

Post again and let me know what happened.
Feb 12 '13 #4
heiro1
29
Thanks so much for clarifying those important things! I made osme adjustments and am still trying to figure out a logical approach. Here is what I have so far:


Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <fstream> 
  3. #include <string> 
  4. #include <iomanip>
  5. using namespace std;
  6. int main()
  7. {
  8.     //Declare variables to manipulate data
  9.     char M;
  10.     char F;
  11.     string file;
  12.     string commcoll;
  13.     string uni;
  14.     double malescore;
  15.     double maleaverage;
  16.     double femalescore;
  17.     double femaleaverage;
  18.     double commcollscore;
  19.     double commcollaverage;
  20.     double uniscore;
  21.     double uniaverage;
  22.     double overallsum = 0;
  23.     int count = 0;
  24.     int score;
  25.  
  26.     //Declare stream variables
  27.     ifstream inData;
  28.     ofstream outData;
  29.  
  30.     // Promt user for file location
  31.     cout << "Please input file location: ";
  32.     cin >> file;
  33.  
  34.     // open output file and run program exit failsafe command
  35.     inData.open(file);
  36.     if (!inData) {
  37.         cout << "Cannot open input file. "
  38.         << "Program will now terminate." << endl;
  39.         return 1; }
  40.         outData << fixed << showpoint << setprecision(2);
  41.  
  42.     // while reading incoming data from file, execute the conditions
  43.     while (inData) 
  44.     {
  45.         if (char = M)
  46.             malescore = malescore + score;
  47.         else if (char = F)
  48.             femalescore = femalescore + score;
  49.         count ++;
  50.     }
  51.     inData.close();
  52.     return 0;
  53. }
Is there anything I am not doing correctly? I am not sure where to go from here.

Oh, also is this echoing the file?

// echo
Expand|Select|Wrap|Line Numbers
  1. {
  2.  cout << istream.get;
  3. }
Feb 13 '13 #5
weaknessforcats
9,208 Expert Mod 8TB
This is a sample of your data:

Bailey M CC 68

What I see is a name, a sex identifier, a school identifier and a score.

You would read this data by:

Expand|Select|Wrap|Line Numbers
  1. string Name;
  2. string Sex;
  3. string School;
  4. int    Score;
  5.  
  6. inData >> Name >> Sex >> School >> Score;
I don't see in your code where you are actualy reading the data.

Then if Sex == "F" you add to the female total
if Sex == "M" you add to the male total
if School == "CC" you add to the community college total
etc...

Finally add 1 to the record count and go back and read another record using a while loop as you are already doing.

Echoing the file would be to display the data read by:

Expand|Select|Wrap|Line Numbers
  1. cout << Name << Sex << School << Score << "\n";
right after your inData >>. Nothing prevents you from formatting the cout so that your data isn't all mushed together.
Feb 13 '13 #6
heiro1
29
Hello again wfc,
Thank you for sharing your observations! I thought about it for a bit and came up with this version of the code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream> 
  2. #include <fstream> 
  3. #include <string> 
  4. #include <iomanip>
  5. using namespace std;
  6. int main()
  7. {
  8.     //Declare variables to manipulate data
  9.     char sex;
  10.     string name;
  11.     string school;
  12.     string fileSource;
  13.     string CC;
  14.     string UN;
  15.     int maleScore = 0;
  16.     int femScore = 0;
  17.     int unScore = 0;
  18.     int ccScore = 0;
  19.     double maleAvg;
  20.     double femAvg;
  21.     double unAvg;
  22.     double ccAvg;
  23.     double sumAvg = 0;
  24.     int femCount = 0;
  25.     int maleCount = 0;
  26.     int ccCount = 0;
  27.     int unCount = 0;
  28.     int score;
  29.     int sum;
  30.     //Declare stream variables
  31.     ifstream inData;
  32.     ofstream outData;
  33.     inData >> name >> sex >> school >> score;
  34.     // Promt user for file location
  35.     cout << "Please input file location: ";
  36.     cin >> fileSource;
  37.  
  38.     // open output file and run program exit failsafe command
  39.     inData.open(fileSource);
  40.     if (!inData)
  41.     {
  42.         cout << "Cannot open input file. "
  43.         << "Program will now terminate." << endl;
  44.         return 1;
  45.     }
  46.         outData << fixed << showpoint << setprecision(2);
  47.  
  48.     // echo the data file
  49.     while (inData)
  50.     {
  51.         cout << name << sex << school << score << endl;
  52.         inData >> name >> sex >> school >> score;
  53.     }
  54.  
  55.     // while reading incoming data from file, execute the conditions
  56.     // Male and female calculations
  57.     while (inData)
  58.     {
  59.         if(sex=='M')
  60.         {
  61.             maleScore = maleScore + score;
  62.             ++maleCount;
  63.         }
  64.         else if(sex =='F')
  65.                 {
  66.                     femScore = femScore + score;
  67.                     ++femCount;
  68.                 }
  69.         if (maleCount == 12)
  70.         {
  71.             maleAvg = maleScore/maleCount;
  72.         }
  73.  
  74.         // Male average output
  75.         cout << maleAvg;
  76.  
  77.         if (femCount == 12)
  78.         {
  79.             femAvg = femScore/femCount;
  80.         }
  81.         // Female average output
  82.         cout << femAvg;
  83.         // Community college and University calculations
  84.         if(school == CC)
  85.         {
  86.             ccScore = ccScore + score;
  87.             ++ccCount;
  88.         }
  89.         else if(school == UN)
  90.         {
  91.             unScore = unScore + score;
  92.             ++femCount;
  93.         }
  94.         if (ccCount == 12)
  95.         {
  96.             ccAvg = ccScore/ccCount;
  97.         }
  98.         // Community College average output
  99.         cout << ccAvg;
  100.  
  101.         if (unCount == 12)
  102.         {
  103.             unAvg = unScore/unCount;
  104.         }
  105.         // University average output
  106.         cout << unAvg;
  107.         sum = maleScore + femScore + ccScore + unScore;
  108.         sumAvg = sum/12;
  109.         cout << sumAvg;
  110.         return 0;
  111. }
  112. }
  113.  
3 things about it though,

1 - my compiler is telling me that none of my "count" variables are initialized. I am pretty sure that is because I did not create a condition that bases the amount of loops with the counters. Any idea on how I can incorporate that into my code or is it even necessary?

2 - I played it pretty safe here with the coding. I think the teacher wanted us to be more daring with the loops. Any ideas on how I can cut my code shorter using a "while..do" or a "for" loop?

3 - The first loop was intended to be an echo. Did I do it wrong? How do I close it? Place a

Expand|Select|Wrap|Line Numbers
  1. cout << endl; 
on the next line? Thanks for your insight!!
Feb 13 '13 #7
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1.  inData.open(fileSource.c_str());
is how you open the file. The open method does not use a C++ string argument but instead uses a C string argument. I'm surprised your code:

Expand|Select|Wrap|Line Numbers
  1. inData.open(fileSource);
is compiling. What compiler are you using.

Next, you have loop problems. This loop:
Expand|Select|Wrap|Line Numbers
  1. while (inData)
  2.     {
  3.         cout << name << sex << school << score << endl;
  4.         inData >> name >> sex >> school >> score;
  5.     }
  6.  
Will display the data before it is read. Not good. It should be:
Expand|Select|Wrap|Line Numbers
  1. while (inData)
  2.     {
  3.            inData >> name >> sex >> school >> score;
  4.            cout << name << sex << school << score << endl;
  5.  
  6.     }
  7.  
Now the inout will be echoed but when you leave it your input file is at the end of the file so the next loop:
Expand|Select|Wrap|Line Numbers
  1.     while (inData)
  2.     {
  3.         if(sex=='M')
  4.         {
  5. ETC...
Won't do anything because inData is false. You have to do the echo plus all the analysis inside the first loop.


Expand|Select|Wrap|Line Numbers
  1. while (inData)
  2.     {
  3.         inData >> name >> sex >> school >> score;
  4.         cout << name << sex << school << score << endl;
  5.  
  6.  
  7.         //totaling and other processing done here
  8.     }
  9.  
This way you come into the loop, read a record of data, then echo that record, then process that record, then go back to the start of the loop so you can read the nexr record.

When you leave this single loop all inout has been echoed, totals calculated. Now you just calculate your averages. To do this you will need to count the number of records read but I do not see you doing that.

Next, this code:
Expand|Select|Wrap|Line Numbers
  1. if(sex=='M')
  2. etc...
compares sex to a single character. This may be OK but for beginners I wouls use "M" rather than 'M'. The difference is that "M" is a C string whereas 'M' is a single character. So use "CC". I see you using CC where CC is a string but you never assigned "CC" to the string.

I also see hard-coded numbers, like 12. Why 12? Did you peek at the input? A hard-coded number like this puts a limit in your program. The program gives wrong answers when the input is not exactly 12.

I would to this in steps.

Write one loop to read and echo the input. Get this working.

Then add code to test for community college and add to a total. Display the total at the end of the program.

When this works, add code for the other schools. Display thse totals.

Now add code that counts males and females...

Make sure at each step your program works. If ti fails to work you are pretty sure it is the you just added that's not working.

And before you modify a working program make a copy of it in case your modifications result in a mess. You can start over just by making a copy of the original from the previous step.

The code is coming along. I see progress.
Feb 13 '13 #8
heiro1
29
Thanks so much for your input, I really appreciate it! I am using Xcode. I had that C command in there because a friend who knows C told me that is how to echo, I didn't realize it was a different type of code.

I meant to say the compiler gave me 4 warning markers, I didn't actually compile anything at that point. I figured out why they were not initialized though.

I made some changes based on your observations and now everything compiles and the program runs but there are a few problems.

The first problem is with my echo.
// echo the data file
Expand|Select|Wrap|Line Numbers
  1.  while (inData)
  2.     {
  3.         inData >> name >> sex >> school >> score;
  4.         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.

Here is my revised code, let me know what you think:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. using namespace std;
  6. int main()
  7. {
  8.     //Declare variables to manipulate data
  9.     char sex;
  10.     string name;
  11.     string school;
  12.     string fileSource;
  13.     string CC;
  14.     string UN;
  15.     int maleScore = 0;
  16.     int femScore = 0;
  17.     int unScore = 0;
  18.     int ccScore = 0;
  19.     double maleAvg;
  20.     double femAvg;
  21.     double unAvg;
  22.     double ccAvg;
  23.     double sumAvg = 0;
  24.     int femCount = 0;
  25.     int maleCount = 0;
  26.     int ccCount = 0;
  27.     int unCount = 0;
  28.     int score;
  29.     int sum;
  30.     //Declare stream variables
  31.     ifstream inData;
  32.     ofstream outData;
  33.     inData >> name >> sex >> school >> score;
  34.     // Promt user for file location
  35.     cout << "Please input file location: ";
  36.     cin >> fileSource;
  37.  
  38.     // open output file and run program exit failsafe command
  39.     inData.open(fileSource);
  40.     if (!inData)
  41.     {
  42.         cout << "Cannot open input file. "
  43.         << "Program will now terminate." << endl;
  44.         return 1;
  45.     }
  46.     outData << fixed << showpoint << setprecision(2);
  47.  
  48.     // echo the data file
  49.     while (inData)
  50.     {
  51.         inData >> name >> sex >> school >> score;
  52.         cout << name << sex << school << score << endl;
  53.  
  54.         // while reading incoming data from file, execute the conditions
  55.  
  56.         // Male and female calculations
  57.             if(sex=='M')
  58.             {
  59.                 maleScore = maleScore +=score;
  60.                 ++maleCount;
  61.             }
  62.             else if(sex =='F')
  63.             {
  64.                 femScore = femScore +=score;
  65.                 ++femCount;
  66.             }
  67.  
  68.             // Community college and University calculations
  69.             if(school == CC)
  70.             {
  71.                 ccScore = ccScore +=score;
  72.                 ++ccCount;
  73.             }
  74.             else if(school == UN)
  75.             {
  76.                 unScore = unScore +=score;
  77.                 ++unCount;
  78.             }
  79.             maleAvg = maleScore/maleCount;
  80.         }
  81.  
  82.             // Male average output
  83.             cout << maleAvg;
  84.  
  85.             femAvg = femScore/femCount;
  86.  
  87.             // Female average output
  88.             cout << femAvg;
  89.  
  90.             ccAvg = ccScore/ccCount;
  91.  
  92.             // Community College average output
  93.             cout << ccAvg;
  94.  
  95.             unAvg = unScore/unCount;
  96.  
  97.             // University average output
  98.             cout << unAvg;
  99.             sum = maleScore + femScore + ccScore + unScore;
  100.             sumAvg = sum/12;
  101.             cout << sumAvg;
  102.             return 0;
  103.     }
  104.  
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.
Feb 13 '13 #9
weaknessforcats
9,208 Expert Mod 8TB
This code:
Expand|Select|Wrap|Line Numbers
  1.   while (inData)
  2.       {
  3.           inData >> name >> sex >> school >> score;
  4. etc...
Try:

Expand|Select|Wrap|Line Numbers
  1.   while (! inData.eof())
  2.       {
  3.           inData >> name >> sex >> school >> score;
  4. etc...
The input file has a member function named eof(). This function returns true when you reach the end of file. So the code above says "while NOT at the end of file..."

As to the last line over and over I suspect th actual data zipped off the top of the screen between eyeblinks. Nw yu are in a loop forever until inData is false (which will never happen) displaying the last data read every cycle of the loop.
Feb 13 '13 #10
heiro1
29
Ah, yes that's right. Still, even after making that change, I still have all of those problems though. Nothing changed. Well, one thing did change actually. Now the echo does not repeat the last name in the file list.

It still isn't doing any of the calculations and it is still perpetually running until I manually stop it. Mainly though, I am concerned with how to get my calculations to run. Any thoughts? It looks solid, I can't figure out why it isn't calculating.
Feb 13 '13 #11
heiro1
29
Ok problem solved, here is my result. It works like a charm:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. using namespace std; 
  7.  
  8. int main()
  9. {
  10.     //Declare variables to manipulate data
  11.     string name;
  12.     string fileSource;
  13.  
  14.     int maleScore = 0;
  15.     int femScore = 0;
  16.     int unScore = 0;
  17.     int ccScore = 0;
  18.     int femCount = 0;
  19.     int maleCount = 0;
  20.     int ccCount = 0;
  21.     int unCount = 0;
  22.  
  23.     //Declare stream variables
  24.     ifstream inData;
  25.     ofstream outData;
  26.  
  27.     // Prompt user for file location
  28.     cout << "Please input file location: ";
  29.     cin >> fileSource;
  30.  
  31.     // open output file and run program exit failsafe command
  32.     inData.open(fileSource);
  33.  
  34.     if(!inData)
  35.     {
  36.         cout << "Cannot open input file. "
  37.         << "Program will now terminate." << endl;
  38.         return 1;
  39.     }
  40.  
  41.     cout << "Reading data from '" << fileSource << "'" << endl;
  42.  
  43.     while(inData >> name)
  44.     { // If we read a name, we can continue. Otherwise, we're done.
  45.         char sex;
  46.         int score;
  47.         string school;
  48.  
  49.         inData >> sex >> school >> score;
  50.  
  51.         // Echo
  52.         cout << name << sex << school << score << endl;
  53.  
  54.         // Male and female calculations
  55.         if(sex=='M')
  56.         {
  57.             maleScore +=score;
  58.             maleCount++;
  59.         }
  60.         else if(sex =='F')
  61.         {
  62.             femScore +=score;
  63.             femCount++;
  64.         }
  65.  
  66.         // Community college and University calculations
  67.         if(school == "CC")
  68.         {
  69.             ccScore +=score;
  70.             ccCount++;
  71.         }
  72.         else if(school == "UN")
  73.         {
  74.             unScore +=score;
  75.             unCount++;
  76.         }
  77.     }
  78.  
  79.     // Average Calculations
  80.     cout << fixed << showpoint << setprecision(2) << endl; 
  81.     if(maleCount != 0)
  82.     {
  83.         cout << "The average scores for males is: " 
  84.         << static_cast<double>(maleScore) / maleCount << endl;
  85.     }
  86.  
  87.     if(femCount != 0)
  88.     {
  89.         cout << "The average score for females is: " 
  90.         << static_cast<double>(femScore) / femCount << endl;
  91.     }
  92.  
  93.     if(ccCount != 0)
  94.     {
  95.         cout << "The average score for Community Colleges is: " 
  96.         << static_cast<double>(ccScore) / ccCount << endl;
  97.     }
  98.  
  99.     if(unCount != 0)
  100.     {
  101.         cout << "The average score for Universities is: "   
  102.         << static_cast<double>(unScore) / unCount << endl;
  103.     }
  104.  
  105.     cout << "The average test score for all students combined is: " 
  106.     << (1.0 * (maleScore + femScore + ccScore + unScore)) / 12 << endl;
  107.  
  108.     return 0;
  109. }
Still, I have this nagging feeling like there is a way to make this program shorter. Is it possible?
Feb 14 '13 #12
weaknessforcats
9,208 Expert Mod 8TB
It's always possible to improve a program but my motto has always been:

The only reality is a running program - all else is prophecy.

Your program works. Call it day. The lessons you learned on this program will be applied to the next one making the next program better.

Be sure to post again as necessary.
Feb 14 '13 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: a | last post by:
(I've reached that familiar place where I've got a nagging little problem in a program I'm writing but I've been staring at code for too long and I probably wouldn't be able to recognize the answer...
2
by: J.R | last post by:
Greetings, I'm adding dynamically created input type='file' controls via JavaScript. However when I try to access they do not seem to be returned in the form collection. Any ideas? Thanks,...
6
by: tshad | last post by:
I have an upload file input as: <input id="MyFile" style="width:300px" type="File" runat="Server"> This works fine, but I find that if my page doesn't pass validation during postback, the page...
0
by: GB | last post by:
Help needed: in the C++ code of Backprapagation I am using data in the way shwon below. I need to change the program, so that it reads same data (actually I need to put more complex data, for 64...
2
by: oscar78 | last post by:
Hi, I am trying to write a perl script that will read a three-column data file and write certain lines of it in to a new data file according the ranges of values that each of these lines contain. ...
3
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never...
11
by: Trent | last post by:
Running this I see that on first run, both bubble and selection sort have 9 sort counts while insertion sort has ZERO. With a sorted list, should this be ZERO for all? Also bsort and Ssort have...
11
by: Peter Olcott | last post by:
Does C++ have anything like this?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.