Newbie needs direction!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2008
Posts: 26
Reputation: 2fac323 is an unknown quantity at this point 
Solved Threads: 0
2fac323 2fac323 is offline Offline
Light Poster

Newbie needs direction!!

 
0
  #1
Jul 17th, 2008
I am posting this problem because I am a newbie to C++ and this program has me totally stumped. Any direction, advice, examples would be extremely helpful! I will try to explain my problem as well as possible-

For research purposes and to better help students, the admission office of your local university wants to know how well female and male students perform in certain courses. You will receive a file that contains female and male students GPA's for certian courses. Due to confidentiality, the letter code f is used for female students and m is used for male students. Every file entry consists of a letter code followed by a GPA. Each line has one entry. The number of entries in the file is unknown. Write a program that computes and outputs the average GPA for both female and male students. Format your results to two decimal places. Your program should use the following functions.

a. Function openFiles: This function opens the input and output files, and sets the output of the floating-point numbers to two decimal places in a fixed decimal format with a decimal point and trailing zeros.

b. Function initialize: This function initializes variables such as countFemale, countMale, sumFemaleGPA, and sumMaleGPA.

c. Function sumGrades: This function finds the sum of the female and male students GPA's.

d. Function averageGrade: This function finds the average GPA for female and male students.

e. Function printResults: This function outputs the relevant results.

f. There can be no global variables. Use the appropriate parameters to pass information in and out of functions,

here is my data file:
f 3.40
f 4.00
m 3.56
m 3.80
f 2.30
f 3.95
m 3.90
m 4.00
m 2.00
f 4.00
f 2.80
m 3.70
m 2.98
f 3.89
m 4.00
f 3.90
m 1.90
m 2.90
f 1.50
f 2.67
m 3.80
m 2.35
f 2.90
f 3.70
f 4.00
m 3.78
m 4.00
f 3.98

And here is the code I have so far:
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. void openFiles();
  9. void averageGpa();
  10. void initialize();
  11. void sumGrades();
  12. void printResult(ofstream& outp, char gender, double avg);
  13.  
  14.  
  15. main()
  16. {
  17. int numMale=0;
  18. int numFemale=0;
  19. double averageFemale= 0.00;
  20. double averageMale = 0.00;
  21. double gpa = 0.00;
  22. double sumGpaFemale = 0.00;
  23. double sumGpaMale = 0.00;
  24. char gender;
  25. openFiles();
  26. cout << "Processing grades "<< endl;
  27. cout << "Output data is in file Ch7_Ex4Data.txt " << endl;
  28. cout << " "<< endl;
  29. system("pause");
  30. return 0;
  31. }
  32. void openFiles()
  33. {
  34. int numMale=0;
  35. int numFemale=0;
  36. double averageFemale= 0.00;
  37. double averageMale = 0.00;
  38. double gpa = 0.00;
  39. double sumGpaFemale = 0.00;
  40. double sumGpaMale = 0.00;
  41. char gender;
  42. ifstream infile;
  43. ofstream outfile;
  44.  
  45. infile.open ("c:\\ch7_Ex4data.txt");
  46. if (!infile)
  47. {
  48. cout << "InFile not found" << endl;
  49. cout << "Program cannot execute." << endl;
  50.  
  51. }
  52.  
  53.  
  54.  
  55. outfile.open ("c:\\ch7_Ex4out.txt");
  56.  
  57. outfile << fixed << showpoint;
  58. outfile << setprecision(2);
  59.  
  60. infile >> gender >> gpa;
  61. while (infile >> gender >> gpa)
  62. {
  63. outfile << gender << " " << gpa << endl;
  64. }
  65.  
  66. }
Here is my outfile so far (why does it change? It is missing the first line from the infile)
f 4.00
m 3.56
m 3.80
f 2.30
f 3.95
m 3.90
m 4.00
m 2.00
f 4.00
f 2.80
m 3.70
m 2.98
f 3.89
m 4.00
f 3.90
m 1.90
m 2.90
f 1.50
f 2.67
m 3.80
m 2.35
f 2.90
f 3.70
f 4.00
m 3.78
m 4.00
f 3.98



I know I am not going in the right direction! First when I try to output the data from the infile to the outfile the first and last numbers are missing in the outfile!
I know this code looks like a mess, and it is homework! So any advice or examples to lead me in the right direction would be greatly appreciated! I am not asking for someone to do the homework for me I just a better explanation as to the steps to take to complete this program.........Please any help will be beneficial, as i am not grasping the concepts very well
Thank you!!
Last edited by Ancient Dragon; Jul 17th, 2008 at 9:46 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 26
Reputation: 2fac323 is an unknown quantity at this point 
Solved Threads: 0
2fac323 2fac323 is offline Offline
Light Poster

Re: Newbie needs direction!!

 
0
  #2
Jul 17th, 2008
Ok I solved the problem with the infile data missing the first line when sending to outfile. The line- infile >> gender >> gpa; before the while statement was making it skip a line. I removed that line and replaced the while statement with-
while (!infile.eof())
{
infile >> gender >> gpa;
outfile << gender << " " << gpa << endl;
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Newbie needs direction!!

 
0
  #3
Jul 17th, 2008
Originally Posted by 2fac323 View Post
Ok I solved the problem with the infile data missing the first line when sending to outfile. The line- infile >> gender >> gpa; before the while statement was making it skip a line. I removed that line and replaced the while statement with-
while (!infile.eof())
{
infile >> gender >> gpa;
outfile << gender << " " << gpa << endl;
You fixed it the wrong way. All you had to do was delete line 60 and leave the rest alone. You had that loop coded correct, and replaced it with bad code.

The instructions say nothing about openFile() function reading the files. All that function is supposed to do is open the file, so you need to pass the ifstream and ofstream to that function by reference.
  1. void openFile(ifstream& in, ofstream& out)
  2. {
  3.  
  4. }
Nowhere in any of those instructions are you told when to read the input file, so I presume the input file is read in sumGrades() and again in averageGrade().
Last edited by Ancient Dragon; Jul 17th, 2008 at 10:09 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 26
Reputation: 2fac323 is an unknown quantity at this point 
Solved Threads: 0
2fac323 2fac323 is offline Offline
Light Poster

Re: Newbie needs direction!!

 
0
  #4
Jul 17th, 2008
Thank you! I will change back. May I ask why the-
while (!infile.eof())
{
infile >> gender >> gpa;
outfile << gender << " " << gpa << endl;

is considered bad code??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,625
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1496
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Newbie needs direction!!

 
0
  #5
Jul 17th, 2008
Originally Posted by 2fac323 View Post
Thank you! I will change back. May I ask why the-
while (!infile.eof())
{
infile >> gender >> gpa;
outfile << gender << " " << gpa << endl;

is considered bad code??
Because it doesn't work, that's why. eof() is known only AFTER an attempt to read after end-of-file. The construct you posted attempts to look-ahead and see if the next read will be eof(), which of course can't be done. Consequently the last line of the file will be processed twice.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC