尝试将数字放入 ifstream 文件中的变量中

Trying to put numbers into variables from an ifstream file

本文关键字:文件 变量 ifstream 数字      更新时间:2023-10-16

这是输入文件:

Attendance: 5
Midterm: 20
Final: 20
Homework: 15
Projects: 40
Mc Pherson, David Patrick
Attendance: 12 15
Midterm: 80 100
Homework: 50 100
Homework: 60 100
Homework: 80 100
Project: 90 100
Project: 80 100
Project: 75 100
Final: 80 100

我的任务是获取此输入文件,然后将其放入将输出成绩簿的代码中。我目前坚持的部分是试图将数字放入变量中并跳过它们之前的单词。我尝试使用 cin.ignore(),但这不起作用,或者至少在我的实现中不起作用。

我该怎么做这样的事情?

编辑:对于那些,在这里问是我的代码。主文件:

//This file tests out gradebook.h
#include <iostream>
#include <fstream>
#include "gradebook.h"
using namespace std;
int main()
{   
ifstream in ( "gradebook.txt" );
ostream out ( "grades.txt" );
while ( !in.fail() ) //tests to see if there are any more numbers coming in from resistors.h
{
    //process input
    gradebook ( in, cout );
}
in.close();
return 0;
}

这是我的另一个文件:

//This program will format a gradebook
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
using namespace std;
void gradebook ( istream& in, ostream& out )
{
string firstname[2];
string lastname[2];
string dummy;
//header for table
int projwe;
int hwwe;
int attendwe;
int midwe;
int finalwe;
int attendgr[2];
int midgr[2];
int hwgr1[2];
int hwgr2[2];
int hwgr3[2];
int projgr1[2];
int projgr2[2];
int projgr3[2];
int finalgr[2];
int attendgrade;
int hwgrade1;
int hwgrade2;
int hwgrade3;
int projgrade1;
int projgrade2;
int projgrade3;
int midtermgrade;
int finalsgrade;
double averagegr;
//weights for each category
in >> dummy >> attendwe;
in >> dummy >> midwe;
in >> dummy >> finalwe;
in >> dummy >> hwwe;
in >> dummy >> projwe;
in >> firstname[2];
in >> lastname[2];
in >> dummy >> attendgr[2];
in >> dummy >> midgr[2];
in >> dummy >> hwgr1[2];
in >> dummy >> hwgr2[2];
in >> dummy >> hwgr3[2];
in >> dummy >> projgr1[2];
in >> dummy >> projgr2[2];
in >> dummy >> projgr3[2];
in >> dummy >> finalgr[2];
//loads words and weights into above variables
attendgrade = ( ( attendgr[0]*attendwe ) + ( attendgr[1]*attendwe ) )/2;
//calculates the overall attendance grade
hwgrade1 = ( ( hwgr1[0]*hwwe ) +  ( hwgr1[1]*hwwe ) ) /2;
hwgrade2 =  ( ( hwgr2[0]*hwwe ) +  ( hwgr2[1]*hwwe ) ) /2;
hwgrade3 =  ( ( hwgr3[0]*hwwe ) +  ( hwgr3[1]*hwwe ) ) /2;
//calculates the overall homework grade
projgrade1 = ( ( projgr1[0]*projwe ) + ( projgr1[1]*projwe ) ) /2;
projgrade2 = ( ( projgr2[0]*projwe ) + ( projgr2[1]*projwe ) ) /2;
projgrade3 = ( ( projgr3[0]*projwe ) + ( projgr3[1]*projwe ) ) /2;
//calculates the overall project grade
midtermgrade = ( ( midgr[0]*midwe ) + ( midgr[1]*midwe ) ) /2;
//calculates the overall midterm grade
finalsgrade = ( ( finalgr[0]*finalwe ) + ( finalgr[1]*finalwe ) ) /2;
//calculates the overall finals grade
averagegr = ( finalsgrade + midtermgrade + projgrade1 + projgrade2 + projgrade3 + hwgrade1 + hwgrade2 + hwgrade3 + attendgrade ) / 9;
//calculates the average grade
out << "LastName" << "," << "FirstName" << "," << "HW1" << "," << "HW2" << "," << "HW3" << "," 
<< "Proj1" << "," << "Proj2" << "," << "Proj3" << "," << "Midterm" << "," << "Final" << "," << "Average" << endl;
//outputs the header for the table
out << firstname << "," << lastname << "," << attendgrade << "," << hwgrade1 << "," << hwgrade2 << "," << hwgrade3 << ","
<< projgrade1 << "," << projgrade2 << "," << projgrade3 << "," << midtermgrade << "," << finalsgrade << "," << averagegr << endl;
//outputs the numbers in the table
}

现在我得到一个奇怪的错误,它在主的第 10 行:

error C2664: 'std::basic_ostream<_Elem,_Traits>::basic_ostream(std::basic_streambuf<_Elem,_Traits> *,bool)' : cannot convert parameter 1 from 'const char [11] to std::basic_streambuf<+Elem,_Traits> *'
string dummy;
int n;
fin >> dummy >> n;

我试图猜测格式:它会是一个标题,带有每个元素的权重,后跟带有名称的记录,以及多个结果,每个结果由一个元素类型指定_n_ m,其中元素类型是指 中给出的元素标题,n 此评估的点数,以及m 最大点数。 在这种情况下,最好的解决方案可能是逐行解析,然后搜索 ':' . 如果没有':',它是一个名称,否则,你有解析:

std::string name;
std::string line;
while ( std::getline( fin, line ) ) {
    std::string::const_iterator colon = std::find( line.begin(), line.end(), ':' );
    if ( colon == line.end() ) {
        name = line;
    } else if ( name.empty() ) {
        parseHeaderLine( std::string( line.begin(), colon ), std::string( colon + 1, line.end() ) );
    } else {
        parseDataLine( name, std::string( line.begin(), colon ), std::string( colon + 1, line.end() ) );
    }
}

至于解析,你可以在两者上使用std::istringstream部分生产线。