尝试在数组中查找平均值时获取-nan(ind)输出(c ++)

时间:2019-02-21 02:26:47

标签: c++ arrays

我试图获取数组中一组数字的平均值,并且我得到-nan(ind)作为输出。我不确定导致此问题的原因。这是我到目前为止用C ++编写的代码:

#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

void input_from_file(ifstream & inFile, char * item, char * name, double & price, double & cost, double & material_cost);
double array_price[50], array_total_cost[50];
double price, base_cost, material_cost;
int cnt = 0, i;
double calAverage(double arr[], int s);
char item[2]{ "" };
char name[21];
double total_cost = base_cost + material_cost;
double profit = price - total_cost;



void File_Input(ifstream & Main_File)
{
    do {
        string File_Name;
        cout << "Please enter the file name including extension: ";
        cin >> File_Name;
        Main_File.open(File_Name);
    } while (!Main_File);
}

void File_Read(ifstream & The_File)
{
    cout << setw(29) << left << "Name" << right << setw(10) << "Price" << setw(10) << "Cost" << setw(10) << "Profit" << "\n" << endl;
    string Typed_File;
    int increment = { 0 };
    while (!The_File.eof())
    {
        input_from_file(The_File, item, name, price, base_cost, material_cost);
        total_cost = base_cost + total_cost;
        profit = price - total_cost;
        array_total_cost[increment] = total_cost;
        array_price[increment] = price;
        increment++;
        cout << setw(29) << left << name << right << setw(10) << price  << setw(10) << total_cost << setw(10) << profit << endl;
    };
    cout << "\n\nThe average price is " << calAverage(array_price, cnt);
    cout << "\nThe average cost is " << calAverage(array_total_cost, cnt) << " \n";
}

void input_from_file(ifstream & inFile, char * item, char * name, double & price, double & base_cost, double & material_cost) 
{
    inFile >> item;
    inFile >> name;
    inFile >> price;
    inFile >> base_cost;
    inFile >> material_cost;
    array_price[cnt] = price;
    array_total_cost[cnt] = base_cost + material_cost;
}

double calAverage(double arr[], int s)
{
    int i;
    double sum = 0;
    for (i = 0; i < s; i++)
    {
        sum += arr[i];
    }
    return (sum/(double)(s));
}

int main()
{
    ifstream The_File;

    File_Input(The_File);

    File_Read(The_File);

}

这是我要从中读取的文件:

food2.txt D白葡萄酒3.5 0.75 0.0

F Courgetts 751.88 125.31 75.19

F BroccoliAuRoquefort 860.63 227.81 111.38

D RedWine 357.88 76.69 0.0

F CoqAuVin 774.38 129.06 77.44

F GratinDePates 886.13 234.56 114.68

F GnocchiAuxLegumes 368.38 78.94 55.23

F Raviole 796.88 132.81 79.69

F PatesFraichesAuNoix 911.63 241.31 117.98

D牛奶378.88 81.19 0.0

F RizAuCumin 819.38 136.56 81.94

F GratinDePolenta 937.13 248.06 121.28

D咖啡389.38 83.44 0.0

F RisottoAuxAsperges 841.88 140.31 84.19

F GateauDePommes 962.63 254.81 124.58

D茶3.00 .69 0.0

F SoupeD'Epeautre 864.38 144.06 86.44

Tartiflette 988.13 261.56 127.88

D含气水4.38 5.23 0.0

F Aligot 886.88 147.81 88.69

F abcdefghijklmnopqrst 999.99 268.31 131.18

这是我得到的完整输出: output

感谢您的帮助。谢谢

1 个答案:

答案 0 :(得分:1)

确保将cnt ++;添加到while循环中。如果可能,还尝试远离全局变量。如前所述,hereeof在while循环中不好的原因

不递增cnt会使它保持为零。这将导致计算0.0 / 0,即NaN。