Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

fscanf user input compiler errors

user input compiler error scanf

  • Please log in to reply
2 replies to this topic

#1 chewdonkey

chewdonkey

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 13 April 2011 - 04:23 PM

Hi, this is my first C project as well as my first forum post! I am having trouble getting any kind of user input into a char array variable inside of a structure.

The project is a simple text game. I am using a structure to define the rooms by the rooms north, east, south, and west of it, as well as a description. An enumerator gives each room a number. I have also defined a structure for the save file. I think I have a good understanding of the basic structure I need, though Im sure there are many problems yet to come. The full code I have done so far is at the bottom.

My problem is that I cant get any user input into the char string inside the structure. Iv done simple experiments and can use scanf(), gets(), fscanf(), and fgets() properly when directing to a simple variable. However when trying to direct the input to the structure member my compiler gives the error "expected expression before usrsave". Could someone tell me what im doing wrong? Thanks.


//standard libary files
#include <stdio.h>
#include <stdlib.h>

//global external definitions
#define north =0
#define east =1
#define south =2
#define west =3

//function prototypes


//global external structure definitions
typedef struct room_detail {
	char description[201];
	int room_n;
	int room_e;
	int room_s;
	int room_w;
} no_room,roomone,roomtwo,roomthree,roomfour,roomfive;

typedef struct save_detail {
	char usrname[20]; /*this is the line i am trying to get my user input into */
	int room;
} usrsave;

//global external enumerator definitions
enum room_numbers {
	room0,room1,room2,room3,room4,room5
};

//main function
void main () {
    printf("\nPrint Credits\n");
	printf("Input File Name to Load:\nInput New Name to Start New:\n");
	fscanf(stdin, "%c", &usrsave.usrname[]); /*this is the offending line i get errors on*/
}

  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 13 April 2011 - 05:50 PM

You've "typedefed" the save_detail structure to usrsave, this is so you do not have to type "struct save_detail" every time you wish to reference to this structure. It does not actually instantiate it.

We can use two options to instantiate and otherwise use our struct:

The standard method:
usrsave myUsrSave;
With a pointer:
usrsave* myUsrSave;
I have preferred the pointer to a structure myself, as we can easily pass this structure through functions if need be.

If you place the above code in main, we can now access its members with either of the two methods:

Standard:
fscanf(stdin, "%s", usrsave.usrname);
With pointers:
fscanf(stdin, "%s", usrsave->usrname);
Now with this code, I have done the following:

  • As we appear to be accepting a file name (of twenty maximum), I have changed the type of argument to %s (string) instead of %c (char)
  • In the second example, we have assigned a pointer to a struct rather than a struct itself, we use the -> operator to access its member.
Other than the aforementioned set of errors:

  • Your main function should return an integer ( int main() ), such as zero if success in the end ( return 0; ). Failing to do this will not produce errors, and is only needed if you want your program to be more standards conforming to whichever standard that may be.
  • You should be aware of how strings work in C, and how your fscanf may not be able to stop the user from inputting more than twenty characters in to your structure. Consider using fgets instead.

  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 chewdonkey

chewdonkey

    CC Lurker

  • New Member
  • Pip
  • 6 posts

Posted 14 April 2011 - 05:29 AM

Thanks Alexander!

That worked perfectly. It seems my confusion/misunderstanding was arising from the fact that the structures function/purpose and the way you define it does change when you are using "typedef".

I think I am right in saying that in my above code I do not actually need to "typedef" my "save_detail" structure because I will only ever be using one of these structures. However for my "room_detail" structure I will need this structure for 6, and possibly later more, rooms and so this function is needed.

Thanks for the help. This is my final working code that inputs into "typedefed" structures properly, incase it helps anyone else:

/
/standard libary files
#include <stdio.h>
#include <stdlib.h>

//global external definitions
#define north =0
#define east =1
#define south =2
#define west =3

//function prototypes


//global external structure definitions
typedef struct {
	char description[201];
	int room_n;
	int room_e;
	int room_s;
	int room_w;
} room_detail;

room_detail no_room, roomone, roomtwo, roomthree, roomfour, roomfive;

typedef struct {
	char usrname[20];
	int room;
} save_detail;

save_detail usrsave;

//global external enumerator definitions
enum room_numbers {
	room0,room1,room2,room3,room4,room5
};

//main function
int main () {
    printf("\nPrint Credits\n");
	printf("Input File Name to Load:\nInput New Name to Start New:\n");
	fscanf(stdin, "%s", usrsave.usrname);
	return 0;
}

  • 0





Also tagged with one or more of these keywords: user input, compiler error, scanf

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download