내 코드의 모든 내용이 파일에서 가져 와서 평균을 계산하고 출력합니다. 그것은 단지 그 자체로 모든 일을 즉시하지 않습니다. 내가 입력하면 파일의 끝까지 다음 줄을 반복합니다. 한 번에 모든 줄을 그렇게 만드는 방법은 무엇입니까?파일 끝까지 파일 자체가 반복되지 않습니다.
코드 :
/***************************************************/
/* 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 << "\n" << "This program will calculate the average of 10 test scores that are read from a file. " << "\n" << "\n";
//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)
{
getline(infile, studentname);
infile >> test1 >> test2 >> test3 >> test4 >> test5 >> test6 >> test7 >> test8 >> test9 >> test10;
infile.ignore(10, '\n');
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;
}
감사합니다. 그것은 효과가있었습니다. –
좋아요, 이제는 함수 매개 변수를 정리하는 것에 대해 생각해보십시오. 그레이더가 그 선언을 즐긴다는 것을 의심하십시오. –