尝试将数字放入 ifstream 文件中的变量中
Trying to put numbers into variables from an ifstream file
这是输入文件:
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
部分生产线。
相关文章:
- 通过多个头文件使用常量变量
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 无法通过头文件保存变量的值
- 尝试调用 .h 文件中定义的变量时出现变量未定义错误
- 在 C++ 中将双精度变量写入二进制文件
- 如何使用文件中的文本,例如变量
- 定义C++新的环境变量并在 bat 文件中使用它
- 在头文件中定义变量不会出错
- 复制文件流C++静态变量
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 在C++中使用变量而不是"#define"来指定数组大小是不是一种糟糕的做法?(C错误:在文件范围内
- C++将文本从.txt文件解析为变量
- 从 C 中的变量获取文件的路径,C++
- 使用boost ::日志配置文件变量
- C++:帮助存储和显示输入文件变量
- 文件变量的问题
- 如何在先决条件列表中展开生成文件变量
- QT多个文件c++变量