473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

gets(whatever) not work in some places

i have a project that i'm working on and i faces this problem
that gets in line 123 isn't work
note:
the project should be totally in c
Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #define size 1000000
  4. #include <string.h>
  5. void add_fractions (int a, int b, int c, int *d, int *ansn, int *ansd);
  6. void subtract_fractions (int a, int b, int c, int *d, int *ansn, int *ansd);
  7. void multiply_fractions (int a, int b, int c, int *d, int *ansn, int *ansd); 
  8. void divide_fractions (int a, int *b, int c, int *d, int *ansn, int *ansd) ;
  9. void simplify(int *ansn,int *ansd);
  10. int wordcon(FILE *in);
  11.  
  12. int main ()
  13. {
  14.       int cho,cho1,x,z,a_num, b_denom, c_num, d_denom,ans1,ans2,numwd;
  15.       FILE *in;
  16.       char name[size];
  17.     while (x!=0)
  18.     {
  19.           printf("please enter :\n1- Fraction addition\n2- Fraction subtraction\n3- Fraction multiplication\n4- Fraction division\n5- File processing\n6- Exit\n");
  20.           scanf("%d",&cho);
  21.           x=1;
  22.           z=1;
  23.         switch (cho){
  24.                case 1:{
  25.               printf ("Please enter the first numerator> \n"); 
  26.               scanf ("%d", &a_num); 
  27.  
  28.               printf ("Please enter the first denominator> \n "); 
  29.               scanf ("%d", &b_denom); 
  30.  
  31.               printf ("Please enter the second numerator> \n "); 
  32.               scanf ("%d", &c_num); 
  33.  
  34.               printf ("Please enter the second denominator> \n "); 
  35.               scanf ("%d", &d_denom); 
  36.               add_fractions (a_num, b_denom, c_num, &d_denom,&ans1,&ans2);
  37.               if (d_denom==0)
  38.               printf("answer undefined\n");
  39.               else 
  40.               {
  41.                    simplify(&ans1,&ans2);
  42.                    printf("the answer is = %d/%d\n",ans1,ans2);
  43.                    }
  44.                    }
  45.                break;
  46.                case 2 :{
  47.                 printf ("Please enter the first numerator> \n"); 
  48.               scanf ("%d", &a_num); 
  49.  
  50.               printf ("Please enter the first denominator> \n "); 
  51.               scanf ("%d", &b_denom); 
  52.  
  53.               printf ("Please enter the second numerator> \n "); 
  54.               scanf ("%d", &c_num); 
  55.  
  56.               printf ("Please enter the second denominator> \n "); 
  57.               scanf ("%d", &d_denom); 
  58.               subtract_fractions (a_num, b_denom, c_num, &d_denom,&ans1,&ans2);
  59.               if (d_denom==0)
  60.               printf("answer undefined\n");
  61.               else 
  62.               {
  63.                    simplify(&ans1,&ans2);
  64.                    printf("the answer is = %d/%d\n",ans1,ans2);
  65.                    }
  66.  
  67.                  }
  68.                break;
  69.                case 3 :{
  70.                 printf ("Please enter the first numerator> \n"); 
  71.               scanf ("%d", &a_num); 
  72.  
  73.               printf ("Please enter the first denominator> \n "); 
  74.               scanf ("%d", &b_denom); 
  75.  
  76.               printf ("Please enter the second numerator> \n "); 
  77.               scanf ("%d", &c_num); 
  78.  
  79.               printf ("Please enter the second denominator> \n "); 
  80.               scanf ("%d", &d_denom); 
  81.               multiply_fractions (a_num, b_denom, c_num, &d_denom,&ans1,&ans2);
  82.               if (d_denom==0)
  83.               printf("answer undefined\n");
  84.               else 
  85.               {
  86.                    simplify(&ans1,&ans2);
  87.                    printf("the answer is = %d/%d\n",ans1,ans2);
  88.                    }
  89.  
  90.                  }
  91.                break;
  92.                case 4: {
  93.                 printf ("Please enter the first numerator> \n"); 
  94.               scanf ("%d", &a_num); 
  95.  
  96.               printf ("Please enter the first denominator> \n "); 
  97.               scanf ("%d", &b_denom); 
  98.  
  99.               printf ("Please enter the second numerator> \n "); 
  100.               scanf ("%d", &c_num); 
  101.  
  102.               printf ("Please enter the second denominator> \n "); 
  103.               scanf ("%d", &d_denom); 
  104.               divide_fractions (a_num, &b_denom, c_num, &d_denom,&ans1,&ans2);
  105.               if (d_denom==0||b_denom==0)
  106.               printf("answer undefined\n");
  107.               else 
  108.               {
  109.                    simplify(&ans1,&ans2);
  110.                    printf("the answer is = %d/%d\n",ans1,ans2);
  111.                    }
  112.  
  113.                  }
  114.                break;
  115.                case 5:{
  116.                     while(z!=0){
  117.                     printf("enter:\n1- count words in the file \n2- find and display characters with their frequencies \n3- search for a word in the file\n4- go to main menu\n5- Exit\n");    
  118.                     scanf("%d",&cho1);
  119.                        switch (cho1){
  120.                            case 1:{
  121.                                 printf("Enter the name of file to be checked>\n");
  122.                                 char name[size];
  123.                                 gets(name);
  124.                                 in=fopen(name,"r");
  125.                                 if (in==NULL)
  126.                                  {
  127.                                    printf("ERROR - can't open file %s\n",name);
  128.                                    system("pause");
  129.                                    exit(0);
  130.                                    }
  131.                                 numwd=wordcon(in);
  132.                                 printf("%d\n",numwd);
  133.                                 }
  134.                                 break;
  135.                            case 2 :printf("sunday\n");
  136.                            break;
  137.                            case 3 :printf("monday\n");
  138.                            break;
  139.                            case 4: z=0;
  140.                            break;
  141.                            case 5:{x=0;
  142.                                    z=0;}    
  143.                            break;
  144.                            default: printf("wrong input\n");
  145.                            }}}
  146.                break;
  147.                case 6 :x=0;
  148.                break;
  149.                default: printf("wrong input\n");
  150.                }}
  151.            system("pause");
  152.            return 0;
  153.            }
  154.  
  155.            void simplify(int *ansn,int *ansd)
  156. {
  157.     int x,y,a,z,gcd,rem;
  158.     x=*ansd;
  159.     y=*ansn;
  160.  
  161.         if (y==0)
  162.     gcd = x;
  163.     else
  164.     {
  165.         do
  166.         {
  167.         rem = x%y;
  168.         x = y;
  169.         y = rem;
  170.  
  171.  
  172.         }
  173.         while (y!=0);
  174.         gcd = x;
  175.  
  176.  
  177.     }
  178.     *ansd=*ansd/gcd;
  179.     *ansn=*ansn/gcd;
  180. }     
  181. void add_fractions (int a, int b, int c, int *d, int *ansn, int *ansd)
  182. {
  183.     a = a*(*d);
  184.     c = c*b;
  185.     b = b*(*d);
  186.     *d = b;   
  187.  
  188.  
  189.     *ansn = a + c; 
  190.     *ansd = *d; 
  191. void subtract_fractions (int a, int b, int c, int *d, int *ansn, int *ansd)
  192. {
  193.     a = a*(*d);
  194.     c = c*b;
  195.     b = b*(*d);
  196.     *d = b;   
  197.  
  198.  
  199.     *ansn = a - c; 
  200.     *ansd = *d; 
  201. }
  202. void multiply_fractions (int a, int b, int c, int *d, int *ansn, int *ansd)
  203. {
  204.       a = a*c;
  205.     c = a; 
  206.     b = b**d; 
  207.     *d = b;  
  208.     *ansn = a; 
  209.     *ansd = b; 
  210.  
  211.  
  212. void divide_fractions (int a, int *b, int c, int *d, int *ansn, int *ansd) 
  213.     a = a*(*d); 
  214.     *d = a;  
  215.     *b = *b*c;  
  216.     c = *b;     
  217.  
  218.     *ansn = a; 
  219.     *ansd = *b; 
  220.  
  221. int wordcon(FILE *in)
  222. {
  223.  
  224.     char str[size];
  225.     fgets(str, size, in);
  226.     char delims[] = " ";
  227.     char *token;
  228.     int numwd=0;
  229.     puts(str);
  230. token = strtok( str, delims );
  231.     while ( token != NULL ) {
  232.         numwd++;
  233.         token = strtok( NULL, delims );
  234.     }
  235. return numwd;
  236. }
  237.  
  238.  
  239.  
Jun 3 '10 #1

✓ answered by Banfa

Well to start with make size a sensible value, 1000000 is rather asking for trouble.

When you say will always be null are you talking about the string name or the file pointer in?

12 1608
Use fgets() instead of gets()
Jun 3 '10 #2
how to use it with out a file
Jun 3 '10 #3
Banfa
9,065 Expert Mod 8TB
The console, command prompt has file handles through which it is accessed:

stdout - the standard output stream, normally buffered
stderr - the error output stream normally unbuffered
stdin - the standard input stream, normally buffered

To use fgets to read user input instead of the using gets you use the file handle stdin

Expand|Select|Wrap|Line Numbers
  1. char text[100];
  2.  
  3. fgets(text, sizeof text, stdin);
  4.  
Jun 3 '10 #4
char buff[SOME_SIZE];
fgets(buff, sizeof(buff), stdin);
Jun 3 '10 #5
still not working I've try these things and nothing different
Jun 3 '10 #6
Banfa
9,065 Expert Mod 8TB
What makes you say it is not working?
Jun 3 '10 #7
You should clearly explain desired behaviour from the code and the problem your are facing.
Jun 3 '10 #8
@Banfa
try to compile the code and you will not be able to enter the file name in this step and it will be always null
Expand|Select|Wrap|Line Numbers
  1. printf("Enter the name of file to be checked>\n");
  2.                                 char name[size];
  3.                                 gets(name);
  4.                                 in=fopen(name,"r");
  5.                                 if (in==NULL)
  6.                                  {
  7.                                    printf("ERROR - can't open file %s\n",name);
  8.                                    system("pause");
  9.                                    exit(0);
  10.                                    }
Jun 3 '10 #9
Banfa
9,065 Expert Mod 8TB
No that code compiles and works fine for me once I put it in main and defined all the variables.
Jun 3 '10 #10
me to but if i but it in the program it isn't work
Jun 3 '10 #11
Banfa
9,065 Expert Mod 8TB
Well to start with make size a sensible value, 1000000 is rather asking for trouble.

When you say will always be null are you talking about the string name or the file pointer in?
Jun 3 '10 #12
ok it was a sealy mistake it was about the size just about the size :(
Jun 3 '10 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Michele Simionato | last post by:
I have read with interest the recent thread about closures. The funny thing is that the authors are arguing one against the other but I actually agree with all of them and I have a proposal that...
8
by: Bartek | last post by:
Compiler: g++ code: char data; int e; //.. gets(data); // ..
13
by: Jigar Mehta | last post by:
Hye all, I have noted while debugging my program (or also in runtime execution) that sometimes (not regularly) char * pointer gets overwritten and looses its value... So, because of this my...
32
by: Marcus | last post by:
We all know that the "gets" function from the Standard C Library (which is part of the Standard C++ Library) is dangerous. It provides no bounds check, so it's easy to overwrite memory when using...
193
by: Michael B. | last post by:
I was just thinking about this, specifically wondering if there's any features that the C specification currently lacks, and which may be included in some future standardization. Of course, I...
5
by: Jeremy | last post by:
This is a variation on the last 2 unresolved questions I've posted. Having removed the "required" attribute from a field that was causing trouble, I'm finding that my dataAdapter update gets...
1
by: arielCo | last post by:
Hello, I'm attempting to use proc_open to launch, control and log CLI applications from my scripts, but my first tests are disheartening: <pre> <?php $descriptorspec = array( 0 =>...
185
by: Martin Jørgensen | last post by:
Hi, Consider: ------------ char stringinput ..bla. bla. bla. do {
104
by: jayapal | last post by:
Hi all, Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). why...? regards, jayapal.
19
by: John Salerno | last post by:
Hey all. Just thought I'd ask a general question for my own interest. Every time I think of something I might do in Python, it usually involves creating a GUI interface, so I was wondering what kind...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.