2 Replies - 214 Views - Last Post: 25 October 2013 - 06:10 PM Rate Topic: -----

#1 whitaker144  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 20-September 13

Simple menu in C not working using fscanf

Posted 25 October 2013 - 03:44 PM

Hi I have a simple menu in my main method which is this:

int main( void ) {
	char option = 'z';
	while(option != 'e') {
		printf("a(dd) {x } = add a new node with value x to the list, at the front of the list\n");
		printf("d(el)      = delete the fist node of list\n");
		printf("l(ength )  = print the number of nodes in the list\n");
		printf("p(rint)    = print  the complete list\n");
		printf("z(ero)     = delete  the entire list\n");
		printf("e(xit)     = quit the program\n");
		printf("Enter option here: ");
		fscanf(stdin, "%c",option);
	}
	return 0;
}


When I compile using Wall ansi and pedantic I get a warning telling me: "warning: format ‘%c’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]"

I looked it over and can't really see anything wrong, even though I've mostlikely mis-typed or mis-written something, I was wondering what this warning means and why it's happening in here.

Also I forgot to put for clarification: I am required to use fscanf rather than scanf for this.

Is This A Good Question/Topic? 0
  • +

Replies To: Simple menu in C not working using fscanf

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Simple menu in C not working using fscanf

Posted 25 October 2013 - 06:08 PM

You forgot to add in the & before the name of the variable in fscanf().
Was This Post Helpful? 0
  • +
  • -

#3 michael072  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 97
  • Joined: 08-February 11

Re: Simple menu in C not working using fscanf

Posted 25 October 2013 - 06:10 PM

It's because fscanf needs a pointer to the options variable so it can modify it:

fscanf(stdin, "%c", &option);


The & gives the memory address of the variable. In c if a function is going to be modifying a variable you need to give it the address of that variable whether it's by using & or by declaring the option as a char *.

This post has been edited by michael072: 25 October 2013 - 06:10 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1