Wednesday, 11 September 2013

Program "double reading" a string

Program "double reading" a string

I need to create a program that reads a string " A = B" and searchs B in
an array of variables. If it doesn't find B, then it asks its value and
put it in another array. Well, I don't know if the idea is clear, but here
is an example:
while(1){
printf("Get string\n");
gets(L);
if(L[0]=='\0') break;
if(L[2] == '1') {
printf("Value of 1: ");
scanf(" %lf", &m);
}
}
printf("\nbreak");
I need that this program stops when we type enter, so I used
if(L[0]=='\0') break; for it.
My problem is: Everytime I ask the value of B, my program read a "ghost
string" L, it is, it doesn't let me put the value of L and the program
stops. It is almost double reading the string, but it breaks because of
the condition L[0] != '\0'. What am I doing wrong? If we remove this
condition, then the program print 2 times "Get string", without asking me
to enter the string 2 times..
Thank you very much :)

No comments:

Post a Comment