Thread: scanf() doesnt work

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    115

    scanf() doesnt work

    Hello

    I am trying to give some input with scanf() but it doesnt work.
    Does anyone has an idea wy? My terminal doesnt ask me to give a char.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct album{
    	char band[20];
    	char CDtitel[50];
    	int year;
    	char songs[3][30];
    	struct album *next;
    };
    
    typedef struct album *palbum;
    
    void voegAlfToe(struct album album1, palbum *list);
    
    
    
    int main (void){
    
    	int i;
    	char inputTest;
    	char isTrue = 1, anotherAlbum;
    
    	palbum list;
    	struct album album1;
    	printf("Press a to add a album\n");
    
    	while (isTrue){
    			scanf("%c", &inputTest);
    
    		switch (inputTest){
    				case 'a' :
    
    					printf("Band\n");
    					scanf("%s", album1.band);
    					printf("CDtitel\n");
    					scanf("%s", album1.CDtitel);
    					printf("year\n");
    					scanf("%i", &album1.year);
    					printf("songs\n");
    					for (i = 0; i < 3; i++)
    						scanf("%s", album1.songs[i]);
    
    				/*	voegAlfToe(album1, &list);*/
    					isTrue = 0;
    					break;
    
    		}
    	/*	if (inputTest == 'a'){
    			printf("Do you want to add another album: press a else press any key\n");
    			scanf("%c", &anotherAlbum);
    			printf("dsfd\n");
    			if (anotherAlbum != 'a')
    					isTrue = 0;*/
    		
    	}
    scanf("%c", &inputTest);
    	
    
    	return 0;
    
    }
    Last edited by thescratchy; 02-17-2010 at 12:28 PM.

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    I think that when you are trying to access members of a struct the way that you are you have to use -> instead of &...I'm not sure though, but give that a try and see if it works.

    EDIT: Wait, I lied.

    You have to make a pointer to the struct(i.e. ptr = &struct) and then refer to elements in it like this ptr->x...it's in the lesson on here actually;

    Another thing that may be causing the error is the fact that when you ask the user for a character, they punch in a character and then hit enter. Enter is considered a character by the computer, so it stays in the buffer...so the next time you use a scanf for a char, that enter gets tossed in as the value. I would either use fflush(stdin) or put \n in your scanf, like so

    Code:
    scanf("\n%c",album1.band);
    \n clears the keyboard buffer, as does fflush(stdin), but you'd have to write that every time before a scanf, which is tedious...so in this case I'd say use \n to clear the buffer.
    Last edited by mkylman; 02-17-2010 at 12:36 PM.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What input are you giving it? My magic eight-ball says you probably have a newline character being processed due to your use of %c in the scanf call.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    im sorry if i didnt make it clear.

    everything works, I have no errors when compiling.
    I can give input to all the scanf's in the loop. But the program just doesnt ask me to give an input after the loop (the line in bold).

    In the loop I give as input: test enter.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    I just updated my post, reread it lol...just put a \n in the scanf or fflush(stdin) before it(i think lol)

    The reason it works for the scanf in the loops is because %s and %i are handled differently...they terminate at \n or reject it if it's not in the data type
    Last edited by mkylman; 02-17-2010 at 12:40 PM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by mkylman View Post
    just put a \n in the scanf or fflush(stdin) before it
    No, don't use fflush(stdin)!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    HMMMM....

    i have tried to type \n in the scanf. If i do this sometimes the terminal asks me for a char and sometimes it doesnt.

    And i think i dont have to use the ->. Correct me if i am wrong.
    Here is the entire code so you can see better.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct album{
    	char band[20];
    	char CDtitel[50];
    	int year;
    	char songs[3][30];
    	struct album *next;
    };
    
    typedef struct album *palbum;
    
    void voegAlfToe(struct album album1, palbum *list);
    void genIndex(int index[26], palbum *list);
    
    
    
    int main (void){
    
    	int i;
    	char inputTest;
    	char isTrue = 1, anotherAlbum;
    
    	palbum list;
    	struct album album1;
    	printf("Press a to add a album\n");
    
    	while (isTrue){
    			scanf("%c", &inputTest);
    
    		switch (inputTest){
    				case 'a' :
    
    					printf("Band\n");
    					scanf("%s", album1.band);
    					printf("CDtitel\n");
    					scanf("%s", album1.CDtitel);
    					printf("year\n");
    					scanf("%i", &album1.year);
    					printf("songs\n");
    					for (i = 0; i < 3; i++)
    						scanf("%s", album1.songs[i]);
    
    				/*	voegAlfToe(album1, &list);*/
    					isTrue = 0;
    					break;
    
    		}
    	/*	if (inputTest == 'a'){
    			printf("Do you want to add another album: press a else press any key\n");
    			scanf("%c", &anotherAlbum);
    			printf("dsfd\n");
    			if (anotherAlbum != 'a')
    					isTrue = 0;*/
    		
    	}
    scanf("\n%c", &inputTest);
    	
    
    	return 0;
    
    }
    
    
    void voegAlfToe(struct album album1, palbum *list){
    
    	int i;
    
    	palbum q;
    	palbum listCopy;
    
    	q = NULL;
    	listCopy = *list;
    
    	while (listCopy != NULL && (strcmp(album1.band, listCopy -> band) < 0))
    		listCopy = listCopy->next;
    
    	if (listCopy == NULL){
    		*list = (palbum) malloc (sizeof (struct album));
    		listCopy = *list;
    		strcpy(album1.band, listCopy->band);
    		strcpy(album1.CDtitel, listCopy->CDtitel);
    		listCopy->year = album1.year;
    		for (i = 0; i < 3; i++)
    				strcpy(album1.songs[i], listCopy->songs[i]);
    		listCopy->next = NULL;
    	}	
    
    
    }

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    [/code]
    Quote Originally Posted by thescratchy View Post

    when i change this
    scanf("\n%c", &inputtest);

    with

    while(scanf("%c") != '\n');

    it works but i dont know how to fix it



    [/code]

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Yeah, I didn't know what I was talking about when I was blithing about ->.

    what about this line: scanf("%c", &anotherAlbum); ?

    I'm confused as to the point of this line anyway: scanf("\n%c", &inputTest);
    It doesn't seem to have any revelance to the program at all, since it happens outside the loop, why is it there at all?

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    doesnt work

    Because it is easier to test. I get the same error in the part :

    Code:
    		if (inputTest == 'a'){
    			printf("Do you want to add another album: press a else press any key\n");
    			scanf("%c", &anotherAlbum);
    			printf("dsfd\n");
    			if (anotherAlbum != 'a')
    					isTrue = 0;
    		
    	}

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Try putting a space in front of the %c, for example:
    Code:
    scanf(" %c", &anotherAlbum);
    
    ...
    
    scanf(" %c", &inputTest);
    This instructs scanf to ignore leading whitespace.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    115
    doesnt work

    Dont know if this changes anything but i am using a mac

  13. #13
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    would the enter key be considered whitespace?

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by mkylman View Post
    would the enter key be considered whitespace?
    Yes.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    I don't think being on a mac would have an effect on something like this, since you're dealing with C functions here and not operating system dependent things(like system("CLS"); or whatever).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about scanf and spaces or whitespace
    By jensbodal in forum C Programming
    Replies: 4
    Last Post: 11-26-2009, 04:12 PM
  2. scanf return values
    By pirog in forum C Programming
    Replies: 15
    Last Post: 09-13-2009, 03:58 AM
  3. Can not get this program to work?
    By BLG in forum C Programming
    Replies: 9
    Last Post: 09-09-2009, 09:28 AM
  4. If you are employed as a programmer, please look
    By Flood Fighter in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-28-2004, 02:35 AM
  5. scanf - strtok and space as delimiter?
    By Bill Cosby in forum C Programming
    Replies: 6
    Last Post: 09-20-2004, 06:45 PM