我有麻烦从输入文本文件中跳过某些字符(C ++)

时间:2014-03-09 16:45:16

标签: c++ file-io input io

(我很抱歉,与我在本网站上看到的大部分问题相比,这是一个很低的水平,但我已经没有想法,我不知道还有谁问。)

我正在开展一个学校项目,要求我从名为in06.txt的文件中读取篮球统计数据。 in06.txt文件完全如下:

5

P 17 24 9 31 28

R 4 5 1 10 7

A 9 2 3 6 8

S 3 4 0 5 4

我需要阅读并将第一个数字5存储到名为“游戏”的变量中。从那里,我必须从第二行读取数字并确定高,低和平均值。我必须为第3,4和5行做同样的事情。(仅供参考,字母P,R,A和S表示“积分”,“反弹”,“助攻”和“抢断”。)

由于我只学习了几周的编程,所以我不想通过直接处理项目的各个方面来压倒自己。所以,我首先要确定每条线的平均值。我的计划是保持每一行的总计,然后将运行总数除以游戏数量,在这种情况下为5。

这是我的代码:

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
    int games;
    int points_high, points_low, points_total;
    int rebounds_high, rebounds_low, rebounds_total;
    int assists_high, assists_low, assists_total;
    int steals_high, steals_low, steals_total;
    double points_average, rebounds_average, assists_average, steals_average;

    ifstream fin;
    ofstream fout;

    fin.open("in06.txt");
        if( fin.fail() ) {
            cout << "\nInput file opening failed.\n";
            exit(1);
        }
        else
            cout << "\nInput file was read successfully.\n";

    int tempint1, tempint2, tempint3, tempint4;
    char tempchar;

    fin >> games;
    fin.get(tempchar);  // Takes the endl; from the text file.
    fin.get(tempchar);  // Takes the character P from the text file.

    while( fin >> tempint1 ) {
         points_total += tempint1;
    }

    fin.get(tempchar);  // Takes the endl; from the text file.
    fin.get(tempchar);  // Takes the character R from the text file.

    while( fin >> tempint2 ) {
        rebounds_total += tempint2;
    }

    fin.get(tempchar);  // Takes the endl; from the text file.
    fin.get(tempchar);  // Takes the character A from the text file.

    while( fin >> tempint3 ) {
         assists_total += tempint3;
    }

    fin.get(tempchar);   // Takes the endl; from the text file.
    fin.get(tempchar);   // Takes the character S from the text file.

    while( fin >> tempint4 ) {
         steals_total += tempint4;
    }

    cout << "The total number of games is " << games << endl;
    cout << "The value of total points is " << points_total << endl;
    cout << "The value of total rebounds is " << rebounds_total << endl;
    cout << "The value of total assists is " << assists_total << endl;
    cout << "The value of total steals is " << steals_total << endl;

    return 0;
}

这是(不正确的)输出:

Input file was read successfully.
The total number of games is 5
The value of total points is 111
The value of total rebounds is 134522076
The value of total assists is 134515888
The value of total steals is 673677934

我一直在阅读我的教科书中的文件输入几个小时,希望我能找到一些能说明我的程序输出错误值的原因。但是,我一无所获。我也在这个论坛以及其他论坛上研究过类似的问题,但是这些解决方案使用的方法我还没有学过,因此,我的老师不会在我的项目代码中允许它们。我看到的一些方法是数组和getline函数。我们还没有了解过。

注意:我的老师不希望我们存储输入文件中的每个整数。他希望我们一次打开文件并存储游戏数量,然后使用循环和if语句来确定每行的高,平均和低数字。

如果有人能帮助我,我会非常感激! 谢谢!

2 个答案:

答案 0 :(得分:0)

您声明了所有这些变量:

    int games;
    int points_high, points_low, points_total;
int rebounds_high, rebounds_low, rebounds_total;
int assists_high, assists_low, assists_total;
int steals_high, steals_low, steals_total;
double points_average, rebounds_average, assists_average, steals_average;

然后你递增它们:

 points_total += tempint1;

这些变量从未初始化为已知值(0),因此它们中有垃圾。你需要初始化它们。

答案 1 :(得分:0)

除了OldProgrammer所说的,你已经错误地读取了整数。像这样的循环

while( fin >> tempint2 ) {
        rebounds_total += tempint2;
}
发生错误时

将停止。也就是说,要么它达到EOF,要么提取遇到无法格式化为整数的数据 - 换句话说,good()返回false。它似乎认为,在一行的末尾停止阅读。一旦设置了错误标志,所有进一步的提取将失败,直到您清除标志。在你的情况下,一个循环在P之后开始读取,提取五个整数,但随后它会遇到下一行的R并出错。

将此更改为读取固定数量的整数的循环,或者使用std::getline将整行读入std::string,将其放入std::stringstream并从那里读取。

无论如何,要学会编写健壮的代码。 检查提取的成功并计算您获得的元素数量。

最多读取5个整数的循环示例:

int i;
int counter = 0;
while (counter < 5 && file >> i) {
    ++counter;
    // do something with i
}
if (counter < 5) {
    // hm, got less than 5 ints...
}