Thursday, July 9, 2009

C++ Inputing a file. Please help?

For research purposes and to better help students, the admissions office of your local university wants to know how well female and male student GPAs for certain courses. Due to confidentiality, the letter code f is used for fimale students and m 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 uptputs the average GPA for both females and male students. Format your results to two decimal places.





That is the question and this is what i have so far --%26gt;





//include section


#include %26lt;iostream%26gt;


#include %26lt;conio.h%26gt; //for use with getch


#include %26lt;string%26gt; //string data types


#include %26lt;fstream%26gt; // in and out data








using namespace std;





int main ()


{


string name,majorname,coursename;


ifstream inData;


ofstream outData;


double f;


double m;


double average;


char gender=' ';








inData.open("input.txt")

C++ Inputing a file. Please help?
Your syntax for the fstream class looks correct. What is the error output from the debugger?





Try this tutorial


http://www.cplusplus.com/doc/tutorial/fi...
Reply:function void calculateAndPrintAverages() {





double femaleScoreTotal;


double maleScoreTotal;


int numFemaleScores;


int numMaleScores;





ifstream inFile ("filename") ;


string line;


while ( !inFile.eof ) {





line %26lt;%26lt; inFile.getLine;


if ( line.split(" ")[0] == "f" ) {


femaleScoreTotal += lineline.split(" ")[1];


numFemaleScore++;


}


else {


maleScoreTotal += line.split(" ")[1];


numMaleScore++;


}


}





cout %26lt;%26lt; "Male average " %26lt;%26lt; maleScoreTotal / numMaleScore %26lt;%26lt; endl;


cout %26lt;%26lt; "Female average " %26lt;%26lt; femaleScoreTotal / numFemaleScore;





System.exit(1);





}
Reply:speaking of syntax ...


just wanted to help you a bit with your grammar ... something many of us forget to check in our efforts to clean up just the code.





-cout%26lt;%26lt;"Hello there, What is your name?"%26lt;%26lt; endl;


... End the sentence after "there" instead of using a comma


// could use a (!) or a (.)





-you have used 'your' instead of you're incorrectly a few times





-in written text it is improper to use conjunctives such as 'you're' for 'you are', this is only acceptable in the form of writing out "quoted speech" ... in this instance you should write it out in full.





cout%26lt;%26lt;"What the name of the course YOU ARE taking for your major "%26lt;%26lt;majorname%26lt;%26lt;" ?"%26lt;%26lt;endl;


AND


cout%26lt;%26lt;"Please enter your degree YOU ARE majoring in."%26lt;%26lt;endl;


//I'd suggest changing the "your" above to "the" as it is better english








TIP: programs always look great when they capitalize input on things such as Major and Name, even when it was not input by the user as such. This "added intelligence" adds a little extra to the programs worth in the eyes of the user (or professor!)


No comments:

Post a Comment