Thread: Could someone explain why this happens

  1. #1
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134

    Question Could someone explain why this happens

    Hi everyone

    I was fiddling with some code ln and came across a dilemma ,here is the code

    Code:
    # include <stdio.h>
    # include <conio.c>
    
    /********************************** FUNCTION PROTOTYPES ********************/
    
    int test(void);
    
    /********************************** BEGIN MAIN  ****************************/
    
    int main(int argv, char *argc[])
    {
     int number;
     printf("Enter a number:");
     scanf("%d",&number);
     switch (number){
                     case (1):
                             test();
                     break;
                     }
    
         system("PAUSE");
          return 0;
    }
    
    /********************************** FUNCTION ******************************/
    
    int test(void)
    {
    int f;
    char name_test[20];
    
    clrscr();
    printf("Program is here");
    //scanf("%d",&f);
    gets(name_test);
     printf(name_test);
     return 0;
     }
    ok so whats the problem, well if i run this program as is the gets function is never called or (skipped) so i never get to input some text.
    I found that if i put a scanf function just before the gets function the program halts and allows me to enter input into the char array.
    I found tht it was the switch command that was creating all this hassle and i was just wondering why?

    Thanx for your help.

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Don't mix scanf and gets, the newline that scanf tends to leave in the input stream immediately satisfies gets and you never get a chance to enter more data. You shouldn't be using gets anyway, there's no way to make it safe. Try this instead:
    Code:
    /* Read the data then ignore a single character, presumably the newline */
    scanf("%d%*c",&number);
    then
    Code:
    /* Use fgets to get the name */
    fgets(name_test, sizeof (name_test), stdin);
    p.s. What the alphabet would look like without q and r.

  3. #3
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134
    Hi

    ok i just tried to replace gets with fgets and when i run the program with the scanf function remed out it still skips the fgets function and doesnt allow me to enter the input from stdin.

    Thanx

    Code:
    # include <stdio.h>
    # include <conio.c>
    
    /********************************** FUNCTION PROTOTYPES ********************/
    
    int test(void);
    
    /********************************** BEGIN MAIN  ****************************/
    
    int main(int argv, char *argc[])
    {
     int number;
     printf("Enter a number:");
     scanf("%d",&number);
     switch (number){
                     case (1):
                             test();
                     break;
                     }
    
         system("PAUSE");
          return 0;
    }
    
    /********************************** FUNCTION ******************************/
    
    int test(void)
    {
    int f;
    char name_test[20];
    
    clrscr();
    printf("Program is here");
    //scanf("%d",&f);    /*i dont want to use this to pause prog flow
    fgets(name_test,20,stdin);                   /*MODIFIED LINE*/
     printf(name_test);
     return 0;
     }
    And To All Those Opposed, WELL !!!
    >Deleted< " Looks like a serial no."

  4. #4
    Patent Pending GSLR's Avatar
    Join Date
    Sep 2001
    Posts
    134
    Thanx Salem

    much appreciated.
    And To All Those Opposed, WELL !!!
    >Deleted< " Looks like a serial no."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you explain these bitwise operations?
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2008, 01:19 PM
  2. Please explain?
    By neo_phyte in forum C Programming
    Replies: 3
    Last Post: 08-25-2006, 05:23 AM
  3. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 12:36 PM
  4. Replies: 4
    Last Post: 11-19-2002, 09:18 PM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM