English 中文(简体)
A. 从档案和另一个未知问题的角度处理
原标题:Getline from a file and another unknown issue
  • 时间:2011-11-21 22:17:04
  •  标签:
  • c++

一如我的产出那样管理守则:

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
最佳回答
infile >> studentname;

this line will get only the string value in the stream up until the first white space. that is why the next >>(float) operations wont work - you have an invalid float value on your cursor.

http://www.cplus.com/vis/iostream/istream/operator%3E%3E/"rel=“nofollow”>cplus.com/a。

str - Extracts characters and stores them as a c-string (i.e. in successive locations starting at location pointed by str and terminated by a null-character). Extraction ends when the next character is either a valid whitespace or a null character, or if the End-Of-File is reached. The terminating null character is automatically appended after the extracted characters. The extraction operation can be limited to a certain number of characters (thus avoiding the possibility of buffer overflow) if the field width (which can be set with ios_base::width or setw) is set to a value greater than zero. In this case, the extraction ends one character before the count of characters extracted reaches the value of field width, leaving space for the ending null character. After a call to this extraction operation the value of the field width is automatically reset to zero.

问题回答

如果你想要一个简单的解决办法,那就不要求你做大量改写,那么你可能只是把名字分成头和最后的名字,用你已经读过的名字。 从技术上讲,你有两条插座(有2个名字),必须作为座标处理,否则,你将第二名改为你的浮标。

因为你想要学生名字成为一整条一线,而这是你应该使用的第一个词。

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

就产出而言,你可以把产出与投入相提并论,并在字面上添加一个空间。 此外,我增加了一条新路线,如果你不希望你能够用另一个空间或你想要的任何东西来取代它的话,最后一条。

//Replace:
  cout << studentname;
  cout << average;     
//With:
  cout << studentname <<     << average << endl;




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签