2011-11-21 3 views
-1

내 코드의 모든 내용이 파일에서 가져 와서 평균을 계산하고 출력합니다. 그것은 단지 그 자체로 모든 일을 즉시하지 않습니다. 내가 입력하면 파일의 끝까지 다음 줄을 반복합니다. 한 번에 모든 줄을 그렇게 만드는 방법은 무엇입니까?파일 끝까지 파일 자체가 반복되지 않습니다.

코드 :

/***************************************************/ 
/* 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; 
} 

답변

2

는 입력 한 숯불 때까지 프로그램 실행을 차단하고, 주에서 while 루프에서

cin.ignore(1); 

를 제거합니다.

아, 그리고 당신이 그것을 모두 한 줄에 인쇄 이유에 대해 새로운 질문을 열기 전에 당신이 라인을 배출하는 데 사용 CIN에 공급 입력 때문입니다하지만 지금은 당신이 당신의 출력에 endl을 추가해야

cout << blah << bleh << endl; 
당신의 main 기능에
+0

감사합니다. 그것은 효과가있었습니다. –

+0

좋아요, 이제는 함수 매개 변수를 정리하는 것에 대해 생각해보십시오. 그레이더가 그 선언을 즐긴다는 것을 의심하십시오. –

2

, 당신은 cin에서 입력 문자/대기를 추출하고이를 폐기

cin.ignore(1); 

을 사용하고 있습니다. 이에 대한 자세한 내용은 istream::ignore 참조를 참조하십시오.

이 문을 제거/주석 처리하면 문제가 해결됩니다.

관련 문제