use of scanf()

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2006
Posts: 78
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

Re: use of scanf()

 
1
  #21
Sep 25th, 2006
also the program is showing no compilation error, onlyruntime segmentation error is there...
i am working in linux
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: use of scanf()

 
1
  #22
Sep 25th, 2006
> 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 78
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

Re: use of scanf()

 
1
  #23
Sep 26th, 2006
So i will have to declare my variables global..
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 78
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

Re: use of scanf()

 
1
  #24
Sep 26th, 2006
it didn't work . i think the segmentation error is because of some logical error not because of scanf(), gets() problem
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: use of scanf()

 
1
  #25
Sep 26th, 2006
Originally Posted by rati View Post
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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 78
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

Re: use of scanf()

 
0
  #26
Sep 27th, 2006
  1. //# include<stdio.h>
  2. void function();
  3. main()
  4. {
  5. function();
  6. }
  7. void function()
  8. {
  9. int i,index,k,m;
  10. char str1[100],retry,str[100],*ptr,*ctr;
  11. int j=0;
  12. printf("enter the string");
  13. fgets(str, sizeof(str), stdin);
  14. if((ptr = strchr(str, '\n')) != NULL)
  15. *ptr = '\0';
  16. printf("enter the string to be compared");
  17. fgets(str1, sizeof(str1) , stdin);
  18. if((ctr = strchr(str1, '\n')) != NULL)
  19. *ctr = '\0';
  20. /* printf("enter index"); */
  21. /* canf("%d",& index); */
  22. char *ch,*pch;
  23. ch=strtok(str,":");
  24. printf(" ratg");
  25. if (ch==NULL)
  26. {
  27. printf("entered string is incorrect,reenter? y/n");
  28. scanf("%s",&retry);
  29. if(retry=='y')
  30. function();
  31. else return;
  32. }
  33. else
  34. printf("\nch is NULL\n");
  35. while(ch!=NULL)
  36. {
  37. pch=strstr(ch,";");
  38. i=pch-ch;
  39. /* strncpy(tr[j].member1,ch,i); */
  40. /* strcpy(tr[j].member2,pch+1); */
  41. j++;
  42. ch=strtok(NULL,":");
  43. }


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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 78
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

Re: use of scanf()

 
0
  #27
Sep 27th, 2006
  1. // # include<stdio.h>
  2. void function();
  3. main()
  4. {
  5. function();
  6. }
  7. void function()
  8. {
  9. int i,index,k,m;
  10. char str1[100],retry,str[100],*ptr,*ctr;
  11. int j=0;
  12. printf("enter the string");
  13. fgets(str, sizeof(str), stdin);
  14. if((ptr = strchr(str, '\n')) != NULL)
  15. *ptr = '\0';
  16. printf("enter the string to be compared");
  17. fgets(str1, sizeof(str1) , stdin);
  18. if((ctr = strchr(str1, '\n')) != NULL)
  19. *ctr = '\0';
  20. /* printf("enter index"); */
  21. /* canf("%d",& index); */
  22. char *ch,*pch;
  23. ch=strtok(str,":");
  24. printf(" ratg");
  25. if (ch==NULL)
  26. {
  27. printf("entered string is incorrect,reenter? y/n");
  28. scanf("%s",&retry);
  29. if(retry=='y')
  30. function();
  31. else return;
  32. }
  33. else
  34. printf("\nch is NULL\n");
  35. while(ch!=NULL)
  36. {
  37. pch=strstr(ch,";");
  38. i=pch-ch;
  39. /* strncpy(tr[j].member1,ch,i); */
  40. /* strcpy(tr[j].member2,pch+1); */
  41. j++;
  42. ch=strtok(NULL,":");
  43. }
  44. }



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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 78
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

Re: use of scanf()

 
0
  #28
Sep 27th, 2006
  1. //# include<stdio.h>
  2. # include <string.h>
  3. # include<stdlib.h>
  4. void function(char *);
  5. int j;
  6. struct test
  7. {
  8. char member1[20];
  9. char member2[20];
  10. };
  11. struct test *tr;
  12. int main()
  13. {
  14. char str[100];
  15. printf("enier the string");
  16. scanf("%s",&str);
  17. tr=(struct test *)malloc( sizeof (struct test)*100);
  18. function(str);
  19. free(tr);
  20. return 0;
  21. }
  22. void function(char * str)
  23. {
  24. long int i,index,k;
  25. char str1[100],*ch,*pch;
  26. j=0;
  27. ch=strtok(str,":");
  28. if (ch==NULL)exit(0);
  29. while(ch!=NULL)
  30. {
  31. pch=strstr(ch,";");
  32. i=pch-ch;
  33. strncpy(tr[j].member1,ch,i);
  34. strcpy(tr[j].member2,pch+1);
  35. j++;
  36. ch=strtok(NULL,":");
  37. };
  38. for(k=0;k<j;k++)
  39. {
  40. printf("\n%s",tr[k].member1);
  41. printf("\n%s",tr[k].member2);}
  42. }
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
Last edited by rati; Sep 27th, 2006 at 3:36 am. Reason: code written in incorrect way
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: use of scanf()

 
1
  #29
Sep 27th, 2006
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
  1. #include<stdio.h>
  2. #include <string.h>
  3. #include<stdlib.h>
  4. void function(char *);
  5. int j;
  6. struct test
  7. {
  8. char member1[20];
  9. char member2[20];
  10. };
  11. struct test *tr;
  12. int main()
  13. {
  14. char str[100], *ptr;
  15. printf("enter the string: ");
  16. /* scanf("%s",&str); */
  17. fgets(str, sizeof(str), stdin);
  18. if((ptr = strchr(str, '\n')) != NULL)
  19. *ptr = '\0';
  20. tr=(struct test *)malloc( sizeof (struct test)*100);
  21. function(str);
  22. free(tr);
  23. return 0;
  24. }
  25. void function(char * str)
  26. {
  27. long int i,index,k;
  28. char str1[100],*ch = NULL,*pch = NULL;
  29. j=0;
  30. tr->member1[0] = '\0';
  31. tr->member2[0] = '\0';
  32.  
  33. ch=strtok(str, ":");
  34. printf("*ch = %c\n", *ch); /* debug print */
  35. if (ch==NULL)
  36. exit(0);
  37. while(ch!=NULL)
  38. {
  39. pch=strstr(ch,";");
  40. if (pch == NULL)
  41. {
  42. ch=strtok(NULL,":");
  43. continue;
  44. }
  45. printf("*pch = %c\n", *pch); /* debug print */
  46. i=pch-ch;
  47. printf("i = %ld\n", i);
  48. strncpy(tr[j].member1,ch,i);
  49. strcpy(tr[j].member2,pch+1);
  50. j++;
  51. ch=strtok(NULL,":");
  52. }
  53. for(k=0;k<j;k++)
  54. {
  55. printf("\n%s",tr[k].member1);
  56. printf("\n%s",tr[k].member2);
  57. }
  58. }
Run this
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 78
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training

Re: use of scanf()

 
1
  #30
Sep 27th, 2006
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??
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC