11 Replies - 820 Views - Last Post: 14 September 2011 - 02:47 PM Rate Topic: -----

#1 Queen_Aries  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-September 11

Keep getting error of undeclared identifier when it has been declared

Posted 14 September 2011 - 11:08 AM

Hello,

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;

			
		}
}





Is This A Good Question/Topic? 0
  • +

Replies To: Keep getting error of undeclared identifier when it has been declared

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4131
  • View blog
  • Posts: 12,844
  • Joined: 25-December 09

Re: Keep getting error of undeclared identifier when it has been declared

Posted 14 September 2011 - 11:16 AM

When you get errors post the complete error message exactly as they appear in your development environment.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 Queen_Aries  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-September 11

Re: Keep getting error of undeclared identifier when it has been declared

Posted 14 September 2011 - 11:23 AM

line 64 MaxIndex undeclared identifier
line 65 MinIndex undeclared identifier
line 67 PrintData cannot convert parameter 12 from 'void (_cdecl*)(std::ifstream &, double, int, double)' to double
line 96 CandyTotal undeclared identifier
line 109 and 110 RCost undeclared identifier
line 119 and 120 BCost undeclared identifier
line 129 and 130 GCost undeclared identifier
line 142 error C2109: subscript requires array or pointer type
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Keep getting error of undeclared identifier when it has been declared

Posted 14 September 2011 - 11:35 AM

:/ I think you need to read up on how to use functions a little more. Your errors are because you have not declared these variables.

Ture they were in the function declarations at the top of the program but those are just argument names for use WITHIN the function.

if I have:

void MyFunction(int Argument);

I don't call it as:

MyFunction(Argument);

unless I have declared a variable named Argument in the local scope. I would use it like this:

int someValue = 10;
MyFunction(someValue);


So I would take some local variable and pass it in as the parameter. Inside of the function the parameter is named "Argument" since that is what the first argument to this function was named.


So take a deep breath and try to work though a couple of examples on how to use functions.
Was This Post Helpful? 0
  • +
  • -

#5 Queen_Aries  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-September 11

Re: Keep getting error of undeclared identifier when it has been declared

Posted 14 September 2011 - 11:52 AM

I have done some tweaking to my program and I'm now down to 3 errors.

line 68 error C2064 term does not evaluate to a function taking 3 arguments.
line 69 error C2064 term does not evaluate to a funciton taking 3 arguments.

line 146 error C2109 subscript requires array or pointer type.

NickDMax- I'm trying really hard. I've had 2 programs due and a test today. I managed to get the first one done on my own, but this program is throwing me off a bit.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4131
  • View blog
  • Posts: 12,844
  • Joined: 25-December 09

Re: Keep getting error of undeclared identifier when it has been declared

Posted 14 September 2011 - 11:53 AM

Please post your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 Queen_Aries  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-September 11

Re: Keep getting error of undeclared identifier when it has been declared

Posted 14 September 2011 - 11:55 AM

I have done some tweaking to my program and I'm now down to 3 errors.

line 68 error C2064 term does not evaluate to a function taking 3 arguments.
line 69 error C2064 term does not evaluate to a funciton taking 3 arguments.

line 146 error C2109 subscript requires array or pointer type.



#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], int CandyTotal[3]);
void Price (ifstream &infile, int RedCandies[5], int BlueCandies[5], int GreenCandies[5], double RCost[5], double BCost[5], double GCost[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], double MaxCost);
void MinCost (ifstream &infile, double Total[5], 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];
	int MaxIndex;
	int MinIndex;
	
	double RCost[5];
	double BCost[5];
	double GCost[5];
	double Total[5];
	double AvgCost;
	double Candies;
	double MaxCost;
	double MinCost;
	
	

	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, CandyTotal);
	Price (infile, RedCandies, BlueCandies, GreenCandies, RCost, BCost, GCost);
	TotalCost (infile, RCost, BCost, GCost, Total);
	AverageCost (infile, Total);
	MaxCost (infile, Total, MaxCost);
	MinCost (infile, Total, 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 CandyTotal[])
{
	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[], double RCost[], double BCost[], double GCost[])
{
	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] = BlueCandies[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++)
	{
		double 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[], double MaxCost)
{
	int i;
	double MaximumCost;
	MaximumCost = Total[0];
	for (i = 0; i < 5; i++)
	{
		if(Total[i] > MaximumCost)
		{
			MaximumCost = Total[i];
			int MaxIndex = i;
			MaxCost = MaximumCost;
		}
	}
}

void MinCost(ifstream &infile, double Total[], double MinCost)
{
	int i;
	double MinimumCost;
	MinimumCost = Total[0];
	for (i = 0; i < 5; i++)
	{
		if(Total[i] < MinimumCost)
		{
			MinimumCost = Total[i];
			int 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;

			
		}
}




Was This Post Helpful? 0
  • +
  • -

#8 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Keep getting error of undeclared identifier when it has been declared

Posted 14 September 2011 - 12:15 PM

Look at here:
void MaxCost(ifstream &infile, double Total[], double MaxCost)
void MinCost(ifstream &infile, double Total[], double MinCost)

Here you are using the same name for function and parameter (MaxCost,MinCost).

Also look here:
for (i = 0; i < 5; i++)
	{
		double Total = 0;
		Total = RCost[i] + BCost[i] + GCost[i];
		infile >> Total[i];
	}

You are declaring Total which hides the Total[].

This post has been edited by aaa111: 14 September 2011 - 12:20 PM

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4131
  • View blog
  • Posts: 12,844
  • Joined: 25-December 09

Re: Keep getting error of undeclared identifier when it has been declared

Posted 14 September 2011 - 12:16 PM

These are the errors I get when I compile your code:

Quote

main.cpp||In function ‘int main(int, char**)’:|
main.cpp|65|error: ‘MaxCost’ cannot be used as a function|
main.cpp|66|error: ‘MinCost’ cannot be used as a function|
main.cpp||In function ‘void TotalCost(std::ifstream&, double*, double*, double*, double*)’:|
main.cpp|141|warning: declaration of ‘double Total’ shadows a parameter|
main.cpp|136|warning: shadowed declaration is here|
main.cpp|143|error: invalid types ‘double[int]’ for array subscript|


The first two are on lines 65 and 66

	double MaxCost;
	double MinCost;

	MaxCost (infile, Total, MaxCost); // line 65
	MinCost (infile, Total, MinCost); // line 66


Look closely at these four lines. You have defined a variable named MaxCost and MinCost and you have functions with the same name, you can not have functions with the same name as variables. I would suggest that you rename your variables to something different.

Next:

Quote

main.cpp|141|warning: declaration of ‘double Total’ shadows a parameter|
main.cpp|136|warning: shadowed declaration is here|


void TotalCost(ifstream &infile, double RCost[], double BCost[], double GCost[], double Total[]) // Line 136
{
	for (i = 0; i < 5; i++)
	{
		double Total = 0;  // Line 141



You have a parameter name Total and in the last line you create a new variable named Total. You probably want to use your parameter so you should delete the "double" on line 141.

And finally:

Quote

main.cpp|143|error: invalid types ‘double[int]’ for array subscript

	    double Total = 0;
		Total = RCost[i] + BCost[i] + GCost[i];
		infile >> Total[i];


When you created the new variable with the name Total above you created a single variable not an array. So when you delete the "double" you should fix this error. This is why you should always fix the errors in order, fixing one error may fix others.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 Queen_Aries  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 07-September 11

Re: Keep getting error of undeclared identifier when it has been declared

Posted 14 September 2011 - 12:44 PM

Ok, now I have new errors. 6 warnings.

warning C4101: 'MaxIndex' : unreferenced local variable (line 31)
warning C4101: 'MinIndex' : unreferenced local variable (line 32)
warning C4700: uninitialized local variable 'MaxCost' used (line 68)
warning C4700: uninitialized local variable 'MinCost' used (line 69)
warning C4700: uninitialized local variable 'Candies' used (line 71)
warning C4700: uninitialized local variable 'AvgCost' used (line 71)



#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], int CandyTotal[3]);
void Price (ifstream &infile, int RedCandies[5], int BlueCandies[5], int GreenCandies[5], double RCost[5], double BCost[5], double GCost[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 Max_Cost (ifstream &infile, double Total[5], double MaxCost);
void Min_Cost (ifstream &infile, double Total[5], 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];
	int MaxIndex;
	int MinIndex;
	
	double RCost[5];
	double BCost[5];
	double GCost[5];
	double Total[5];
	double AvgCost;
	double Candies;
	double MaxCost;
	double MinCost;
	
	

	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, CandyTotal);
	Price (infile, RedCandies, BlueCandies, GreenCandies, RCost, BCost, GCost);
	TotalCost (infile, RCost, BCost, GCost, Total);
	AverageCost (infile, Total);
	Max_Cost (infile, Total, MaxCost);
	Min_Cost (infile, Total, 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 CandyTotal[])
{
	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[], double RCost[], double BCost[], double GCost[])
{
	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] = BlueCandies[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++)
	{
		double 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 Max_Cost(ifstream &infile, double Total[], double MaxCost)
{
	int i;
	double MaximumCost;
	MaximumCost = Total[0];
	for (i = 0; i < 5; i++)
	{
		if(Total[i] > MaximumCost)
		{
			MaximumCost = Total[i];
			int MaxIndex = i;
			MaxCost = MaximumCost;
		}
	}
}

void Min_Cost(ifstream &infile, double Total[], double MinCost)
{
	int i;
	double MinimumCost;
	MinimumCost = Total[0];
	for (i = 0; i < 5; i++)
	{
		if(Total[i] < MinimumCost)
		{
			MinimumCost = Total[i];
			int 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;

			
		
}




Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is online

  • member icon


Reputation: 4131
  • View blog
  • Posts: 12,844
  • Joined: 25-December 09

Re: Keep getting error of undeclared identifier when it has been declared

Posted 14 September 2011 - 01:07 PM

First, your code does not match your error messages.

Quote

warning C4101: 'MaxIndex' : unreferenced local variable (line 31)

  double RCost[5]; // line 31 


Quote

warning C4700: uninitialized local variable 'MaxCost' used (line 68)
warning C4700: uninitialized local variable 'MinCost' used (line 69)
warning C4700: uninitialized local variable 'Candies' used (line 71)
warning C4700: uninitialized local variable 'AvgCost' used (line 71)


You are trying to use these variables before you have assigned any values to them.

In the following function, you do know that the value you assign to MaxCost in this function is lost when the function returns?

void Max_Cost(ifstream &infile, double Total[], double MaxCost)
{
	int i;
	double MaximumCost;
	MaximumCost = Total[0];
	for (i = 0; i < 5; i++)
	{
		if(Total[i] > MaximumCost)
		{
			MaximumCost = Total[i];
			int MaxIndex = i;
			MaxCost = MaximumCost;
		}
	}
}


Jim

This post has been edited by jimblumberg: 14 September 2011 - 01:07 PM

Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Keep getting error of undeclared identifier when it has been declared

Posted 14 September 2011 - 02:47 PM

Look, Queen_Aries this is not really how to go about making software. Programmers don't sit down and write 300 lines of code and hit compile and see what happens.

Programming is done in steps. First we code and test one function, when it compiles and is working then we add another function, compile and test... so on and so forth until we have the program. Programming is an incremental activity done in baby-steps not giant leaps. (one programmer's baby step may be another's giant leap though).

So lets just see if we can get something that compiles first:
#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 main()
{
    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);
    
    //display data
    for(int i = 0; i < 5; ++i) 
    {
        cout << Name[i] << "\t" << RedCandies[i] << "\t" << BlueCandies[i] << "\t" << GreenCandies[i] << endl;
    }
    
    //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;
//
//            
//        }
//}



test this and see if it compiles and if it does, does it read the data in properly.

If so... then un-comment the next function and test it etc. i.e. build your program up don't sit down and write a bunch of code, cross your figures and hope for the best. It is much more approachable this way and you *should* spend less time tracking down compiler error after compiler error just to find that the program doesn't work when put all together.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1