Getline来自一个文件和另一个未知问题

Getline from a file and another unknown issue

本文关键字:文件 另一个 未知 问题 一个 Getline      更新时间:2023-10-16

当我按原样运行代码时,我的输出是:

This program will calculate the average of 10 test scores that you input
Number0

我的光标就在0之后,当我按下回车键时,它只会再次显示最后一行。我知道我需要更改说明。我也知道我需要使用getline命令让它显示带有空格的字符串,但我不知道如何在从文件中获取输入时做到这一点。此外,更重要的是,它不会从文件中获取考试成绩,也不会运行到文件末尾。

代码:

/***************************************************/
/* 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 you input. " << "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)
{
  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;
}

输入文件:

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
infile >> studentname;

这一行将只获得流中的字符串值,直到第一个空白。这就是为什么接下来的>>(float(操作不起作用——您的光标上有一个无效的float值。

来自cplusplus.com

str-提取字符并将其存储为c字符串(即从str和由空字符终止(提取在下一个字符是有效的空白或null字符,或者如果已到达文件结尾。终止的null字符为自动附加在提取的字符之后。提取操作可以限制为一定数量的字符(因此避免缓冲区溢出的可能性(,如果字段宽度(可以使用ios_base::width或setw设置(设置为大于大于零。在这种情况下,提取结束于提取的字符数达到字段宽度的值,为结尾的空字符留出空间。接到电话后提取操作字段宽度的值是自动的重置为零。

如果你想要一个不需要太多重写的简单解决方案,你可能只需要将名字拆分为名字和姓氏,用你已经在读名字的方式读取这两个名字。从技术上讲,你有两个字符串(有两个名字(,它们必须被这样对待,否则你最终会试图将第二个名字读取到一个浮动中。

因为您希望学生名称是一整行,而不是第一个单词,所以应该使用getline。

//Replace:
  infile >> studentname    
//With:
  getline( infile, studentname );

对于输出,您可以像输入一样链接输出,而要添加空格,只需在空格的文本中添加即可。此外,我添加了一行新行,如果你不想用endl,你可以用另一个空格或任何你想要的东西来代替它。

//Replace:
  cout << studentname;
  cout << average;     
//With:
  cout << studentname << ' ' << average << endl;
相关文章: