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

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
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:
#include <stdio.h>
#include <string.h>

main()
{
  FILE *fp;
  int i;
  char name[20];
  float salary;
   
  fp=fopen("Detail.txt","w");
   
  if(fp==NULL)
  {
  printf("\nThe file cannot be opened");
  close(1);
  }
   
  for(i=0; i<2; i++)
  {
  puts("\nEnter your name : ");
  gets(name);
  puts("\nEnter your salary : ");
  scanf("%f", &salary);
  fprintf(fp,"(%d) NAME : %s \nSALARY : %f \n ", i, name, salary);
  }
   
  fclose(fp);
}
[\code]
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
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
but i declared it with [100] already. and then why when i use gets(), it will show a warning. i use programming n C
 

WBahn

Joined Mar 31, 2012
29,979
First, let's format your code a bit better.

Code:
#include <stdio.h>
#include <string.h>

main()
{
  FILE *fp;
  int i;
  char name[20];
  float salary;
 
  fp=fopen("Detail.txt","w");
 
  if(fp==NULL)
  {
    printf("\nThe file cannot be opened");
    close(1);
  }
 
  for(i=0; i<2; i++)
  {
    puts("\nEnter your name : ");
    gets(name);
    puts("\nEnter your salary : ");
    scanf("%f", &salary);
    fprintf(fp,"(%d) NAME : %s \nSALARY : %f \n ", i, name, salary);
  }
 
  fclose(fp);
}
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.
 

WBahn

Joined Mar 31, 2012
29,979
but i declared it with [100] already.
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 then why when i use gets(), it will show a warning. i use programming n C
And how are we supposed to tell you why it showed a warning when you don't tell us what warning it showed?
 

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
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?
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.
 

WBahn

Joined Mar 31, 2012
29,979
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:

Thread Starter

kamarul amin

Joined Dec 2, 2014
62
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.
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.
 

WBahn

Joined Mar 31, 2012
29,979
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.
 
Top