Array passing between function

This is a discussion on Array passing between function within the C++ Programming forums, part of the General Programming Boards category; The code does not compile running into an error in the function userSelectItem(budget budgetData[]) *******Compile*******Error******* 1>------ Build started: Project: Project4, ...

  1. #1
    six
    six is offline
    Registered User
    Join Date
    Apr 2010
    Posts
    4

    Array passing between function

    The code does not compile running into an error in the function userSelectItem(budget budgetData[])

    *******Compile*******Error*******
    1>------ Build started: Project: Project4, Configuration: Debug Win32 ------
    1> main.cpp
    1>c:\users\ahoffman\documents\personal\school\cs36 2\project4_new\project4\project4\main.cpp(417): error C2664: 'isCategoryInArray' : cannot convert parameter 1 from 'budget []' to 'budget'
    1> No constructor could take the source type, or constructor overload resolution was ambiguous
    1>c:\users\ahoffman\documents\personal\school\cs36 2\project4_new\project4\project4\main.cpp(426): error C2664: 'isCategoryInArray' : cannot convert parameter 1 from 'budget []' to 'budget'
    1> No constructor could take the source type, or constructor overload resolution was ambiguous
    1>c:\users\ahoffman\documents\personal\school\cs36 2\project4_new\project4\project4\main.cpp(432): error C2664: 'NumberOfItemsInArray' : cannot convert parameter 1 from 'budget' to 'budget []'
    1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    1>c:\users\ahoffman\documents\personal\school\cs36 2\project4_new\project4\project4\main.cpp(436): error C2676: binary '[' : 'budget' does not define this operator or a conversion to a type acceptable to the predefined operator
    1>c:\users\ahoffman\documents\personal\school\cs36 2\project4_new\project4\project4\main.cpp(436): error C2228: left of '.categoryName' must have class/struct/union
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Code:
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    const int MAXIMUM_BUDGET_ITEMS = 500;
    const int MAXIMUM_USER_INPUT_CATEGORIES = 100;
    
    enum categoryType {income, expense};
    
    struct budget{
    	categoryType budgetCategory;
    	string categoryName;
    	double categoryAmount;
    	bool cleared;
    };
    void SetupBudget(budget budgetData[]);
    void QueryuserNewOldData(budget budgetData[]);
    bool LoadData(budget budgetData[]);
    void AddData(budget budgetData[]);
    int NumberOfItemsInArray(budget budgetData[]);
    bool StringHasSpace(string str);
    double GetCategoryAmount();
    bool GetCleared();
    bool GetCategoryType();
    char GetUserInput();
    string GetCategoryName();
    budget ClearBudgetItems();
    int GetMenuSelection(int lower, int upper);
    void DisplayBudget(budget budgetData[]);
    void SaveData(budget budgetData[]);
    void MainMenu(budget budgetData[]);
    void DeleteBudgetItem(budget budgetData[]);
    string userSelectItem(budget budgetData[]);
    bool isCategoryInArray(budget budgetData, string categoryNameString);
    
    int main()
    {
    	budget budgetData[MAXIMUM_BUDGET_ITEMS];
    	SetupBudget(budgetData);
    	QueryuserNewOldData(budgetData);
    	MainMenu(budgetData);
    
    	return 0;
    }
    void SetupBudget(budget budgetData[])
    {
    	for (int i = 0; i < MAXIMUM_BUDGET_ITEMS; i++)
    	{
    		budgetData[i] = ClearBudgetItems();
    	}
    }
    void QueryUserNewOldData(budget budgetData[])
    {
    	char userChoice = '\0';
    	
    	userChoice = GetUserInput();
    
    	if (userChoice == 'E')
    	{
    		if (LoadData(budgetData) == false)
    		{
    			AddData(budgetData);
    		}
    	}
    	if (userChoice == 'N')
    	{
    		AddData(budgetData);
    	}	
    }
    bool LoadData(budget budgetData[])
    {
    	bool success = false;
    	char categoryType;
    	char cleared;
    	ifstream inFile;
    	inFile.open("BUDGET.TXT");
    	int i = 0;
    		while (inFile.is_open() && !inFile.eof())
    		{
    			inFile >> categoryType >> budgetData[i].categoryName >> budgetData[i].categoryAmount >> cleared;
    
    			if (categoryType == 'I')
    			{
    				budgetData[i].budgetCategory = income;
    			}
    			if (categoryType == 'E')
    			{
    				budgetData[i].budgetCategory = expense;
    			}
    			if (cleared == 'Y')
    			{
    				budgetData[i].cleared = true;
    			}
    			if (cleared == 'N')
    			{
    				budgetData[i].cleared = false;
    			}
    
    			success = true;
    			i++;
    		}
    		if (!inFile || inFile.eof() || success == false)
    		{
    			cout << endl << "Could not open file to load data." << endl
    				<< "Please enter some data!" << endl;
    		}
    	inFile.close();
    	return success;
    }
    void AddData(budget budgetData[])
    {
    	char keepEntering = '\0';
    	int limit = 0;
    	limit = NumberOfItemsInArray(budgetData);
    	for 
    		(int i = 0; 
    		(limit + i) < (limit + MAXIMUM_USER_INPUT_CATEGORIES) 
    		&& (limit + i) < MAXIMUM_BUDGET_ITEMS;
    		i++)
    	{
    		if (GetCategoryType() == true)
    		{
    			budgetData[i+limit].budgetCategory = income;
    		}
    		else
    		{
    			budgetData[i+limit].budgetCategory = expense;
    		}
    		budgetData[i+limit].categoryName = GetCategoryName();
    		budgetData[i+limit].categoryAmount = GetCategoryAmount();
    		budgetData[i+limit].cleared = GetCleared();
    		do{
    			cout << endl << "Enter another item Y/N?";
    			cin >> keepEntering;
    			if (toupper(keepEntering) == 'N')
    				i = MAXIMUM_USER_INPUT_CATEGORIES;
    		} while (toupper(keepEntering) != 'N' && toupper(keepEntering) != 'Y');
    	}
    }
    int NumberOfItemsInArray(budget budgetData[])
    {
    	int items = 0;
    	bool quit = false;
    	for (int i = 0; quit == false; i++)
    	{
    		if (budgetData[i].categoryName != "\0")
    			items++;
    		else
    			quit = true;
    	}
    	return items;
    }
    bool GetCategoryType()
    {
    	char userChoice = '\0';
    	do {
    	cout << "Is this an (I)ncome or (E)xpense?" << endl << "Choice : ";
    	cin >> userChoice;
    	userChoice = toupper(userChoice);
    	if (userChoice != 'I' && userChoice != 'E')
    	{
    		cout << endl << "!!!!!Please enter I or E" << endl << endl;
    	}
    	} while (userChoice != 'I' && userChoice != 'E');
    
    	if (userChoice == 'I')
    	{
    		return true;
    	}
    	if (userChoice == 'E')
    	{
    		return false;
    	}
    }
    bool GetCleared()
    {
    	char userChoice = '\0';
    	do {
    	cout << "Has this item cleared enter Y/N" << endl << "Choice : ";
    	cin >> userChoice;
    	userChoice = toupper(userChoice);
    	if (userChoice != 'Y' && userChoice != 'N')
    	{
    		cout << endl << "!!!!!Please enter Y or N" << endl << endl;
    	}
    	} while (userChoice != 'Y' && userChoice != 'N');
    
    	if (userChoice == 'Y')
    	{
    	return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    double GetCategoryAmount()
    {
    	double amount = 0;
    	do {
    		cout << "Enter the ammount for the item" << endl
    				<< "The item must be positive, i.e. 433.22" << endl
    				<< "Please enter the ammount: ";
    		cin >> amount;
    	} while (amount < 0);
    	
    	return amount;
    }
    string GetCategoryName()
    {
    	string name = "\0";
    	do {
    		cout << "Enter the name for the category" << endl
    				<< "The category name must be 15 charcters or less" << endl
    				<< "and it may not contain any spaces" << endl;
    		cin >> name;
    	} while (name.size() >= 15 || StringHasSpace(name));
    	return name;
    }
    bool StringHasSpace(string str)
    {
    	for (unsigned int i = 0; i < str.length(); i++)
    	{
    		if (str[i] == ' ')
    		return true;
    	}
    	return false;
    }
    char GetUserInput()
    {
    	char userChoice = '\0';
    	do {
    		cout << "Which Data would you like to use?" << endl << "		N - New budget data" << endl;
    		cout << "		E - Existing budget data" << endl << "Choice : ";
    		cin >> userChoice;
    		userChoice = toupper(userChoice);
    		if (userChoice != 'E' && userChoice != 'N')
    		{
    			cout << endl << "!!!!!Please enter E or N" << endl << endl;
    		}
    		} while (userChoice != 'E' && userChoice != 'N');
    	return userChoice;
    }
    budget ClearBudgetItems()
    {
    	budget b;
    	b.categoryAmount = 0.00;
    	b.categoryName = "\0";
    	b.cleared = false;
    	b.budgetCategory = income;
    	
    	return b;
    }
    int GetMenuSelection(int lower, int upper)
    {
    	int menuSelection = 0;
    	do{
    		cin >> menuSelection;
    		if (menuSelection < lower || menuSelection > upper)
    		{
    			cout << "Your selection must be between " << lower << " and " 
    					<< upper << endl << "Please try again: ";
    		}
    	} while (menuSelection < lower || menuSelection > upper);
    	return menuSelection;
    }
    void DisplayBudget(budget budgetData[])
    {
    	int numberOfBudgetItems = 0;
    	numberOfBudgetItems = NumberOfItemsInArray(budgetData);
    
    	cout << endl << "INCOME:" << endl;
    	cout << "Item Amount Cleared" << endl;
    		for (int i = 0; i < numberOfBudgetItems; i++)
    		{
    			if (budgetData[i].budgetCategory == income)
    			{
    				cout << endl << budgetData[i].categoryName << " " << budgetData[i].categoryAmount;
    				if (budgetData[i].cleared == true)
    					cout << " *";
    			}
    		}	
    	cout << endl << endl << "Expense:" << endl;
    	cout << "Item Amount Cleared" << endl;
    		for (int i = 0; i < numberOfBudgetItems; i++)
    		{
    			if (budgetData[i].budgetCategory == expense)
    			{
    				cout << endl << budgetData[i].categoryName << " " << budgetData[i].categoryAmount;
    				if (budgetData[i].cleared == true)
    					cout << " *";
    			}
    		}
    }
    void SaveData(budget budgetData[])
    {
    	char save = '\0';
    
    	do{
    		cout << "Would you like to save data Y/N?";
    		cin >> save;
    	} while ((toupper(save) != 'Y') && (toupper(save) !='N'));
    
    	if (toupper(save) == 'Y')
    	{
    		ofstream outFile;
    		outFile.open("BUDGET.TXT");
    		for (int i = 0; i < NumberOfItemsInArray(budgetData); i++)
    		{ 
    			if (budgetData[i].budgetCategory == income)
    			{
    				outFile << "I ";
    			}
    			if (budgetData[i].budgetCategory == expense)
    			{
    				outFile << "E ";
    			}
    
    			outFile << budgetData[i].categoryName << " " << budgetData[i].categoryAmount << " ";
    			
    			if (budgetData[i].cleared == true)
    			{
    				outFile << 'Y' << endl;
    			}
    			else
    			{
    				outFile << 'N' << endl;
    			}
    		}
    		outFile.close();
    	}
    }
    void MainMenu(budget budgetData[])
    {
    	bool quit = false;
    	do{
    		cout << endl << endl 
    			<< "Main Menu!" << endl
    			<< "(1)Display Budget" << endl
    			<< "(2)Add Budget Item" << endl
    			<< "(3)Find Budget Category" << endl
    			<< "(4)Delete Budget Item" << endl
    			<< "(5)Exit" << endl;
    	switch(GetMenuSelection(1, 5))
    	{
    		case 1: DisplayBudget(budgetData);
    		break;
    	case 2: AddData(budgetData);
    		break;
    	case 3: //FindData(budgetData);
    		break;
    	case 4: //DeleteData(budgetData);
    		break;
    	case 5:
    		quit = true;
    		SaveData(budgetData);
    		break;
    	}
    	}while (quit == false);
    }
    void DeleteBudgetItem(budget budgetData[])
    {
    	do{
    		string itemToDelete = userSelectItem(budgetData);
    
    		int numberOfItemsInBudget = NumberOfItemsInArray(budgetData);
    		for (int i = 0; i < numberOfItemsInBudget; i++)
    		{
    
    			if (budgetData[i].categoryName == itemToDelete)
    			{
    				for (int x = i; x < numberOfItemsInBudget; x++)
    				{	
    					categoryType budgetCategoryTemp;
    					string categoryNameTemp;
    					double categoryAmountTemp;
    					bool clearedTemp;
    
    					budgetCategoryTemp = budgetData[i].budgetCategory;
    					categoryNameTemp = budgetData[i].categoryName;
    					categoryAmountTemp = budgetData[i].categoryAmount;
    					clearedTemp = budgetData[i].cleared;
    
    					budgetData[i].budgetCategory = budgetData[i+1].budgetCategory;
    					budgetData[i].categoryName = budgetData[i+1].categoryName;
    					budgetData[i].categoryAmount = budgetData[i+1].categoryAmount;
    					budgetData[i].cleared = budgetData[i+1].cleared;
    
    					budgetData[i+1].budgetCategory = budgetCategoryTemp;
    					budgetData[i+1].categoryName = categoryNameTemp;
    					budgetData[i+1].categoryAmount = categoryAmountTemp;
    					budgetData[i+1].cleared = clearedTemp;
    					
    					if (i == numberOfItemsInBudget - 1)
    					{
    						budgetData[i] = ClearBudgetItems();
    					}
    				}
    			}
    		}
    	} while(itemToDelete != "quit");
    }
    string userSelectItem(budget budgetData[])
    {
    	string userInput = NULL;
    	do{
    		DisplayBudget(budgetData);
    	
    		cout << "Enter the name of the item you would like to delete: ";
    		cin >> userInput;
    
    		if(isCategoryInArray(budgetData, userInput))
    		{
    			return userInput;
    		}
    		else
    		{
    			cout << endl << "**Please type the category exactly as shown**" << endl;
    		}
    
    	}while(!isCategoryInArray(budgetData, userInput));
    
    	return userInput;
    }
    bool isCategoryInArray(budget budgetData, string categoryNameString)
    {
    	int itemsInArray = NumberOfItemsInArray(budgetData);
    
    	for (int i = 0; i < itemsInArray; i++) 
    	{
    		if (budgetData[i].categoryName == categoryNameString)
    		{
    			return true;
    		}
    	}
    
    	return false;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your isCategoryInArray is defined (and declared) to take just a single budget, yet you try to pass in an array of budgets (and for that matter, you treat that single budget as an array of budgets inside inCategoryInArray as well). So you should make the declaration match reality.

  3. #3
    six
    six is offline
    Registered User
    Join Date
    Apr 2010
    Posts
    4
    Thanks for the Help. That was a total brain fart...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 12:53 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21