gets(whatever) not work in some places

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • non star
    New Member
    • Jun 2010
    • 6

    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
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #define size 1000000
    #include <string.h>
    void add_fractions (int a, int b, int c, int *d, int *ansn, int *ansd);
    void subtract_fractions (int a, int b, int c, int *d, int *ansn, int *ansd);
    void multiply_fractions (int a, int b, int c, int *d, int *ansn, int *ansd); 
    void divide_fractions (int a, int *b, int c, int *d, int *ansn, int *ansd) ;
    void simplify(int *ansn,int *ansd);
    int wordcon(FILE *in);
    
    int main ()
    {
          int cho,cho1,x,z,a_num, b_denom, c_num, d_denom,ans1,ans2,numwd;
          FILE *in;
          char name[size];
        while (x!=0)
        {
              printf("please enter :\n1- Fraction addition\n2- Fraction subtraction\n3- Fraction multiplication\n4- Fraction division\n5- File processing\n6- Exit\n");
              scanf("%d",&cho);
              x=1;
              z=1;
            switch (cho){
                   case 1:{
                  printf ("Please enter the first numerator> \n"); 
                  scanf ("%d", &a_num); 
      
                  printf ("Please enter the first denominator> \n "); 
                  scanf ("%d", &b_denom); 
      
                  printf ("Please enter the second numerator> \n "); 
                  scanf ("%d", &c_num); 
      
                  printf ("Please enter the second denominator> \n "); 
                  scanf ("%d", &d_denom); 
                  add_fractions (a_num, b_denom, c_num, &d_denom,&ans1,&ans2);
                  if (d_denom==0)
                  printf("answer undefined\n");
                  else 
                  {
                       simplify(&ans1,&ans2);
                       printf("the answer is = %d/%d\n",ans1,ans2);
                       }
                       }
                   break;
                   case 2 :{
                    printf ("Please enter the first numerator> \n"); 
                  scanf ("%d", &a_num); 
      
                  printf ("Please enter the first denominator> \n "); 
                  scanf ("%d", &b_denom); 
      
                  printf ("Please enter the second numerator> \n "); 
                  scanf ("%d", &c_num); 
      
                  printf ("Please enter the second denominator> \n "); 
                  scanf ("%d", &d_denom); 
                  subtract_fractions (a_num, b_denom, c_num, &d_denom,&ans1,&ans2);
                  if (d_denom==0)
                  printf("answer undefined\n");
                  else 
                  {
                       simplify(&ans1,&ans2);
                       printf("the answer is = %d/%d\n",ans1,ans2);
                       }
                  
                     }
                   break;
                   case 3 :{
                    printf ("Please enter the first numerator> \n"); 
                  scanf ("%d", &a_num); 
      
                  printf ("Please enter the first denominator> \n "); 
                  scanf ("%d", &b_denom); 
      
                  printf ("Please enter the second numerator> \n "); 
                  scanf ("%d", &c_num); 
      
                  printf ("Please enter the second denominator> \n "); 
                  scanf ("%d", &d_denom); 
                  multiply_fractions (a_num, b_denom, c_num, &d_denom,&ans1,&ans2);
                  if (d_denom==0)
                  printf("answer undefined\n");
                  else 
                  {
                       simplify(&ans1,&ans2);
                       printf("the answer is = %d/%d\n",ans1,ans2);
                       }
                  
                     }
                   break;
                   case 4: {
                    printf ("Please enter the first numerator> \n"); 
                  scanf ("%d", &a_num); 
      
                  printf ("Please enter the first denominator> \n "); 
                  scanf ("%d", &b_denom); 
      
                  printf ("Please enter the second numerator> \n "); 
                  scanf ("%d", &c_num); 
      
                  printf ("Please enter the second denominator> \n "); 
                  scanf ("%d", &d_denom); 
                  divide_fractions (a_num, &b_denom, c_num, &d_denom,&ans1,&ans2);
                  if (d_denom==0||b_denom==0)
                  printf("answer undefined\n");
                  else 
                  {
                       simplify(&ans1,&ans2);
                       printf("the answer is = %d/%d\n",ans1,ans2);
                       }
                  
                     }
                   break;
                   case 5:{
                        while(z!=0){
                        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");    
                        scanf("%d",&cho1);
                           switch (cho1){
                               case 1:[U]{
                                    printf("Enter the name of file to be checked>\n");
                                    char name[size];
                                    gets(name);
                                    in=fopen(name,"r");
                                    if (in==NULL)
                                     {
                                       printf("ERROR - can't open file %s\n",name);
                                       system("pause");
                                       exit(0);
                                       }
                                    numwd=wordcon(in);
                                    printf("%d\n",numwd);
                                    }
                                    break;[/U]
                               case 2 :printf("sunday\n");
                               break;
                               case 3 :printf("monday\n");
                               break;
                               case 4: z=0;
                               break;
                               case 5:{x=0;
                                       z=0;}    
                               break;
                               default: printf("wrong input\n");
                               }}}
                   break;
                   case 6 :x=0;
                   break;
                   default: printf("wrong input\n");
                   }}
               system("pause");
               return 0;
               }
               
               void simplify(int *ansn,int *ansd)
    {
        int x,y,a,z,gcd,rem;
        x=*ansd;
        y=*ansn;
        
            if (y==0)
        gcd = x;
        else
        {
            do
            {
            rem = x%y;
            x = y;
            y = rem;
     
     
            }
            while (y!=0);
            gcd = x;
            
            
        }
        *ansd=*ansd/gcd;
        *ansn=*ansn/gcd;
    }     
    void add_fractions (int a, int b, int c, int *d, int *ansn, int *ansd)
    {
        a = a*(*d);
        c = c*b;
        b = b*(*d);
        *d = b;   
              
      
        *ansn = a + c; 
        *ansd = *d; 
    } 
    void subtract_fractions (int a, int b, int c, int *d, int *ansn, int *ansd)
    {
        a = a*(*d);
        c = c*b;
        b = b*(*d);
        *d = b;   
              
      
        *ansn = a - c; 
        *ansd = *d; 
    }
    void multiply_fractions (int a, int b, int c, int *d, int *ansn, int *ansd)
    {
          a = a*c;
        c = a; 
        b = b**d; 
        *d = b;  
        *ansn = a; 
        *ansd = b; 
      
    } 
    
    void divide_fractions (int a, int *b, int c, int *d, int *ansn, int *ansd) 
    { 
        a = a*(*d); 
        *d = a;  
        *b = *b*c;  
        c = *b;     
      
        *ansn = a; 
        *ansd = *b; 
      
    } 
    int wordcon(FILE *in)
    {
        
        char str[size];
        fgets(str, size, in);
        char delims[] = " ";
        char *token;
        int numwd=0;
        puts(str);
    token = strtok( str, delims );
        while ( token != NULL ) {
            numwd++;
            token = strtok( NULL, delims );
        }
    return numwd;
    }
  • mayankarpit
    New Member
    • Jun 2010
    • 13

    #2
    Use fgets() instead of gets()

    Comment

    • non star
      New Member
      • Jun 2010
      • 6

      #3
      how to use it with out a file

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

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

        Code:
        char text[100];
        
        fgets(text, sizeof text, stdin);

        Comment

        • mayankarpit
          New Member
          • Jun 2010
          • 13

          #5
          char buff[SOME_SIZE];
          fgets(buff, sizeof(buff), stdin);

          Comment

          • non star
            New Member
            • Jun 2010
            • 6

            #6
            still not working I've try these things and nothing different

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              What makes you say it is not working?

              Comment

              • mayankarpit
                New Member
                • Jun 2010
                • 13

                #8
                You should clearly explain desired behaviour from the code and the problem your are facing.

                Comment

                • non star
                  New Member
                  • Jun 2010
                  • 6

                  #9
                  Originally posted by Banfa
                  What makes you say it is not working?
                  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
                  Code:
                  printf("Enter the name of file to be checked>\n");
                                                  char name[size];
                                                  gets(name);
                                                  in=fopen(name,"r");
                                                  if (in==NULL)
                                                   {
                                                     printf("ERROR - can't open file %s\n",name);
                                                     system("pause");
                                                     exit(0);
                                                     }

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    No that code compiles and works fine for me once I put it in main and defined all the variables.

                    Comment

                    • non star
                      New Member
                      • Jun 2010
                      • 6

                      #11
                      me to but if i but it in the program it isn't work

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        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?

                        Comment

                        • non star
                          New Member
                          • Jun 2010
                          • 6

                          #13
                          ok it was a sealy mistake it was about the size just about the size :(

                          Comment

                          Working...