一如我的产出那样管理守则:
This program will calculate the average of 10 test scores that you input
Number0
My cursor is right after the 0 and when i press enter it just displays the last line again. I know i need to change the instructions. I also know i need to use the getline command to have it display the string with a space in it but i dont know how to do that when getting input from a file. Also and more importiantly, it isnt getting the test scores from the file and it also isnt running till end of file.
法典:
/***************************************************/
/* Author: Sam LaManna */
/* Course: CSC 135 Lisa Frye */
/* Assignment: Program 4 Grade Average */
/* Due Date: 10/10/11 */
/* Filename: program4.cpp */
/* Purpose: Write a program that will process */
/* students are their grades. It will */
/* also read in 10 test scores and */
/* compute their average */
/***************************************************/
#include <iostream> //Basic input/output
#include <iomanip> //Manipulators
#include <string> //String stuff
#include <fstream>
using namespace std;
void instruct (); //Function declaration for printing instructionstring studname ();
void input (ifstream &infile, float& test1, float& test2, float& test3, float& test4, float& test5, float& test6, float& test7, float& test8, float& test9, float& test10, string& studentname); //Function declaration for input
float aver (float test1, float test2, float test3, float test4, float test5, float test6, float test7, float test8, float test9, float test10); //Function declaration for calculating average
void output (string studentname, float average); //Function declaration for output
int main()
{
float test1 = 0; //Vars (test1 - test10) for test scores
float test2 = 0;
float test3 = 0;
float test4 = 0;
float test5 = 0;
float test6 = 0;
float test7 = 0;
float test8 = 0;
float test9 = 0;
float test10 = 0;
string studentname = "a"; //Define Var for storing students name
float average = 0; //Define var for storing average
instruct(); //Function call to print instructions
ifstream infile("grades.dat");
input (infile, test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, studentname); //Function call for scores
while (!infile.eof())
{
average = aver (test1, test2, test3, test4, test5, test6, test7, test8, test9, test10); //Function call for average
output (studentname, average); //Function call for output
cin.ignore(1);
input (infile, test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, studentname); //Get new input
} //end eof
return 0;
}
/***************************************************/
/* Name: instruct */
/* Description: Print instructions to user. */
/* Paramerters: N/A */
/* Return Value: N/A */
/***************************************************/
void instruct()
{
cout << "
" << "This program will calculate the average of 10 test scores that you input. " << "
" << "
";
//Prints instructions
return;
}
/***************************************************/
/* Name: input */
/* Description: Get input */
/* Paramerters: N/A */
/* Return Value: N/A */
/***************************************************/
void input (ifstream& infile, float& test1, float& test2, float& test3, float& test4, float& test5, float& test6, float& test7, float& test8, float& test9, float& test10, string& studentname)
{
infile >> studentname;
infile >> test1 >> test2 >> test3 >> test4 >> test5 >> test6 >> test7 >> test8 >> test9 >> test10;
infile.ignore(10,
);
return;
}
/***************************************************/
/* Name: aver */
/* Description: Calculate Average */
/* Paramerters: N/A */
/* Return Value: aver */
/***************************************************/
float aver (float test1, float test2, float test3, float test4, float test5, float test6, float test7, float test8, float test9, float test10)
{
float aver = 0;
aver = test1 + test2 + test3 + test4 + test5 + test6 + test7 + test8 + test9 + test10;
aver = aver / 10;
return aver;
}
/***************************************************/
/* Name: output */
/* Description: Calculate Average */
/* Paramerters: N/A */
/* Return Value: aver */
/***************************************************/
void output (string studentname, float average) //Function declaration for output
{
cout << studentname;
cout << average;
return;
}
投入文件:
Number One
99 99 99 99 99 99 99 99 99 99
John Doe
90 99 98 89 87 90.2 87 99 89.3 91
Clark Bar
67 77 65 65.5 66 72 78 62 61 66
Scooby Doo
78 80 77 78 73 74 75 75 76.2 69
Santa Clause
89 92.5 94 95 91 89 88 90 92 91
Jessie James
45 54 55 56 66 61 65.6 67 43 54
Clara Barton
87 88 76 75.7 80 88 83 84 85 81.2
Alex Mack
55 65 66.5 67 76 77.7 66 67.8 71 70
Ann Apolis
87 88 88 88 88 85.4 81 82 89 81
Stewart Mouse
90 92 93 94 95 96 97 97.7 98 99
Sue Sloan
88.5 67.7 88.8 99.9 90.9 89 87 78 89 88
Luke Skywalker
76.7 77.8 88.8 76.7 77 88 87 86 85 80.9
Harry Potter
80 83 84 85 86 79.4 78.5 81 80 82
Mary Poppins
100 100 100 100 100 100 100 100 100 100
Last One
33 44 55 66 77 88 99 22 11 91