-1

I am trying to get the averages of a set of numbers in an array and I am getting -nan(ind) as my output. I am unsure of what is causing this issue. Here is the code that I have written so far in 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);

}

Here is the file that I am trying to read from:

food2.txt D WhiteWine 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 Milk 378.88 81.19 0.0

F RizAuCumin 819.38 136.56 81.94

F GratinDePolenta 937.13 248.06 121.28

D Coffee 389.38 83.44 0.0

F RisottoAuxAsperges 841.88 140.31 84.19

F GateauDePommes 962.63 254.81 124.58

D Tea 3.00 .69 0.0

F SoupeD'Epeautre 864.38 144.06 86.44

F Tartiflette 988.13 261.56 127.88

D WaterWithGas 4.38 5.23 0.0

F Aligot 886.88 147.81 88.69

F abcdefghijklmnopqrst 999.99 268.31 131.18

Here is the full output that I am getting: output

Any help is appreciated. Thank you

user4581301
  • 29,019
  • 5
  • 26
  • 45
palmtrees
  • 1
  • 2

1 Answers1

1

Make sure you add cnt ++; to your while loop. Also try to stay away from global variables if possible. And as previously stated, here is a reason why eof is bad in a while loop

Not incrementing cnt leaves it at zero. This leads to calculating 0.0 / 0 which is NaN.

Michael Veksler
  • 7,337
  • 1
  • 16
  • 29
donpsabance
  • 348
  • 3
  • 9