C++ program and i am stuck!
-
QUESTION: Here is my program. I cannot get it to work ..following that are the instructions..any advice would be appreciated using namespace std; // function prototypes void calculateAverage (ifstream&, ofstream& , double&); void calculateGrade (double); int main() { // Declare Vatriables double avg, average = 0; int count =0; string name; // Declare file streams ifstream infile; ofstream outfile; // opens the input file infile.open ("Ch7_Ex9Data.txt"); // Display error if file was not opened if ( !infile.is_open() ) { cout return 1; } // opens the output file outfile.open ("Ch7_ExOut.txt"); outfile // read name from input file infile >> name; //break loop when all data is read while ( !infile ) { } // write the name to the output file outfile > score; outp if (myAvg if (myAvg return 'C'; else if (myAvg // Declare Vatriables double avg, average = 0; int count =0; string name; // Declare file streams ifstream infile; ofstream outfile; // opens the input file infile.open ("Ch7_Ex9Data.txt"); // Display error if file was not opened if ( !infile.is_open() ) { cout outfile // read name from input file infile >> name; //break loop when all data is read while ( !infile ) { } // write the name to the output file outfile //read and then write the test scroes from file calculateAverage ( infile, outfile, avg ); // write average and grade to output file outfile // Keep track of class count and Average count++; average += avg; // Compute class average average /=count; // write class average to output file outfile //Close file Streams infile.close (); outfile.close (); return 0; } // returns the average void calculateAverage ( ifstream& in, ofstream& out, double& avg ) { // declare variable int score; // initialize the average avg = 0; // read scores from input file ,write to output // and compute the scores sum for ( int i = 0 ; i > score; out // compute average of the student avg /= 5.0; } } // returns the grade of the student char calculateGrade (double myAvg) { if (myAvg } ---------- FOLLOW-UP ---------- QUESTION: Okay I got it run and thereare no errors but I hit a wall with the out put Here is how it needs to look ( Im just showing the firat 2 lines, The number mean nothing. I just typed them out ) Student Test1 Test2 Test3 Test4 Test5 Average Grade Johnson 85 83 77 91 76 85 B Aniston 80 90 74 95 48 85 B And here is the ERROR FREE progam ( again it works its just the output) // include statement(s). // using namespace statement. using namespace std; // function prototypes void calculateAverage(ifstream& _inFile, ofstream& _outFile, double& _runningAverage); char calculateGrade( double _grade); const int numTests = 5; int main() { // Declare Vatriables int count = 0; double runningAverage =0; string studentName; // Declare file streams ifstream inFile; ofstream outFile; // opens the input file inFile.open ("Ch7_Ex9Data.txt"); // Display error if file was not opened if ( !inFile.is_open() ) { cout // Read and write a students name from the input file inFile >> studentName; while(inFile) { count++; outFile > studentName; } outFile double testsAverage = 0; for(int i=0; i > test; testsTotal += test; _outFile << setw(10) << test; } testsAverage = testsTotal / numTests; _runningAverage += testsAverage; _outFile << setw(10) << testsAverage; _outFile << setw(10) << calculateGrade(testsAverage) << "\n"; } // determines and returns the students letter grade char calculateGrade (double _grade) { if (_grade < 60) return 'F'; else if (_grade < 70) return 'D'; else if (_grade < 80) return 'C'; else if (_grade < 90) return 'B'; else return 'A'; } Thank you SO much in advance
-
Answer:
Hello Jason Well, I think I got the output you want. I had to change many of the setw calls, and left justify the student names. My changes are marked with //XXX . For the test scores, I shortened the field to 5 characters and printed a space after the score. For the average, I rounded to the nearest integer and printed the integer. That eliminated the decimal point. You should understand that the setw call formats the next thing being printed, not the previous. So, where you had: outFile The first setw should be before "Student", not after. The last setw has no effect in the above line. Honestly, I don't use output formatting at all in my work, so I hope my answer is satisfactory. Here is the entire program again: // using namespace statement. using namespace std; // function prototypes void calculateAverage(ifstream& _inFile, ofstream& _outFile, double& _runningAverage); char calculateGrade( double _grade); const int numTests = 5; int main() { // Declare Vatriables int count = 0; double runningAverage =0; string studentName; // Declare file streams ifstream inFile; ofstream outFile; // opens the input file inFile.open ("Ch7_Ex9Data.txt"); // Display error if file was not opened if ( !inFile.is_open() ) { cout // Opens the output file outFile.open ("Ch7_ExOut.txt"); outFile // Read and write a students name from the input file inFile >> studentName; while(inFile) { count++; outFile > studentName; } outFile //Close file Streams inFile.close (); outFile.close (); return 0; } // XXX added nearestInt function int nearestInt(double x) { return (int)(x+0.5); } // deterime the average of the 5 test scores for each student // Read and write a students test scores void calculateAverage(ifstream& _inFile, ofstream& _outFile, double& _runningAverage) { int testsTotal = 0; int test = 0; double testsAverage = 0; for(int i=0; i > test; testsTotal += test; _outFile testsAverage = testsTotal / numTests; _runningAverage += testsAverage; _outFile } // determines and returns the students letter grade char calculateGrade (double _grade) { if (_grade }
Miningco.com Visit the source
Related Q & A:
- How Can I use .net dll in C program?Best solution by Stack Overflow
- How to run an executable from within a c program?Best solution by Stack Overflow
- Is there any free program that I could download to enhance my voice?Best solution by Yahoo! Answers
- Can anyone help me with this C++ program?Best solution by Yahoo! Answers
- What country should I do my foreign exchange in and what program should I use?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.