读入数据

Reading data in

保留这个问题没有任何意义,对任何人都没有帮助。

这应该可以解决问题。请注意,如果您实际尝试将其部署到任何地方,就会发生各种混乱。这段代码非常不安全,因为它不检查打开文件的失败、文件格式、正在读取的内容的大小,或者实际上......任何东西。

#include <stdio.h>
#include <iostream>

struct salesman
{
    char firstname[64];
    char lastname[64];
    char middleinitial[1];
    int averagecents;
    int totalcents;
};

int main ()
{
const char* inputFilename = "in.txt";
    int numberPeople = 0, weeksToHandlePerPerson = 0;
    int workweeklength = 5;
    int totalcents = 0;
    FILE * fileHandle = fopen ( inputFilename, "r" );
    fscanf ( fileHandle, "%d", &numberPeople );
    fscanf ( fileHandle, "%d", &weeksToHandlePerPerson );
    for ( int i = 0; i < numberPeople; ++i )
    {
        salesman nextsalesman;
        fscanf ( fileHandle, "%s", nextsalesman.firstname );
        fscanf ( fileHandle, "%s", nextsalesman.middleinitial ); 
        fscanf ( fileHandle, "%s", nextsalesman.lastname );

        float t1, t2, t3, t4, t5;
        fscanf ( fileHandle, "%f %f %f %f %f", &t1, &t2, &t3, &t4, &t5 );
        nextsalesman.totalcents = 100 * ( t1 + t2 + t3 + t4 + t5 );
        nextsalesman.averagecents = nextsalesman.totalcents / workweeklength;
        totalcents += nextsalesman.totalcents;
    std::cout << "salesman " << i << "total: $" << nextsalesman.totalcents / 100 << "." <<     nextsalesman.totalcents % 100
        << " and average $" << nextsalesman.averagecents / 100 << "." << nextsalesman.    averagecents % 100 << std::endl;
    }
    int averagecents = totalcents / ( numberPeople * weeksToHandlePerPerson );
std::cout << "total for all: " << totalcents / 100 << "." << totalcents % 100 << " and     average for all $" <<
        averagecents / 100 << "." << averagecents % 100 << std::endl;
    return 0;
}