the first time enter the "while loop" and the second no! Why?

This is a discussion on the first time enter the "while loop" and the second no! Why? within the C Programming forums, part of the General Programming Boards category; When i type first time 1 the printf appeared, but the second time i typed 1 there where no printf...can ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    the first time enter the "while loop" and the second no! Why?

    When i type first time 1 the printf appeared, but the second time i typed 1 there where no printf...can anyone help me?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct Info{
    	char name[81];
    	int age;
    	char email[81];
    	int c;
    };
    
    int main(void){
    	FILE* infile;
    	struct Info info;
    		
    		infile = fopen("my_file.txt","r+"); // pos: r, w, a, r+, w+, a+
    		if(infile ==NULL){
    			printf("File does not exist.\n");
    			exit(1);
    		}else{
    			printf("Please type 1 to see the statistics.\n");
    			printf("Please type 2 to add a new contact.\n");
    			printf("Plesase type 0 to exit.\n");
    			scanf("%d", &info.c);
    			while(1){
    
    				
    				if(info.c == 1){
    					while(fscanf(infile,"%s %d %s", info.name, &info.age, info.email) != EOF){
    						printf("\tName: %s\n", info.name);
    						printf("\tAge: %d\n", info.age);
    						printf("\tE-mail: %s\n\n", info.email);
    					}
    				}else if(info.c == 2){
    					printf("New name:\n");
    					scanf("%s", info.name);
    					printf("New age:\n");
    					scanf("%d", &info.age); 
    					printf("New e-mail:\n");
    					scanf("%s", info.email);
    					fprintf(infile,"%s %d %s", info.name, info.age, info.email);
    					
    					fclose(infile);
    					infile = fopen("my_file.txt","r+");
    				}else{
    					exit(0);
    				}
    				printf("Please type 1 to see the statistics.\n");
    				printf("Please type 2 to add a new contact.\n");
    				printf("Plesase type 0 to exit.\n");
    				scanf("%d", &info.c);
    			}
    		}
    	fclose(infile);
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    After each call to scanf(), you leave a newline char behind on the keyboard buffer. To fix your problem, remove the newline char's, by adding a getchar() immediately after each scanf().

    Your problem will disappear - gotta be THE most common question on the forum.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Woooo....

    You mean to do this after all scanf in the code?

    scanf();
    getchar();

    Why should i have both scanf and getchar? Can' t i work only with the getchar for example?

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    As stated this is a FAQ.
    Read
    Read
    Read
    Read

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Again, what's with the whole gets business!?! Why can those FAQs not avoid gets? Ridiculous.
    And for some reason, reading strings with scanf was not mentioned either: SourceForge.net: Scanf woes - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Because i am not from england...or somewhere else where i can speak read write english well...it' s somehow difficult for me to understand everything the FAQ says...this is why i write posts in this forum in my own way...i hope you understood !

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    scanf() is something you want to avoid later on, but I know it's a normal part of all kinds of programs while you are learning.

    So use scanf(), for now - but add the getchar() after each one - OK, there are SOME data types will "leap" over the left-over newline char - integers, for instance.

    But char's:
    Code:
    scanf("%c", &someChar);
    can't, because the newline char IS a char, and scanf() doesn't know it's not the char you want - so it takes it and says "I'm off with my char!".

    So *before* any scanf() for a char, you have to have a getchar() after each previous scanf()'s, to get the keyboard strream "clean" of newline char's.

    Also, it's a helpful trick to use in a loop, when you want an integer, but the user accidentally enters a letter - and now your loop goes into an endless looping tizzy, because it's input was goofed.

    Just add a getchar() after the scanf() for the number, and if the user enters a letter, it will be pulled off by the getchar() in the next loop, and the program will return to normal, waiting for input. It's a nice trick.

    Later, you'll want to basically throw scanf() away, for anything else besides formatted input (like from a file). Same with gets() - it's just too easy to bust your program with it by over-running the buffer gets feeds into.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Ah. Although mentioning this in your threads might be a good idea since you're going to be redirected to FAQs otherwise.
    Actually, it would be a good idea to read the FAQs anyway, and post questions about what you don't understand. That way you can actually consume the FAQ material and become smarter and we don't have to explain everything again.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21