I have written a program that is supposed to produce the output of an example. The program will show the name of 5 students, the number of candies they purchased (Red, Blue, or Green), the total cost of each candy color, the total each student purchased, the average cost, the Max cost, the Min cost, the total candy count, and the grand total purchased.
I keep getting errors saying that at lines 97, 110, 111, 120, 121, 130, and 131.
Also at line 143, I get the error subscript requires array or pointer type.
I am not certain as to what it is that I'm doing incorrectly. Any help would be greatly appreciated.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
void ReadData (ifstream &infile, string Name[50], int RedCandies[5], int BlueCandies[5], int GreenCandies[5]);
void ChoiceTotal(ifstream &infile, int RedCandies[5], int BlueCandies[5], int GreenCandies[5]);
void Price (ifstream &infile, int RedCandies[5], int BlueCandies[5], int GreenCandies[5]);
void TotalCost (ifstream &infile, double RCost[5], double BCost[5], double GCost[5], double Total[5]);
void AverageCost (ifstream &infile, double Total[5]);
void MaxCost (ifstream &infile, double Total[5], int MaxIndex, double MaxCost);
void MinCost (ifstream &infile, double Total[5], int MinIndex, double MinCost);
void TotalCandy (ifstream &infile, int RedCandies[5], int BlueCandies[5], int GreenCandies[5]);
void PrintData (ofstream &outfile, string Name[50], int Redcandies[5], int BlueCandies[5], int GreenCandies[5],
int CandyTotal[3], double RCost[5], double BCost[5], double GCost[5], double Total[5], double AvgCost, double MaxCost,
double MinCost, double Candies);
int _tmain(int argc, _TCHAR* argv[])
{
string Name[50];
int RedCandies[5];
int BlueCandies[5];
int GreenCandies[5];
int CandyTotal[3];
double RCost[5];
double BCost[5];
double GCost[5];
double Total[5];
double AvgCost;
double Candies;
ifstream infile;
ofstream outfile;
infile.open("candies.txt");
outfile.open("report.txt");
if(!infile)
{
cout << "Error: Cannot open file candies.txt:" << endl;
exit(1);
}
if(!outfile)
{
cout << "Error: Cannot open file report.txt:" << endl;
exit(1);
}
ReadData (infile, Name, RedCandies, BlueCandies, GreenCandies);
ChoiceTotal(infile, RedCandies, BlueCandies, GreenCandies);
Price (infile, RedCandies, BlueCandies, GreenCandies);
TotalCost (infile, RCost, BCost, GCost, Total);
AverageCost (infile, Total);
MaxCost (infile, Total, MaxIndex, MaxCost);
MinCost (infile, Total, MinIndex, MinCost);
TotalCandy(infile, RedCandies, BlueCandies, GreenCandies);
PrintData (outfile, Name, RedCandies, BlueCandies, GreenCandies, CandyTotal, RCost, BCost, GCost, Total,
AvgCost, MaxCost, MinCost, Candies);
infile.close();
outfile.close();
return 0;
}
// Function Definitions
void ReadData (ifstream &infile, string Name[50], int RedCandies[], int BlueCandies[], int GreenCandies[])
{
int i;
for (i = 0; i < 5; i++)
infile >> Name[i] >> RedCandies[i] >> BlueCandies[i] >> GreenCandies[i];
}
void ChoiceTotal(ifstream &infile, int RedCandies[], int BlueCandies[], int GreenCandies[])
{
int i;
int RedTotal = 0;
int BlueTotal = 0;
int GreenTotal = 0;
for (i = 0; i < 5; i++)
{
RedTotal += RedCandies[i];
BlueTotal += BlueCandies[i];
GreenTotal += GreenCandies[i];
infile >> CandyTotal[i];
}
}
void Price (ifstream &infile, int RedCandies[], int BlueCandies[], int GreenCandies[])
{
if (RedCandies)
{
int i;
for (i = 0; i < 5; i++)
{
RCost[i] = RedCandies[i] * 5;
infile >> RCost[i];
}
}
if (BlueCandies)
{
int i;
for (i = 0; i < 5; i++)
{
BCost[i] = BlueCandeis[i] * 3;
infile >> BCost[i];
}
}
if (GreenCandies)
{
int i;
for (i = 0; i < 5; i++)
{
GCost[i] = GreenCandies[i] * 2;
infile >> GCost[i];
}
}
}
void TotalCost(ifstream &infile, double RCost[], double BCost[], double GCost[], double Total[])
{
int i;
for (i = 0; i < 5; i++)
{
int Total = 0;
Total = RCost[i] + BCost[i] + GCost[i];
infile >> Total[i];
}
}
void AverageCost (ifstream &infile, double Total[])
{
int i;
double total = 0;
double AvgCost = 0;
for (i = 0; i < 5; i++)
total += Total[i];
AvgCost = total / 5;
infile >> AvgCost;
}
void MaxCost(ifstream &infile, double Total[], int MaxIndex, double MaxCost)
{
int i;
double MaximumCost;
MaximumCost = Total[0];
for (i = 0; i < 5; i++)
{
if(Total[i] > MaximumCost)
{
MaximumCost = Total[i];
MaxIndex = i;
MaxCost = MaximumCost;
}
}
}
void MinCost(ifstream &infile, double Total[], int MinIndex, double MinCost)
{
int i;
double MinimumCost;
MinimumCost = Total[0];
for (i = 0; i < 5; i++)
{
if(Total[i] < MinimumCost)
{
MinimumCost = Total[i];
MinIndex = i;
MinCost = MinimumCost;
}
}
}
void TotalCandy (ifstream &infile, int RedCandies[], int BlueCandies[], int GreenCandies[])
{
int i;
double total1 = 0;
double total2 = 0;
double total3 = 0;
for (i = 0; i < 5; i++)
total1 += RedCandies[i];
for (i = 0; i < 5; i++)
total2 += BlueCandies[i];
for (i = 0; i < 5; i++)
total3 += GreenCandies[i];
double Candies = 0;
Candies = total1 + total2 + total3;
infile >> Candies;
}
void PrintData (ofstream &outfile, string Name[50], int RedCandies[], int BlueCandies[], int GreenCandies[],
int CandyTotal[], double RCost[], double BCost[], double GCost[], double Total[], double AvgCost,
double MaxCost, double MinCost, double Candies)
{
int i;
for (i = 0; i < 5; i++)
{
outfile << setw(25) << left << Name[i];
outfile << setprecision(2) << fixed;
outfile << setw(10) << RedCandies[i];
outfile << setw(10) << BlueCandies[i];
outfile << setw(10) << GreenCandies[i];
outfile << setw(10) << CandyTotal[i];
outfile << "$" << setw(10) << RCost[i];
outfile << "$" << setw(10) << BCost[i];
outfile << "$" << setw(10) << GCost[i];
outfile << "$" << setw(10) << Total[i];
outfile <<"Average Cost" << setw(10) << AvgCost;
outfile << "Total Candy Count" << setw(10) << Candies;
}
}

New Topic/Question
Reply



MultiQuote






|