Thread: Problem in reading Character

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    Problem in reading Character

    Hi All,

    I am facing a strange problem in reading a character from stdin. I have wrote the following code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void add( int, int);
    void sub( int, int);
    void mul( int, int);
    void swap( int, int);
    void reverse( int, int);
    int rev(int);
    
    int main()
    {
        int num1,num2,ch;
        char c='y' ;
         
         while( c=='y' || c=='Y' )
        {
         
        printf("\n Enter The Two Numbers");
        scanf("%d %d",&num1,&num2);
             printf("\n  Select the operation to perform from the given Menu " );
             printf("\n \t 1. Addition "); 
         	 printf("\n \t 2. Subtraction ");
         	 printf("\n \t 3. Multiplication");
             printf("\n \t 4. Swap the values ");
             printf("\n \t 5. Print both the numbers in reverse order");
             printf("\n \t 6. Exit");
             printf("\n \t Enter Your Choice ");
             scanf("%d",&ch);
             switch(ch)
             {
    
                 case 1:
                 add(num1,num2);
                 break;  
    
                 case 2:
                 sub(num1,num2);
                 break;               
    
                 case 3:
                 mul(num1,num2);
                 break;
    
                 case 4:
                 swap(num1,num2);
                 break; 
    
                 case 5:
                 reverse(num1,num2);
                 break; 
                
                 case 6:
                 exit(0);
                 break; 
            
            }
    
             printf(" Do You Want to continue Y/N "); //Segmentation Fault after this point
             fflush(stdin); 
      
             c=getchar();
      
             scanf("%c",&c); 
         
        }
       
    }
    I am getting the segmentation fault after printing the Line " Do You Want to continue Y/N" ..

    I can't understand why it is giving the same.......

    Can Anybody help me in this..........

    Thanks
    Bargi

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Bargi View Post

    I am getting the segmentation fault after printing the Line " Do You Want to continue Y/N" ..
    The compiler is allowed to do anything it wants with "fflush(stdin)", including trigger a segfault. The question must then be asked: what do you want this "fflush(stdin)" to do, and then we can look at how to accomplish that.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > printf(" Do You Want to continue Y/N "); //Segmentation Fault after this point
    > fflush(stdin);
    fflush isn't defined for input streams. It might work, but it's not guaranteed. You could flush the input stream like this:
    Code:
             while (getchar() != '\n');
             printf(" Do You Want to continue Y/N ");
             c = getchar();

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Thanks for reply.......

    Actually I have used fflush(stdin) to flush out everything in input stream ...since some time we face problem when we try to read string after pressing "enter " and it read "Enter" as string (MAY BE I AM WRONG PLEASE CORRECT ME). That's why I have used fflush(stdin).......

    Thanks
    Bargi

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Bargi View Post
    Thanks for reply.......

    Actually I have used fflush(stdin) to flush out everything in input stream ...since some time we face problem when we try to read string after pressing "enter " and it read "Enter" as string (MAY BE I AM WRONG PLEASE CORRECT ME). That's why I have used fflush(stdin).......

    Thanks
    Bargi
    scanf will read enter (or \n, etc.) when reading a character, not a string. (IOW: if you're willing to deal with the overhead, read in a string using %NUMBERGOESHEREs.) The fact that you want to use fflush(stdin) does not change the fact that fflush(stdin) does not, in fact, actually exist.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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

Similar Threads

  1. Problem with reading in file...
    By Bizmark in forum C Programming
    Replies: 3
    Last Post: 03-27-2008, 01:11 PM
  2. Problem reading file
    By coder_009 in forum C Programming
    Replies: 10
    Last Post: 01-15-2008, 01:22 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Character problem!!
    By cBegginer in forum C Programming
    Replies: 3
    Last Post: 09-02-2005, 11:51 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM