小编mok*_*wi8的帖子

为什么要计算两次的最后一个数字?

我的程序正在从文件中读取数字 - 它会读取最后一个数字两次.我的程序有什么问题,我该怎么做才能修复它?

int main()
{

    ifstream inputFile;

    int number = 0; //the input for the numbers in the file
    double total = 0; //the total of the numbers
    double counter = 0;//number of numbers
    double average = 0;//average of the number



    inputFile.open("random.txt");//open file
    if (!inputFile)//test for file open errors
    {
        cout<<"Error \n";
    }



    while (inputFile)
     {
        inputFile >> number ;
        total+=number;
        counter++;
        cout<<"The running total is: " <<total <<"\n";

     }


    total=total*1.00;
    counter=counter*1.00;

    average = total/counter;
    cout<<"\nthe final total is: …
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
2
解决办法
1384
查看次数

标签 统计

c++ ×1