[Error] invalid types int[int] for array subscript

Jan 10, 2018 at 3:54pm
I try to vompile my program but what ever i do i get this error. I know where the problem is i just don't know how to solve it.

this is the declaration:



struct a0

{

char marka[50] = "AUDI";

char model[50] = "R8 SPYDER";

int obujam[50] = {5204};

int masa[50] = {1720};

int max_snaga_kw[50] = {386};

int max_snaga_ks[50] = {535};

int max_brzina[50] = {313};

float ubrzanje[50] = {4.1};

}a0;



and this is the problem:


if (player.front() == x)

{

player.pop();

cout<<"Your car is "<<a[x-1].marka<<" "<<a[x-1].model<<". It has "<<a[x-1].obujam[50]<<"ccm of Engine Displacement. It weights "<<a[x-1].masa[50]<<"kg. It's power is "<<a[x-1].max_snaga_kw[50]<<"kW and "<<a[x-1].max_snaga_ks[50]<<"KS. It has a top speed of ";

cout<<a[x-1].max_brzina[50]<<"km/h and it's acceleration from 0 to 100 km/h is "<<a[x-1].ubrzanje[50]<<"s.\n";

game.push(x);


problem is whit a[x+1].something(marka, model,...).
If you know how to resolve this please help.
Thanks in advance.
Jan 10, 2018 at 4:49pm
a[x-1]

This is an attempt to use an array named a. I don't see you creating an array named a.
Jan 10, 2018 at 4:51pm
Like struct a0 i have them with different cars up to a55. So i was thinking off writing it like this within a loop. Every time loop would do its thing x would be one number up and it would print out different cars.
Jan 10, 2018 at 5:14pm
I'm not sure, but it might be that you're passing the array around wrongly.
Can you show:
- where you define a
- where you define game
- where you define x
- the signature of the function you're calling a[x-1] from

In general, you're more likely to get useful help if you can recreate a short, but complete program that still reproduces your error. "Short, Self Contained, Compilable, Example" http://sscce.org/

Also, you have 56 different structs named a0 to a55? Sounds like there's some ugly design flaw happening here...
Last edited on Jan 10, 2018 at 5:19pm
Jan 10, 2018 at 5:20pm
I see.

You want a[x-1] to mean a0, and then when x=2, to mean a1.

C++ doesn't work like that. a[7] does not mean "the variable named a7". It means "element number 8 in the array named a".

You need to read up on arrays.
Jan 10, 2018 at 5:21pm
Weeeel shit then. Is there any way i can do something in the way I tried?
Jan 10, 2018 at 5:25pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
struct car
{
string marka;
string model;
int obujam;
int masa;
int max_snaga_kw;
int max_snaga_ks;
int max_brzina;
float ubrzanje;
};

vector<car> allCars (50);  // create a vector of 50 car objects

for (int i = 0; i < allCars .size(); ++i)
{
  // do something with allCars[i], like set some values
}
Jan 10, 2018 at 5:26pm
thanks for the help i will try it right away.
Jan 10, 2018 at 5:31pm
I just don't understand one thing. How am i suposted to set values for all the cars if each and every one has different stats?
Jan 10, 2018 at 5:38pm
If there's no pattern to the 50 or so different cars, you'll just have to manually assign each one

1
2
3
4
allCars[0] = { "mark", "model",    1, 2, 3, 4, 5,  6.5 }; // replace with actual values
allCars[1] = { "mark2", "model2",  1, 2, 3, 4, 5,  7.8 };
// etc.
allCars[49] = { ... };


You could load each one from a file, if you put the necessary information in a file format, would you want to see that?
Jan 10, 2018 at 5:39pm
No i can put that in my curent project. I think it would actualy take less space. Again thank you very much.
Last edited on Jan 10, 2018 at 5:40pm
Jan 10, 2018 at 5:47pm
Is it going to be an error beacuse mark and model are words?
Jan 10, 2018 at 10:56pm
Sorry, I'm not sure what you're asking.

If you're talking about the difference between mark and "mark", for example

string mark = "foo"; mark here is the variable name, of type string.

string my_mark = "mark"; "mark" is the string literal in this case, which is assigned to the variable called my_mark. "mark" is just a string literal, not the name of an actual object.
Jan 11, 2018 at 10:36am
In the example you gave me: allCars[0] = { "mark", "model", 1, 2, 3, 4, 5, 6.5 };

It won't do it for me beacuse mark and model are names of the cars. For me it should look like

this: allCars[0] = { "mark", "model", something, something, 3, 4, 5, 6.5 };

Or i misunderstood you and you meant that instead of mark and model I put actual names of

the cars.
Jan 11, 2018 at 11:35am
Hello niksacokica,

In his case: allCars[0] = { "mark", "model", 1, 2, 3, 4, 5, 6.5 }; "mark" and "model" are just place holders. You would need to change this to the car information that is needed.

Hope that helps,

Andy
Jan 11, 2018 at 12:06pm
Thank you that helped a lot
Topic archived. No new replies allowed.