我的代码中的所有内容都可以运行,它可以从文件中获取输入,计算平均值和输出。它本身并不是一次性完成的。如果我点击输入它会执行下一行,依此类推,直到文件结束。我如何制作它以便一次性完成所有行?
代码:
/***************************************************/
/* 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;
}
答案 0 :(得分:2)
删除
cin.ignore(1);
来自main中的while循环,它阻止程序执行,直到你输入一个char。
哦,在你打开一个关于为什么它在一行上打印的新问题之前,这是因为你输入cin的输入用于推进该行,但现在你应该在输出中添加endl
cout << blah << bleh << endl;
答案 1 :(得分:2)