hello

I have some errors in my program, which as always have to do with how to correctly syntax something :P ... I think :)

1st issue: I am using a 2d array as a parameter in a function but I am getting an error. I think it has to do with my syntax.
2nd Issue: When I use some float parameters within the 2d array I get an error

the 2nd error is:

invalid types `float*[float]' for array subscript

Function

void enterPurchase(float pur[], float nWeek, float counter) {

	float purchase = 0;

	while (!cin.fail() && cin>>purchase) {   // while a correct input is input

		cin >> purchase;                 // enter recent purchase
		pur[nWeek][counter] = purchase;  // 2nd Issue error occurs here		counter++;
	}

	for (int i=0; i<counter; i++) {
		cout << "Purchase No." << i+1 << ": $";
		cout << pur[nWeek][i] << "\n";  // 2nd Issue error occurs here
	}
}

whole program: with 1st issue

#include <cstdlib>
#include <iostream>

using namespace std;

float pur[53][25];   // Purchases Array [week number][purchase no.1, p2, pn...]

void printMenu();
void printBudget();
void getWeek(int& week);
void enterPurchase(float pur[], float nWeek, float counter);


int main()
{
	char option;
	int week = 0;

	while (option!='3' && !cin.fail()) {
		printMenu();
		cin >> option;

		switch (option) {

		case '1':
			getWeek(week);
			enterPurchase(pur,week,0);  // 1st issue error
			//printBudget(week);
			break;
		case '2':
			//printBudget();
			break;
		default:
			cout << "Incorrect input \n";
			break;
		}
	}


	//system("PAUSE");
	return EXIT_SUCCESS;
}

void printMenu() {

	cout << "\tPersonal Budget " << endl << endl;
	cout << " 1 : Add Purchase " << endl;
	cout << " 2 : Print Budget Summary " << endl;
	cout << " 3 : Quit " << endl << flush;

}

void printBudget() {


}

void getWeek(int& week) {


}

void enterPurchase(float pur[], float nWeek, float counter) {

	float purchase = 0;

	while (!cin.fail() && cin>>purchase) {   // while a correct input is input

		cin >> purchase;                 // enter recent purchase
		pur[nWeek][counter] = purchase;  // store recent purchase in relevent array element
		counter++;
	}

	for (int i=0; i<counter; i++) {
		cout << "Purchase No." << i+1 << ": $";
		cout << pur[nWeek][i] << "\n";
	}
}

Recommended Answers

All 4 Replies

change to

void enterPurchase(float pur[][25], float nWeek, float counter);

change to

void enterPurchase(float pur[][25], float nWeek, float counter);

That solved the 1st issue :) thanks

The second issue still seems to be occuring still

When accessing elements of array, the index number must
be a integral type.

so

int Array[4];
Array[4] = 1;//valid
Array[2] = 9;//valid
Array[-3] = 3; //valid but a bug
Array[3.14] = 3; //Wrong

Your code :

pur[nWeek][counter] = purchase;

Booth nWeek and counter is of type float. So either change
the parameters or typecast like so :

pur[ int (nWeek) ][ int (counter) ] = purchase;

The above is dangerous and could be buggy

commented: Got there first +36

What - that you can't use a float to subscript an array?

The error message should have been pretty obvious.

Array subscripts are integers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.