I am stuck

I keep getting a error code saying subscript requires array or pointer

Page 1 of 1

5 Replies - 929 Views - Last Post: 08 December 2008 - 03:58 AM Rate Topic: -----

#1 egile  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-September 08

I am stuck

Post icon  Posted 07 December 2008 - 09:34 PM

I am working on a program and this what I must do, Create a class based on TMoney. TMoney only contains three denominations, tdollars, twons (worth
1/5 or .2 of a dollar) and tpennies.
Create an array of three Tmoney objects. Use a loop to input data into each of the elements. Print out
the data for the second Tmoney entered.
● inputdata prompts the user for input and then stores the values they enter in the object
● outputdata prints the information stored in the object
I am stuck because I keep getting this error code for line 31 and 48 the error code is C2109 it says subscript requires array or pointer type.


 #include <iostream>
using namespace std;

class TMoney
{
private:
	int tdollars;
	int twons;
	int tpennies;

	
public:
	TMoney():tdollars(0),twons(0),tpennies(0){}
	void getinputdata();
	void outputdata();


};




int main()
{	
	TMoney;
		int money;
			money[3];
	int tdollars,twons,tpennies;
	


	for (int j = 0; j < 3; j++)
	{
	cout << "Enter the number of tdollars you own:";
		cin >> tdollars;
	cout << "Enter the number of twons you own:";
		cin >> twons;
	cout << "Enter the number of tpennies you own:";
		cin >> tpennies;
	}
	for (int j=0; j<1; j++)
	{
		cout << "You have  tdollars," << "  twons," << "  tpennies."  << endl;
			cin >>  money[tdollars][twons][tpennies];
			

	}

	



	return 0;
}



thanks for all help in advance

Is This A Good Question/Topic? 0
  • +

Replies To: I am stuck

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: I am stuck

Posted 07 December 2008 - 09:38 PM

Several issues, first:

money[3];




Isn't declared to be anything. You have a semi colon after money and that cuts off the ability of multiple variable declaration in one line.

Also you only have a single dimensional array. If you want one like you coded you need to declare it like this:

int money[size][size][size];



Also, this code segment makes no sense:

 for (int j=0; j<1; j++)
    {
        cout << "You have  tdollars," << "  twons," << "  tpennies."  << endl;
            cin >>  money[tdollars][twons][tpennies];
           
    }



I get trying to input into your array, but this won't work. Clarification into your intentions would be helpful.
Was This Post Helpful? 0
  • +
  • -

#3 GWatt  Icon User is offline

  • member icon

Reputation: 270
  • View blog
  • Posts: 3,068
  • Joined: 01-December 05

Re: I am stuck

Posted 07 December 2008 - 09:41 PM

An array is a special variable that allows you hold multiple values with a single name. The [] allow you to "subscript" the array which is simply a fancy term for getting a value at a particular location in the array.
Anyway, you declare an array like this:
int arrayName[length];
except give an actual number for "length," and a more descriptive name for the variable.

In your main function, you declare an int "money" and then attempt to receive the value at subscript 3. If you're trying to declare money as an array of 3 ints, then you want to do this:
int money[3];

Also, just having "TMoney" there without a variable name will cause errors.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: I am stuck

Posted 07 December 2008 - 09:42 PM

View Postegile, on 7 Dec, 2008 - 08:34 PM, said:

I am stuck because I keep getting this error code for line 31 and 48 the error code is C2109 it says subscript requires array or pointer type.


I assume this is line 31
money[3];


and this is line 48:
cin >>  money[tdollars][twons][tpennies];



Am I right?

Have a think about your use of arrays here.
Do they seem consistent and logical?
Was This Post Helpful? 0
  • +
  • -

#5 egile  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-September 08

Re: I am stuck

Posted 07 December 2008 - 10:17 PM

View PostGWatt, on 7 Dec, 2008 - 08:41 PM, said:

An array is a special variable that allows you hold multiple values with a single name. The [] allow you to "subscript" the array which is simply a fancy term for getting a value at a particular location in the array.
Anyway, you declare an array like this:
int arrayName[length];
except give an actual number for "length," and a more descriptive name for the variable.

In your main function, you declare an int "money" and then attempt to receive the value at subscript 3. If you're trying to declare money as an array of 3 ints, then you want to do this:
int money[3];

Also, just having "TMoney" there without a variable name will cause errors.

Thank you for you input it is very helpful Earl

View Postjanotte, on 7 Dec, 2008 - 08:42 PM, said:

View Postegile, on 7 Dec, 2008 - 08:34 PM, said:

I am stuck because I keep getting this error code for line 31 and 48 the error code is C2109 it says subscript requires array or pointer type.


I assume this is line 31
money[3];


and this is line 48:
cin >>  money[tdollars][twons][tpennies];



Am I right?

Have a think about your use of arrays here.
Do they seem consistent and logical?

yes you are right thank you for the help

View PostKYA, on 7 Dec, 2008 - 08:38 PM, said:

Several issues, first:

money[3];




Isn't declared to be anything. You have a semi colon after money and that cuts off the ability of multiple variable declaration in one line.

Also you only have a single dimensional array. If you want one like you coded you need to declare it like this:

int money[size][size][size];



Also, this code segment makes no sense:

 for (int j=0; j<1; j++)
    {
        cout << "You have  tdollars," << "  twons," << "  tpennies."  << endl;
            cin >>  money[tdollars][twons][tpennies];
           
    }



I get trying to input into your array, but this won't work. Clarification into your intentions would be helpful.

I was trying to output tdollars and twons and tpennies all on one line but using the array. Thank you for all you help.
Was This Post Helpful? 0
  • +
  • -

#6 Linkowiezi  Icon User is offline

  • D.I.C Regular

Reputation: 58
  • View blog
  • Posts: 316
  • Joined: 07-October 08

Re: I am stuck

Posted 08 December 2008 - 03:58 AM

Hi I changed a little in your code, see the comments:
If you do as stated within those comments your program should run fine :)
#include <iostream>
using namespace std;

class TMoney
{
  private:
    int tdollars;
    int twons;
    int tpennies;
  
  public:
    TMoney():tdollars(0),twons(0),tpennies(0){}
    void getinputdata();
    void outputdata();
};

int main()
{
  const int size = 3;   //  Added this line so you can easily change the input for everything just changeing this
  TMoney money[size];   //  Changed your declaration of the money variable
  
  for (int j = 0; j < size; j++)  //  Changed the 3 to size
  {
    money[j].getinputdata();  //  Added this
/*  Put all of this inside your getinputdata() function within the class.
    cout << "Enter the number of tdollars you own:";
    cin >> tdollars;
    cout << "Enter the number of twons you own:";
    cin >> twons;
    cout << "Enter the number of tpennies you own:";
    cin >> tpennies;
*/
  }
  
/*  Change this and ask the user for input on what money[x] he wants to se
    and then call the outoutdata() function from the class where you cout everything.
  for (int j=0; j<1; j++)
  {
    cout << "You have  tdollars," << "  twons," << "  tpennies."  << endl;
    cin >>  money[tdollars][twons][tpennies];
  }
*/
  return 0;
}

Was This Post Helpful? 1

Page 1 of 1