I have got this error: "error: request for member 'nume' in 'tablou[j]', which is of non-class type ' [100]'", and I don't really know how to solve it.I tried searching on youtube and google but I found nothing .Does anyone have any ideas for how to solve this?
#include <iostream>
#include <bits/stdc .h>
using namespace std;
struct{
int counter;
char nume[20] = " ";
}tablou[10][100];
int main()
{
int n, counter = 0;
char second[10][100];
bool verify = true;
cout<<"Cate nume?";
cin>>n;
for(int i = 0; i <= n; i )
{
cin.getline(second[i],20);
}
for(int i = 0; i <= n; i )
{
verify = true;
for(int j = 0; j < i; j )
{
if(strcmp(second[i], tablou[j].nume) == 0)
{
verify = false;
}
}
if(verify == true)
{
strcpy(tablou[i].nume, second[i]);
for(int k = 0; k < n; k )
{
if(strcmp(tablou[i].nume, second[k]))
{
tablou[i].counter ;
}
}
}
}
for(int i = 0; i <= n; i )
{
cout<<tablou[i].nume<<" "<<tablou[i].counter<<endl;
}
return 0;
}
CodePudding user response:
tablou is a 2d array
struct{
int counter;
char nume[20] = " ";
}tablou[10][100];
its elements are tablou[x][y]
you try to access an element with only one index
if(strcmp(second[i], tablou[j].nume) == 0)
----------------------------^
I do not know what your code is trying to do , but thats why you get the error
CodePudding user response:
The array tablou
is a two-dimensopnal array
struct{
int counter;
char nume[20] = " ";
}tablou[10][100];
So for example the expression tablou[j]
has the array type the_unnamed_structure[100].
So such an expression like this
tablou[j].nume
is incorrect and the compiler issues an error.
Maybe actually you mean the following declaration of the array
struct{
int counter;
char nume[20] = " ";
}tablou[10];
Also in these loops
for(int i = 0; i <= n; i )
{
verify = true;
for(int j = 0; j < i; j )
{
if(strcmp(second[i], tablou[j].nume) == 0)
{
verify = false;
}
}
if(verify == true)
{
strcpy(tablou[i].nume, second[i]);
for(int k = 0; k < n; k )
{
if(strcmp(tablou[i].nume, second[k]))
{
tablou[i].counter ;
}
}
}
}
some elements of the array tablou
can be skipped if verify
is set to false
because you are using the index i
to assign elements of the array tablou
. That is the number of actual elements of the array tablou
can be less than n
. In this case this for loop
for(int i = 0; i <= n; i )
{
cout<<tablou[i].nume<<" "<<tablou[i].counter<<endl;
}
will invoke undefined behavior because the data member counter
will be uninitialized for some outputted elements of the array.
You need to support a separate variable as an index in the array tablou
.