输出Struct的Enum返回负数 [英] Outputting an Enum of a Struct returns negative number

查看:96
本文介绍了输出Struct的Enum返回负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将input.dat文件存储到结构数组中,并输出作为枚举的福特,雪佛兰,本田和丰田的总数。工作了好几个小时后,我遇到了障碍。当我在Visual Studio中调试代码时,我在输出中收到负数。我被困住了,真的可以对我所缺少的内容进行解释。

My goal is to store the input.dat file into an array of a struct and output the total number of Fords, Chevys, Hondas, and Toyotas which are enumerations. I've hit a roadblock after working on this for quite a few hours. When I debug the code in Visual studio i receive negative numbers in the output. I'm stuck and could really use an explanation as to what I'm missing.

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

enum CarType
{
        Ford,
        Chevy,
        Honda,
        Toyota
};

struct CarCustomer
{
        string firstName;
        string lastName;
        double price;
        CarType carType;
};

void calcCarStats(CarCustomer arrCustomers[], int count, int carCount[])
{
        for(int index = 0; index < count; index++)
        {
                carCount[arrCustomers[index].carType]++;
        }
}

void displayCarTypeCounts(int carCount[])
{
        for(int index = Ford; index <= Toyota; index++)
        {
                cout //<< translateCarTypeToText((CarType)index) << " "
                        << carCount[index] << endl;
        }
}

int _tmain(int argc, _TCHAR* argv[])
{
        int count = 0;
        CarCustomer arrCustomers[100]; //Array of structs for the Struct CarCustomer
        CarCustomer carCustomer;
        int carCount[100];
        int carTypeInt;
        double carPriceSum[100];
        double carPriceAvg[100];
        ifstream fin;
        CarType carType; //CarType enum

        fin.open("input.dat");

        if(!fin)
        {
                cout << "Error opening file, check the file name" << endl;
                _getch();
                return -1;
        }

        while (!fin.eof())
        {
                fin >> arrCustomers[count].firstName;
                fin >> arrCustomers[count].lastName;
                fin >> arrCustomers[count].price;
                fin >> carTypeInt;
                arrCustomers[count].carType = (CarType)carTypeInt;  //carType entry is an enum
                count++;

                //debug
                /*
                cout << arrCustomers[count].firstName << endl;
                cout << arrCustomers[count].lastName << endl;
                cout << arrCustomers[count].price << endl;
                cout << arrCustomers[count].carType << endl;
                cout << "------------------------" << endl;
                count++;
                */
        }
        fin.close();


        //displayCarTypeCounts(carCount);
        calcCarStats(arrCustomers, count, carCount);
        displayCarTypeCounts(carCount);


        _getch();
        return 0;
}

//<------Contents of Input.dat------------------>

Joe     Smith   5999.99 0
Mary    Doe     23999.99        1
Joe     Green   1999.99 1
Jim     Smith   4999.99 2
Jane    Green   3999.99 0
Mark    Doe     9999.99 1
John    Peters  7999.99 2
Jim     Green   8999.99 3
Mindy   Doe     3999.99 2
Janet   Green   6999.99 1
Mork    Doe     2999.99 3
Jane    Smith   3999.99 3
John    Roberts 15999.99        1
Mandy   Doe     12999.99        0
Janet   Smith   6999.99 0
Macy    Doe     14999.99        1


推荐答案

在开始递增之前将数组初始化为零。

Initialize your arrays to zero before you start incrementing.

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

enum CarType
{
  Ford,
  Chevy,
  Honda,
  Toyota
};

struct CarCustomer
{
  string firstName;
  string lastName;
  double price;
  CarType carType;
};

void calcCarStats(CarCustomer arrCustomers[], int count, int carCount[])
{
  for(int index = 0; index < count; index++)
  {
    carCount[arrCustomers[index].carType]++;
  }
}

void displayCarTypeCounts(int carCount[])
{
  for(int index = Ford; index <= Toyota; index++)
  {
    cout //<< translateCarTypeToText((CarType)index) << " "
        << carCount[index] << endl;
  }
}

int main(int, char* [])
{
  int count = 0;
  CarCustomer arrCustomers[100]; //Array of structs for the Struct CarCustomer
  int carCount[100] = {0};
  double carPriceSum[100] = {0};
  double carPriceAvg[100] = {0};

  ifstream fin("input.dat");

  if(!fin)
  {
    cout << "Error opening file, check the file name" << endl;
    return -1;
  }

  while (true)
  {
    // Joe     Smith   5999.99 0
    int carTypeInt;
    CarCustomer carCustomer;
    fin >> carCustomer.firstName
      >> carCustomer.lastName
      >> carCustomer.price
      >> carTypeInt;
    carCustomer.carType = (CarType)carTypeInt;  // carType entry is an enum
    if (!fin) { break; }
    arrCustomers[count++] = carCustomer;
  }
  fin.close();


  calcCarStats(arrCustomers, count, carCount);
  displayCarTypeCounts(carCount);

  return 0;
}

这篇关于输出Struct的Enum返回负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆