why function gets doesn't work in if statement in c programming?

This is a discussion on why function gets doesn't work in if statement in c programming? within the C Programming forums, part of the General Programming Boards category; Code: int main () { char str1[100]; char str2[100]; int d=1; int ch; int choose; printf("Enter the first string : ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    9

    why function gets doesn't work in if statement in c programming?

    Code:
    int main ()
    {
    char str1[100];
    char str2[100];
    int d=1;
    int ch;
    int choose;
    printf("Enter the first string : \n");
       gets (str1);
    printf("Enter the second string : ");
       gets (str2);
    
    while(d==1){
    printf("   Enter one to remove duplicates from the first string\n"
           "   Enter two to concatenate the two strings\n"
           "   Enter three to sort strings\n"
           "   Enter four to Separate str1 into tokens\n");
           scanf("%d",&choose);
    if (choose==1) {
    remove_duplicates (str1);}
    if(choose==2){
    printf("the two string after  concatenate %s\n",strcon(str1,str2));}
    if (choose==3){
    sort_string(str1);
    sort_string(str2);
    printf("%s\n",str1);
    printf("%s\n",str2); }
    if (choose==4){
    printf("the str1 after Separate it into tokens is \n");
    sentancetoken(str1);
    }
    printf("do you want to do another operation Enter 1 for yes and 2 for no\n ");
    scanf("%d",&d);
    printf("do you want to change the strings Enter 1 for yes and 2 for no");
    scanf("%d",ch);
    if(ch==1){
    printf("Enter the New first string : \n");
       gets (str1);
    printf("Enter the New second string : ");
       gets (str2);}
    
    }
    
    getch ();
    return 0;

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    9
    Code:
    if(ch==1){
    
    printf("Enter the New first string : \n");
    
       gets (str1);
    printf("Enter the New second string : ");
       gets (str2);}
    this part

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Never use gets.

    Use fgets or scanf with max size specified in front of the s.
    Example
    Code:
    scanf("%30s",...)
    Well you have these two variables declared
    Code:
    int d=1;
    int ch;
    and then in scanf you do
    Code:
    scanf("%d",&d);
    scanf("%d",ch);
    On the one there is the needed &, but on the other it does not, despite the fact that they are both of they same type...

    Edit: You need to write about indentation of code.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    4
    Code:
    scanf("%d", &ch);
    this works for me.

    Code:
    int main(int argc, const char * argv[])
    {
        char str1[100], str2[100];
        int ch=0;
        
        scanf("%d", &ch);
        
        if(ch==1){
            
            printf("Enter the New first string : \n");
            
            gets (str1);
            printf("Enter the New second string : ");
            gets (str2);}
        
        return 0;
    }

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    turtegrip, do not be so analytic with the code(solution) that you already provided...
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    9
    but with scanf i can not write spaces
    I need to write spaces

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,196
    The rule you're breaking is "don't mix line oriented input and formatted input on the same stream". Line oriented input includes functions like gets(). Formatted input is scanf().

    The reason is that the two styles of input handle newlines (and whitespace) differently. For example, scanf("%d", &d), when given the input 2 followed by a newline, will retrieve the value 2, but leave the newline in the stream. If the next operation on that stream is gets(), it will encounter the newline, interpret it as the end of a line, and return immediately (i.e. it will read a line with length zero).


    You need to be consistent in how your code interacts with the stream. For example, use only line-oriented input, and extract integers (e.g. using sscanf() - note the two s's) from a line as needed.


    Also, don't use gets(). It is so unsafe that it has been removed from the latest version of the C standard. Look up the fgets() function instead for line-oriented input. It does some things a little differently than gets(), but it can be used safely.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My if statement doesn't seem to work.
    By KittenAqua in forum C Programming
    Replies: 3
    Last Post: 02-23-2012, 02:09 PM
  2. modf function doesn't work properly
    By paranoidgnu in forum C Programming
    Replies: 1
    Last Post: 06-29-2011, 09:59 AM
  3. Replies: 5
    Last Post: 01-18-2006, 07:59 AM
  4. itoa function : doesn't work for -ve number ?????
    By gemini_shooter in forum C Programming
    Replies: 2
    Last Post: 03-22-2005, 05:50 AM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM

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