| | |
Newbie needs direction!!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 26
Reputation:
Solved Threads: 0
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:
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!!
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:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <iomanip> #include <string> using namespace std; void openFiles(); void averageGpa(); void initialize(); void sumGrades(); void printResult(ofstream& outp, char gender, double avg); main() { int numMale=0; int numFemale=0; double averageFemale= 0.00; double averageMale = 0.00; double gpa = 0.00; double sumGpaFemale = 0.00; double sumGpaMale = 0.00; char gender; openFiles(); cout << "Processing grades "<< endl; cout << "Output data is in file Ch7_Ex4Data.txt " << endl; cout << " "<< endl; system("pause"); return 0; } void openFiles() { int numMale=0; int numFemale=0; double averageFemale= 0.00; double averageMale = 0.00; double gpa = 0.00; double sumGpaFemale = 0.00; double sumGpaMale = 0.00; char gender; ifstream infile; ofstream outfile; infile.open ("c:\\ch7_Ex4data.txt"); if (!infile) { cout << "InFile not found" << endl; cout << "Program cannot execute." << endl; } outfile.open ("c:\\ch7_Ex4out.txt"); outfile << fixed << showpoint; outfile << setprecision(2); infile >> gender >> gpa; while (infile >> gender >> gpa) { outfile << gender << " " << gpa << endl; } }
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
•
•
Join Date: Jul 2008
Posts: 26
Reputation:
Solved Threads: 0
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;
while (!infile.eof())
{
infile >> gender >> gpa;
outfile << gender << " " << gpa << endl;
•
•
•
•
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;
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.
C++ Syntax (Toggle Plain Text)
void openFile(ifstream& in, ofstream& out) { }
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.
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.
![]() |
Similar Threads
- Newbie looking for advice (Community Introductions)
- lib tiff help-newbie (C)
- Image manipulations(newbie) (C++)
- vb newbie are dialogue boxes /buttons etc part of vb6 or are they drawn from windows (Visual Basic 4 / 5 / 6)
- Total Newbie Could Use A Pointer In The Right Direction (C++)
- C++ help for a newbie (C++)
- C++ Absolute Newbie (C++)
- Stupid Newbie Question (C)
- Bulletin Board Newbie (Social Media and Online Communities)
- Newbie Needs Help with this ASPX (ASP.NET)
Other Threads in the C++ Forum
- Previous Thread: anonymous array in method argument
- Next Thread: problem with copyfile()
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






