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:

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

sorry i am unable to undestand, plz help

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.

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.

commented: Salem agrees +2
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

Read the rules of the forum Rati.

fgets is making an integer without a cast
this is compilation error using fgets

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.

i tried it but i am getting some segmentation fault again, the problem is not solved using fgets

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);
}

but for int type (like index in my code ) i will have to use scanf, fgets is only for char type

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

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

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

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

here is the code

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

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?

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

also the program is showing no compilation error, onlyruntime segmentation error is there...
i am working in linux

> scanf("%s", &retry);
First buffer overflow - retry is a single char, you can't put a string into a single char.
Second problem, calling a function recursively just to implement a while loop is poor form.
Third, you're mixing scanf() with fgets(), which will surely lead to trouble at some point.

Separate the function into two steps
- readInput
- TokeniseInput.

Both will be a lot simpler than what you have at the moment.

So i will have to declare my variables global..

it didn't work . i think the segmentation error is because of some logical error not because of scanf(), gets() problem

it didn't work . i think the segmentation error is because of some logical error not because of scanf(), gets() problem

Thats why we insist to post the code or if you cant, then U need to reconstruct the problem in smaler code. The post I posted don't give seg error, if U don't belive me compile and run the code which I posted.

//# include<stdio.h>
void function();
main()
{
function();
}
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,":");
   }

i tried your code . yes it is not giving any segmentation error but it is not performing desired operation
first of all it is giving a warning:
assignment makes pointer from integer without a cast
for statements 23 and 42
then it is not going inside the loop for asking the user if he wants to enter again the string

i am working on linux , i hope this is not creating any problems

i will be posting a reconstructed program of mine for your convinience

// # include<stdio.h>
void function();
main()
{
function();
}
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,":");
   }
     }

i tried your code but it is not giving desired result, it is not asking the user if he wants to reenter
also there are warnings for statements 23 and 42
assignment makes pointer from integer without a cast

i am working on linux , i hope that is not causing any problem

i am working on reconstruction of the code for your convinience

//# include<stdio.h>
# include <string.h>
# include<stdlib.h>
void function(char *);
int j;
struct test
{
char member1[20];
char member2[20];
};
struct test *tr;
int main()
{
char str[100];
printf("enier the string");
scanf("%s",&str);
tr=(struct test *)malloc( sizeof (struct test)*100);
function(str);
free(tr);
return 0;
}
void function(char * str)
{
long int i,index,k;
char str1[100],*ch,*pch;
j=0;
ch=strtok(str,":");
if (ch==NULL)exit(0);
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);}
}

this is my code
it gives desired output when string entered is nghg;jhjkh:kjhkh;mhjkh
but gives segmentatiopn error for hjghjgjg
it should exit from the program for such a string

The seg fault is becouse if you dont have ';' then pch is NULL. So seg fault will happen only if you dont have ';' in the input string. You need to change the logic

#include<stdio.h>
#include <string.h>
#include<stdlib.h>
void function(char *);
int j;
struct test
{
   char member1[20];
   char member2[20];
};
struct test *tr;
int main()
{
   char str[100], *ptr;
   printf("enter the string: ");
   /* scanf("%s",&str); */
   fgets(str, sizeof(str), stdin);
   if((ptr = strchr(str, '\n')) != NULL)
      *ptr = '\0';
   tr=(struct test *)malloc( sizeof (struct test)*100);
   function(str);
   free(tr);
   return 0;
}
void function(char * str)
{
   long int i,index,k;
   char str1[100],*ch = NULL,*pch = NULL;
   j=0;
   tr->member1[0] = '\0';
   tr->member2[0] = '\0';
   
   ch=strtok(str, ":");
   printf("*ch = %c\n", *ch); /* debug print */
   if (ch==NULL)
      exit(0);
   while(ch!=NULL)
   {
      pch=strstr(ch,";");
      if (pch == NULL)
      {
         ch=strtok(NULL,":");
         continue;
      }
      printf("*pch = %c\n", *pch); /* debug print */
      i=pch-ch;
      printf("i = %ld\n", i);
      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);
   }
}

Run this

Right now i am testing for ch, and before pch tests for ; ch is null
so the program should end there itself when ch is null, why would it continue till pch??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.