use of scanf()
i have a problem in a program i am making. In my program i need to call a function again and again until the string entered in it is not correct. Initially i was using gets() but it wasn't accepting the string.
i cahnged it to scanf(), so it is accepting first string but not the other strings or integer i am trying to enter after it. My program was correct before i introduced this facility to make it user friendly,its now showing segmentation error:sad:
can you help me please:confused:
rati
Junior Poster in Training
78 posts since Aug 2006
Reputation Points: 45
Solved Threads: 1
sorry i am unable to undestand, plz help
rati
Junior Poster in Training
78 posts since Aug 2006
Reputation Points: 45
Solved Threads: 1
void function()
{
int i,index,k,m;
char str1[100],retry,str[100];
j=0;
printf("enter the string");
scanf("%s", &str);
while ((m= getchar())!= '\n' && m != EOF);
printf("enter the string to be compared");
gets(str1);
printf("enter index");
scanf("%d",&index);
char *ch,*pch;
ch=strtok(str,":");
printf(" ratg");
if (ch==NULL)
{printf("entered string is incorrect,reenter? y/n");
scanf("%s",&retry);
if(retry=='y')function();
else return;}
while(ch!=NULL)
{
pch=strstr(ch,";");
i=pch-ch;
strncpy(tr[j].member1,ch,i);
strcpy(tr[j].member2,pch+1);
j++;
ch=strtok(NULL,":");
};
for(k=0;k<j;k++)
{printf("\n%s",tr[k].member1);
printf("\n%s",tr[k].member2);}
i= function1(str1, index);
if(i==1)
printf("true");
else printf("false");
return ;
}
this is my code, its function is to enter a string separated by : and ;
like qwerty;asdfgh:zxcv;asd , the sequence shud be same
i want if a user by mistake enters agsddhjgdgg and skips the ; then he must be asked if he want to retry , but the problem is as stated above, help please
rati
Junior Poster in Training
78 posts since Aug 2006
Reputation Points: 45
Solved Threads: 1
fgets is making an integer without a cast
this is compilation error using fgets
rati
Junior Poster in Training
78 posts since Aug 2006
Reputation Points: 45
Solved Threads: 1
i tried it but i am getting some segmentation fault again, the problem is not solved using fgets
rati
Junior Poster in Training
78 posts since Aug 2006
Reputation Points: 45
Solved Threads: 1
but for int type (like index in my code ) i will have to use scanf, fgets is only for char type
rati
Junior Poster in Training
78 posts since Aug 2006
Reputation Points: 45
Solved Threads: 1
since index is used for the next function function1(), i placed it just before the function function1() is called, this wont require any flushing of input buffer , but run time error segmentation error is still there
rati
Junior Poster in Training
78 posts since Aug 2006
Reputation Points: 45
Solved Threads: 1
after using fgets for inputting the two strings , the code is not even printing the "the string entered is incorrect, reentery/n" it means the problem is somewhere before the printf() statement
rati
Junior Poster in Training
78 posts since Aug 2006
Reputation Points: 45
Solved Threads: 1
rati
Junior Poster in Training
78 posts since Aug 2006
Reputation Points: 45
Solved Threads: 1
void function()
{
int i, index, k, m;
char str1[100], retry, str[100], *ptr, *ctr;
j = 0;
printf("enter the string");
fgets(str, sizeof(str), stdin);
if ((ptr = strchr(str, '\n')) != NULL)
*ptr = '\0';
printf("enter the string to be compared");
fgets(str1, sizeof(str1), stdin);
if ((ctr = strchr(str1, '\n')) != NULL)
*ctr = '\0';
char *ch, *pch;
ch = strtok(str, ":");
printf(" ratg");
if (ch == NULL) {
printf("entered string is incorrect,reenter? y/n");
scanf("%s", &retry);
if (retry == 'y')
function();
else
return;
}
while (ch != NULL) {
pch = strstr(ch, ";");
i = pch - ch;
strncpy(tr[j].member1, ch, i);
strcpy(tr[j].member2, pch + 1);
j++;
ch = strtok(NULL, ":");
};
for (k = 0; k < j; k++) {
printf("\n%s", tr[k].member1);
printf("\n%s", tr[k].member2);
}
printf("enter index");
scanf("%d", &index);
i = function1(str1, index);
if (i == 1)
printf("true");
else
printf("false");
return;
}
I even indented it for you - Salem
rati
Junior Poster in Training
78 posts since Aug 2006
Reputation Points: 45
Solved Threads: 1
the code above is just a part of the program i am trying to write, its just a function, not the whole program.
asdf;ghjk:jkhdkhd;ajsahd:dkjsakd;sdkjsd for str
and
jsdfhjdf >> ansdb dasd for str1
rati
Junior Poster in Training
78 posts since Aug 2006
Reputation Points: 45
Solved Threads: 1