Error while using an array of Structs

Howdy, I've been following a lot of tutorials and questions here but finally came with a problem I haven't been able to solve myself, maybe I'm just very tired (It's 5am here, been up the whole day)
So I get the following error:
[Error] expected primary-expression before '[' token
[Error] expected unqualified-id before '[' token
In every array of structs passed as parameter..
I haven't actually seen an answer, or perhaps I didn't grasp it, so I decided to post.
Thanks for your help in advance! Sorry my code is a little messy!

Edit: Pay no mind to eventual words in italian, didn't translate them as they didn't have anything to do with the errors, I think..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
//#define dim 10 -- currently not working for some reason
using namespace std;

char menu()	//Menu
{char s;
 do
  {system ("cls");
   cout<<"\tSHOP\n"<<endl;
   cout<<"  a)"<<endl;
   cout<<"  b)"<<endl;
   cout<<"  x)  Exit"<<endl;
   cout<<"Input: ";
   cin>>s;
   }while (s!='a' && s!='b' && s!='x'); 
  return s;
}

char load (struct numero[], int dim)
{
	for (int i=0; i<dim; i++)
	{
		cout<<"Insert the product's name"<<endl;
		cin>>numero[i].nomeProdotto;
				
		cout<<"Insert the product's Manufacturer"<<endl;
		cin>>numero[i].produttore;
		
		cout<<"Insert the quantity"<<endl;
		cin>>numero[i].quantity;
		
		cout<<"Insert the price"<<endl;
		cin>>numero[i].prezzo;
	}
}

void sort (struct numero[]) 
{
    int temp, dim;
    int i;  int j;

    for (i=0; i<dim; i++) 
	{
        for (j=i+1; j<dim; j++) 
		{
            if (strcmp(numero[i].prezzo, numero[j].nomeProdotto) > 0) 
			{
                temp=numero[i];
                numero[i]=numero[j];
                numero[j]=temp;
            }
        }
    }
}

int main()
{
	struct scaffale
	{
		char nomeProdotto;
		char produttore;
		char quantity;
		char prezzo;
	};
	
int dim = 10;
struct scaffale numero[dim];
char s;

	do
   {
    s= menu();
    switch  (s)
      {case 'a':
            load(numero[],dim);
				cout << "Scaffale caricato.";
            system("pause");
            break;
       case 'b':
 
            cout << "Ordina Scaffali";
			sort(numero[], dim);
            system("pause");
            break;
       case 'x': break;
      }
   }while (s!='x');
return 0;
}
Last edited on
This is illegal in c++
1
2
	int dim = 10;
	struct scaffale numero[dim];


dim has to be const ie

1
2
	const int dim = 10;
	scaffale numero[dim];


please note that some compilers will allow it but it is illegal in c++

you don't need to include the [] operator when passing an array to a function
also this function does not take two arguments
sort(numero[], dim);
should be
sort(numero);


void sort (struct numero[])
should be
void sort(scaffale numero[])

char load (struct numero[], int dim) same as above

Last edited on
Thank you man!
I always have problems remembering what's illegal and what not!

I tried what you told me asap but and now I only get errors in the load function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char load (scaffale numero[], int dim)
{
	for (int i=0; i<dim; i++)
	{
		cout<<"Insert the product's name"<<endl;
		cin>>numero[i].nomeProdotto;
				
		cout<<"Insert the product's Manufacturer"<<endl;
		cin>>numero[i].produttore;
		
		cout<<"Insert the quantity"<<endl;
		cin>>numero[i].quantity;
		
		cout<<"Insert the price"<<endl;
		cin>>numero[i].prezzo;
	}
}


The errors this time being:
[Error] 'scaffale' was not declared in this scope
[Error] expected primary-expression before 'int'
[Error] expression list treated as compound expression in initializer [-fpermissive]
[Error] expected ',' or ';' before '{' token

I've tried a couple things but some resulted in the earlier errors replacing these so I'm guessing this is the right path?
In case you're curious, this program loads a shelf, with itemname, manufacturer etc and then sorts it by price, (I could work on an elaborate sort but I'm not confident on that)
Maybe later on I'll add the creation of a file and storage of the information in plain text, but I have enough problems as it's now.

Edit:Ok I removed dim (for some reason I forgot that) and ended up only with the 'scaffale' was not declared in this scope and expected ; before { token

I'm not sure what I'm supposed to do to be honest
Thank you again for your time!
Last edited on
scaffle is declared inside main at lines 63-69.

line 24,42: You have to declare a struct type here, however, the compiler hasn't seen the declaration for scaffle yet.

Move the declaration for scaffale (lines 63-69) to global scope (line 9).

Line 24 needs to be:
 
char load (scaffale numero[], int dim)


Ditto for line 42.


Last edited on
Thanks for your time, however I did as you said and just got a load of errors I am not even familiar with.
I tried some other things myself but I'm still at a stall.

If I manage to improve something I'll post it asap!

Thanks everyone for the help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <cstdio>
#include <string>
//#define dim 10 -- currently not working for some reason
using namespace std;

struct scaffale     //***  moved declaration to global scope
	{
		char nomeProdotto;
		char produttore;
		char quantity;
		char prezzo;
	};

char menu()	//Menu
{char s;
 do
  {system ("cls");
   cout<<"\tSHOP\n"<<endl;
   cout<<"  a)"<<endl;
   cout<<"  b)"<<endl;
   cout<<"  x)  Exit"<<endl;
   cout<<"Input: ";
   cin>>s;
   }while (s!='a' && s!='b' && s!='x'); 
  return s;
}

char load (scaffale numero[], int dim)  //***  changed struct to typename
{
	for (int i=0; i<dim; i++)
	{
		cout<<"Insert the product's name"<<endl;
		cin>>numero[i].nomeProdotto;
				
		cout<<"Insert the product's Manufacturer"<<endl;
		cin>>numero[i].produttore;
		
		cout<<"Insert the quantity"<<endl;
		cin>>numero[i].quantity;
		
		cout<<"Insert the price"<<endl;
		cin>>numero[i].prezzo;
	}
}

void sort (scaffale numero[],int dim)   //***  changed struct to type name
{                                       //*** added dim back
    int temp;
    int i;  int j;

    for (i=0; i<dim; i++) 
	{
        for (j=i+1; j<dim; j++) 
		{
            if (strcmp(&numero[i].prezzo, &numero[j].nomeProdotto) > 0)     //*** changed arguments to pointers
			{
                temp=numero[i];     //***  numero is a struct, temp is an int
                numero[i]=numero[j];
                numero[j]=temp;     //***  ditto
            }
        }
    }
}

int main()
{
		
const int dim = 10;
struct scaffale numero[dim];
char s;

	do
   {
    s= menu();
    switch  (s)
      {case 'a':
            load(numero,dim);       //*** Removed []
				cout << "Scaffale caricato.";
            system("pause");
            break;
       case 'b':
 
            cout << "Ordina Scaffali";
			sort(numero, dim);      //*** Removed []
            system("pause");
            break;
       case 'x': break;
      }
   }while (s!='x');
return 0;
}


The only remaining errors are lines 60-62 where you're trying to copy your struct to an int and back.
Damn, I totally get what I was doing wrong.
Thank you a lot! I'll make a sort that actually works, I hope.

Thanks again for the help!
Topic archived. No new replies allowed.