How can i handle string in loop??? :(

Discussion in 'Programmer's Corner' started by kamarul amin, Apr 6, 2015.

  1. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    can i know is there any restriction while using string in loop?? as far as i know, my reference book don't say anything about the behaviour of string when using it within the loop. but when i code it, when to put the input, i mean the string, it don't behave like the integer. let say a program ask for the name and a salary. for i=0, we enter the first name and then the salary. then increment i++, but when i=1, the program straightly jump to the salary and not asking about the name. why is it happened?? any restriction? i did us fgets, gets, scanf but nothing make difference and show the progress that i wanted for.
    Code (Text):
    1.  
    2. #include <stdio.h>
    3. #include <string.h>
    4.  
    5. main()
    6. {
    7.   FILE *fp;
    8.   int i;
    9.   char name[20];
    10.   float salary;
    11.    
    12.   fp=fopen("Detail.txt","w");
    13.    
    14.   if(fp==NULL)
    15.   {
    16.   printf("\nThe file cannot be opened");
    17.   close(1);
    18.   }
    19.    
    20.   for(i=0; i<2; i++)
    21.   {
    22.   puts("\nEnter your name : ");
    23.   gets(name);
    24.   puts("\nEnter your salary : ");
    25.   scanf("%f", &salary);
    26.   fprintf(fp,"(%d) NAME : %s \nSALARY : %f \n ", i, name, salary);
    27.   }
    28.    
    29.   fclose(fp);
    30. }
    31. [\code]
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    The biggest problem occurs when there are more characters in the input than there are in the array of char that holds the string. There are many webpages that deal with function prototypes and return values. You should get in the habit of using them.
    http://beej.us/guide/bgc/output/html/multipage/gets.html
     
  3. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    but i declared it with [100] already. and then why when i use gets(), it will show a warning. i use programming n C
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    What is the compiler? What is the warning?
     
  5. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,787
    First, let's format your code a bit better.

    Code (Text):
    1.  
    2. #include <stdio.h>
    3. #include <string.h>
    4.  
    5. main()
    6. {
    7.   FILE *fp;
    8.   int i;
    9.   char name[20];
    10.   float salary;
    11.  
    12.   fp=fopen("Detail.txt","w");
    13.  
    14.   if(fp==NULL)
    15.   {
    16.     printf("\nThe file cannot be opened");
    17.     close(1);
    18.   }
    19.  
    20.   for(i=0; i<2; i++)
    21.   {
    22.     puts("\nEnter your name : ");
    23.     gets(name);
    24.     puts("\nEnter your salary : ");
    25.     scanf("%f", &salary);
    26.     fprintf(fp,"(%d) NAME : %s \nSALARY : %f \n ", i, name, salary);
    27.   }
    28.  
    29.   fclose(fp);
    30. }
    31.  
    You actually have a number of problems with your code. First, I applaud you for at least trying to check if your file successfully opened, but you are going about it all wrong.

    The function 'close()' needs the file handle returned by the function 'open()', which you didn't use. You need to use the function 'fclose()' which takes a FILE pointer as its argument. However, if the FILE pointer 'fp' is NULL, then there is not open file to close! But whether there is or not, you then proceed to try to write to that file within the for() loop. I think what you are trying to do in your if() code is simly to exit the program, which you can do with the function 'exit()'.

    Using 'scanf()' should be forbidden, AFAIAC. It is a notoriously poorly behaved function, especially for getting input from a user. It also tends to not play well with other input functions such as 'gets()'. The problems often center around whether you have newline characters that are still sitting in the input buffer.

    Using 'gets()', while not quite as problematic, is also a bad idea because it has no awareness of how large the character array its writing to is and therefore is prone to going out-of-bounds.

    The better function to use is 'fgets()' which allows you to tell it how big the array is.
     
    kamarul amin likes this.
  6. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,787
    Where did you declare it with [100]? Your code shows that name is declared to be length 20 (which means it can hold 19 characters since you have to leave room for the NUL terminator).

    And how are we supposed to tell you why it showed a warning when you don't tell us what warning it showed?
     
    kamarul amin likes this.
  7. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    sorry, i declare the size only 20. hehe sorry..
    the main problem is not the file, but it is all about the time when i want to put the name.
    for the i=0, the program run nicely, i can put the first name, first salary.
    for the i=1, the program dont run nicely like before, it straighty go ask for the salary, skip the name. why this is happen?? i just follow the book :(

    from my original code that i execute, the warning shown as like this :

    string.c: In function ‘main’:
    string.c:22:3: warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
    gets(name);
    ^
    /tmp/ccOsrXcp.o: In function `main':
    string.c:(.text+0x6b): warning: the `gets' function is dangerous and should not be used.
     
  8. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,787
    I agree with both of those warnings and they indirectly reflect the problems you are having. But, admittedly, the warnings don't tell you what the underlying problem is, but they DO tell you how to fix the problem -- don't use gets()! (though the bigger problem is the use of scanf() -- and most compilers will throw similar warnings when you use that function).

    Note what I said in my previous post: "
    Using 'scanf()' should be forbidden, AFAIAC. It is a notoriously poorly behaved function, especially for getting input from a user. It also tends to not play well with other input functions such as 'gets()'. The problems often center around whether you have newline characters that are still sitting in the input buffer."

    So you use scanf() to get a floating point value. You entered the value from the keyboard and hit Enter (or Return). The function fetched the characters that made up the floating point value from the keyboard buffer but did NOT fetch the newline character because that is NOT part of a floating point value. As a result, your buffer still has the newline character sitting in it. So what happens the next time you call gets()? It gets everything from be buffer up to and including the first newline character it sees which is, as a result of the prior scanf() call, the newline character that you entered after the floating point value.
     
    Last edited: Apr 7, 2015
    kamarul amin likes this.
  9. kamarul amin

    Thread Starter Member

    Dec 2, 2014
    62
    3
    i am so sorry because i am not really good in english :( hmm so, the main problem is all about the buffer?? i should use scanf("%s") i think. now i had to make mastermind game, a game just like guessing number game and put in it the serial board.
     
  10. WBahn

    Moderator

    Mar 31, 2012
    17,715
    4,787
    Personally, I think you should not use gets() or scanf() at all. Period. Most especially do not use scanf() for anything. Use fgets() for all user input (when restricted to the standard libraries) though there ARE newer, "safe" versions of other functions. I haven't really looked into them because when they were first available they were not ANSI-compliant. I don't know if ANSI-compliant versions are available now or not.
     
Loading...