Function returning struct

Write your question here.
Hi, there. I've just stumbled upon a problem about functions returning multiple values and I found out that you can use struct. So basically I redid my whole program but I was faced with this problem.

It's treating the function as void when I type the function as this
 
struct scrabble loaddata(games players[4], scrabble variable)

But then it's giving me a parse error if I type it like this
 
struct loaddata(games players[4], scrabble variable)

Here's the whole function
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
  struct scrabble loaddata(games players[4], scrabble variable){
	int k, j, l;
	FILE *load;
	if ((load=fopen("scrabble.txt", "r"))==NULL){
		printf("No existing save game. Loading new game\n");
		variable.n=100;
		variable.p = 1;
		variable.no = 0;
		strcpy(variable.letter, "{'A','A','A','A','A','A','A','A','A','B','B','C','C','D','D','D','D','E','E','E','E','E','E','E','E','E','E','E','E','F','F','G','G','G','H','H','I','I','I','I','I','I','I','I','I','J','K','L','L','L','L','M','M','N','N','N','N','N','N','O','O','O','O','O','O','O','O','P','P','Q','R','R','R','R','R','R','S','S','S','S','T','T','T','T','T','T','U','U','U','U','V','V','W','W','X','Y','Y','Z',' ',' '}"); 
	}else{		
		fscanf(load, "%d\n", &variable.n);
		fscanf(load, "%d\n", &variable.p);
		fscanf(load, "%d\n", &variable.no);
		for(k=1;k<=variable.no;k++){
			fscanf(load,"%c\n",players[k].tiles);
			fscanf(load,"%d\n",&players[k].score);
		}
		for(k=1;k<=100;k++){
			fscanf(load, "%d\n", variable.letter[k]);
		}
		for(k=0; k<24; k++){
			for(j=0; j<=12; j++){
				if (k==0 && j!=12){				
					fscanf(load, "____");				
				}
				if (j==0 && k!=0){						
					fscanf(load, "|");
				}
				if (k==1 && j!=0 && j!=1){						
					fscanf(load, "   %d", j-2);
				}
				if (j==1 && k%2==0 && k!=0){						
					fscanf(load, "%d", k/2 - 1);
				}
				if (k>1 && k%2==0 && j>1){						
					if(j==2){
						if(k==22){ fscanf(load, " %c", variable.board[k][j]);
						}else fscanf(load, "  %c", variable.board[k][j]);
					}else fscanf(load, "   %c", variable.board[k][j]);
				}
				
			}	
			fprintf(load, "\n");			
		}
		fclose(load);
		
	}
	return scrabble variable;
}


Oh, and I'm also not sure how to return the value.
Last edited on
I think the issue you are having here is that the only thing inside of the structure is the function loaddata. Everything else is inside of that function. Of course, that isn't what you want, so I recommend you look at the documentation for structures and classes. They should help you in this regard.
I don't get what you're saying. Here's the stuff inside the structure

1
2
3
4
5
6
7
typedef struct{
	int n;
	int p; 
	int no; 
	char letter[101];
	char board[24][12];
}scrabble;


That struct is the return type of the function loaddata
Last edited on
struct scrabble {};
and
1
2
3
4
5
6
scrabble loaddata(...)
{
    scrabble myscrabble;
    // operations on myscrabble
    return myscrabble;
}


Also the huge strcpy line is wrong. I'll tell more once I'll be on PC.
Last edited on
I've got this one now, the strcpy line is the one bugging me now, you're right.
I can't declare it on the struct so I used that but It's not working properly.
strcpy(variable.letter, "{'A','A','A','A','A','A','A','A','A','B','B','C','C','D','D','D','D','E','E','E','E','E','E','E','E','E','E','E','E','F','F','G','G','G','H','H','I','I','I','I','I','I','I','I','I','J','K','L','L','L','L','M','M','N','N','N','N','N','N','O','O','O','O','O','O','O','O','P','P','Q','R','R','R','R','R','R','S','S','S','S','T','T','T','T','T','T','U','U','U','U','V','V','W','W','X','Y','Y','Z',' ',' '}");
Should just be
strcpy(variable.letter, "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ");

Also, store them as characters in the file:
1
2
3
4
5
6
// begin with k=0. Arrays begin with the 0 index.
// Also the same for the other for loops you have!! Begin with k=0!
for(k=0;k<=100;k++){
	// %c == char
	fscanf(load, "%c\n", variable.letter[k]);
}


I'm certain you'll have many more troubles on your way (Especially once players will begin to actually use the letters).
Topic archived. No new replies allowed.