Thread: Limiting the data received from scanf.

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    12

    Limiting the data received from scanf.

    Hello everyone! I'm having somewhat of a problem trying to sort this out. In this program i want for the scanf to only read one digit, and in case the user inputs characters or other numbers following that digit, ideally the program would disregard said characters and numbers. Furthermore, in this program i will have to use two «scanf's», one for the function i want to use, and another for the point in which i want to apply the function.
    I appreciate the help.
    Here's the program:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    
    void show_help()
    {
      printf("\n This program allows you to calculate the derivative of one of the following functions in a certain point.\n\n");
      printf("1: sin(x);\n2: cos(3*x);\n3: tan(x);\n4: asin(x);\n5: acos(x);\n6: log(x);\n7: log10(x);\n8: exp(5*x);\n9: 3 cos(2*x) sin(4*x).\n\n");
    }
    
    
    
    
    double derivada ( double (*func) (double), double ponto )
    {
      double deriv, f1, h, f2;
      h = 1e-6 * ponto;
      f1 = func(ponto + h);
      f2 = func(ponto);
    
    
      deriv = (f1-f2)/h;
    
    
      return deriv;
    }
    
    
    double cos3x ( double valor )
    {
      return cos(3*valor);
    }
    
    
    
    
    
    
    double exp5x ( double valor )
    {
      return exp(5*valor);
    }
    
    
    
    
    
    
    double cos_sin ( double valor )
    {
      return (3*cos(2*valor)*sin(4*valor));
    }
    
    
    double dcos3x ( double valor )
    {
      return (-3*sin(3*valor));
    }
    
    
    double dtan ( double valor )
    {
      return (1/(cos(valor)*cos(valor)));
    }
    
    
    double dasin ( double valor )
    {
      return (1/sqrt(1-(valor*valor)));
    }
    
    
    double dacos ( double valor )
    {
      return (-1/sqrt(1-(valor*valor)));
    }
    
    
    double dlog ( double valor )
    {
      return (1/valor);
    }
    double dlog10 ( double valor )
    {
      return (1/(valor*log(10)));
    }
    
    
    double dexp5x ( double valor )
    {
      return (5*exp(5*valor));
    }
    
    
    double dcos_sin ( double valor )
    {
      return (-6*sin(2*valor)*sin(4*valor)+12*cos(2*valor)*cos(4*valor));
    }
    
    
    int main ()
    {
    
    
      int c, p, w, d, t1;
      double a, b, z;
      double (*funcao)(double);
      double (*f2)(double);
    
    
    
    
      show_help();
    
    
      while(1)
        {
    
    
          printf("Input a number from 1 to 9 corresponding to the function you want to apply the derivative, and the respective point.\n");
    
    
          d = scanf("%d", &c);
    
    
          if (d == 0)
    	{
    	  printf("\nError!  You need to input a number!\n");
    	  break;
    	}
    
    
          if (c == 0)
    	{
    	  show_help();
    	  printf("Input a number from 1 to 9 corresponding to the function you want to apply the derivative, and the respective point.\n\n");
    	  scanf("%d", &c);
    	}
    
    
          if ( c > 9 || c < 0 )
    	{
    	  printf("\nError! The number %d does not correspond to any function.\n", c);
    	  continue;
    	}
    
    
          t1=scanf("%lf", &b);
    
    
          if(t1 == 0)
          {
              printf("Erro! You need to input a number corresponding to the point you want to apply the derivative of the fucntion!");
              break;
          }
          if(b == 0)
          {
              printf("Erro! Function is divided by 0.\n");
              continue;
          }
    
    
          if(c == 1)
    	{
    	  funcao = sin;
    	  f2=cos;
    	}
    
    
          if(c == 2)
    	{
    	  funcao = cos3x;
    	  f2=dcos3x;
    	}
    
    
          if(c == 3)
    	{
    	  funcao = tan;
    	  f2=dtan;
    	}
    
    
          if(c == 4)
    	{
    	  if ( b<=-1 || b>=1 )
    	    {
    	      printf("ERROR! Function isn't defined in that point\n");
    	      continue;
    	    }
    	  funcao = asin;
    	  f2=dasin;
    
    
    	}
    
    
          if(c == 5)
    	{
    	  if ( b<=-1 || b>=1 )
    	    {
    	      printf("ERROR! Function isn't defined in that point\n");
    	      continue;
    	    }
    	  funcao = acos;
    	  f2=dacos;
    
    
    	}
    
    
          if(c == 6)
    	{
    	  if ( b<=0 )
    	    {
    	      printf("ERROR! Function isn't defined in that point.\n");
    	      continue;
    	    }
    	  funcao = log;
    	  f2=dlog;
    
    
    	}
    
    
          if(c == 7)
    	{
    	  if ( b<=0 )
    	    {
    	      printf("ERROR! Function isn't defined in that point.\n");
    	      continue;
    	    }
    
    
    	  funcao = log10;
    	  f2=dlog10;
    	}
    
    
          if(c == 8)
    	{
    	  funcao = exp5x;
    	  f2=dexp5x;
    	}
    
    
          if(c == 9)
    	{
    	  funcao = cos_sin;
    	  f2=dcos_sin;
    	}
    
    
    
    
          a=derivada(funcao,b);
          z=f2(b);
          printf("Derivada=%lf\n",a);
          printf("Derivada exata=%lf.\n",z);
          printf("Erro resultante da aproximação efetuada = %0.8lf.\n",(z-a));
    
    
          printf("Pretende continuar?\nSe sim pressione 1, caso contrário digite outro número.\n");
          w= scanf("%d",&p);
          if(p != 1 || w==0)
    	break;
    
    
        }
      return 0;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Read a single character:
    Code:
    scanf(" %c", &c)
    convert it to a digit:
    Code:
    if (isdigit(c)) {
        digit = c - '0';
    }
    then discard the rest of the line:
    Code:
    while ((input = getchar()) != '\n' && input != EOF) {
        continue;
    }
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    Ok, makes sense, i understood the concept behind it, but is there any other way to go about changing the character into a number without resorting to the function «isdigit()», because supposedly i still haven't learned that yet, so i'm still not allowed to utilize that function. One more thing, the input mentioned in the third piece of code, is it reffering to the one in «scanf», in this case c? Thank you very much for the reply!

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by user_0x View Post
    is there any other way to go about changing the character into a number without resorting to the function «isdigit()», because supposedly i still haven't learned that yet, so i'm still not allowed to utilize that function.
    "if (isdigit(c))" is an efficient version of:
    Code:
    if (c >= '0' && c <= '9')
    Quote Originally Posted by user_0x View Post
    One more thing, the input mentioned in the third piece of code, is it reffering to the one in «scanf», in this case c? Thank you very much for the reply!
    Oh, I used a different name for that because in my example, "c" should be a char and "input" should be an int.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    12
    Ok, i have sort of restructured the code, and i'm only lacking in something right now, which is the part regarding the «while» cycle that will allow the program to discard the rest of the line. My only doubt being what do i use as the «input». You already mentioned it should be an int, and sorry for asking this again, but what do i exactly use as the int that connects to the remaining piece of code, in this case, after doing the:
    Code:
    if( c >= '0' && c <= '9'){
    n = c - '0';
    }
    //I defined n as an int
    following this structure what is my «input» in this case? Could you explain how is it that this «while» cycle works, so that it discards the rest of the line.
    Sorry for being repetitive, and thanks for the help.
    Last edited by user_0x; 11-26-2017 at 04:11 PM.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    "input" as I said should be an int variable. You use it to save the return value of getchar(), to check it for equality with both '\n'(newline) and EOF(End-of-File). You can do assignment in a while statement like this, it's generally discouraged as bad practice but I think it has its uses.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-03-2011, 03:00 AM
  2. scanf not functioning? impossible value received
    By vsovereign in forum C Programming
    Replies: 7
    Last Post: 02-26-2010, 02:36 PM
  3. loading struct from UART received data
    By droseman in forum C Programming
    Replies: 19
    Last Post: 01-08-2009, 09:38 AM
  4. Received data conversion
    By jmd15 in forum Networking/Device Communication
    Replies: 14
    Last Post: 08-06-2005, 07:37 AM
  5. Limiting scanf input
    By SMurf in forum C Programming
    Replies: 0
    Last Post: 11-21-2002, 06:39 AM

Tags for this Thread