954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
 
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:


You need to flush the input buffer read this and this

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

sorry i am unable to undestand, plz help

rati
Junior Poster in Training
78 posts since Aug 2006
Reputation Points: 45
Solved Threads: 1
 
sorry i am unable to undestand, plz help


Dont use scanf and gets for string input. Instead use fgets which is decribed in the second this of my previous post. You need to flush the input buffer (how to do that read the first this of my previous post). You need to flush the input buffer becouse if you not flush it you it will be unable to input the string second time. Please post your code.

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 
sorry i am unable to undestand, plz help


Run this code

#include <stdio.h>
#include <string.h>
int main()
{
   int num, ch;
   char str[10];
   char * ptr = NULL;
   printf("Input some int: ");
   scanf("%d", &num);
//   while ((ch = getchar()) != '\n' && ch != EOF); /* flushing the input buffer */ 
   printf("Input some string: ");
   fgets(str, sizeof(str), stdin);
   if((ptr = strchr(str, '\n')) != NULL)
      *ptr = '\0'; 
   printf("num = %d string ='%s'\n", num, str);
   return 0;
}

and now uncomment the red line and run again.

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 
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
 

Read the rules of the forum Rati.

Grunt
Junior Poster
152 posts since Jul 2006
Reputation Points: 197
Solved Threads: 12
 

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
 
fgets is making an integer without a cast this is compilation error using fgets


Instead of scanf("%s", &str); which is by the way wrong (don't need the &) use

fgets(str, sizeof(str), stdin);
   if((ptr = strchr(str, '\n')) != NULL)
      *ptr = '\0';


. Similar instead of gets use fgets. When you make this correction post the new code and we will give you further instructions.

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

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
 
i tried it but i am getting some segmentation fault again, the problem is not solved using fgets


Never mind the seg fault, just post the code with fgets (no scanf and gets). Then we will tell you what's the problem (fgets should work). Consider something like this

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

void function();
int main()
{
   function();
   return 0;
}

void function()
{
   int index = 0;
   char str1[100],str[100], strNum[20];
   char * ptr = NULL;
   
   printf("enter the string: ");
   /* scanf("%s", &str); */
   /* while ((m= getchar())!= '\n' && m != EOF); */
   fgets(str, sizeof(str), stdin);
   if((ptr = strchr(str, '\n')) != NULL)
      *ptr = '\0'; 
   
   printf("enter the string to be compared: ");
   /* gets(str1); */
   fgets(str1, sizeof(str1), stdin);
   if((ptr = strchr(str1, '\n')) != NULL)
      *ptr = '\0'; 
   printf("enter index: ");
   /* scanf("%d",&index); */
   if (fgets(strNum, sizeof(strNum), stdin))
   {
      if (sscanf(strNum, "%d", &index) != 1)
      {
         printf("ERROR inputing number!\n");
      }
   }
   printf("str = '%s' str1 = '%s' index = '%d'\n", str, str1, index);
}
andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

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
 
but for int type (like index in my code ) i will have to use scanf, fgets is only for char type


Ok than use scanf but then you will need to flush the input buffer

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

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
 
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


Post the code

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

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
 

here is the code

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
 
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


It means that ch is NULL. Your code have lot of compile errors. j not defined and tr structure not defined.

void function()
{
   int i,index,k,m;
   char str1[100],retry,str[100],*ptr,*ctr;
   int 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';
   /* printf("enter index"); */
   /* canf("%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;
   }
   else
      printf("\nch is NULL\n");
   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 ;*/
}



This code doesn't give me seg fault. Before you post something compile the code.
What do you input for two strings?

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

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
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You