Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How to capture wrong inputs using atoi?? (http://www.go4expert.com/forums/capture-wrong-inputs-using-atoi-t21162/)

askmewhy25 1Mar2010 20:37

How to capture wrong inputs using atoi??
 
My program can already capture strings or characters but the problem is that in some parts of the program it does not prompt that his/her input is already invalid..how can i solve this help please..

Example of problem
Code:

......
......
do{
      system("cls");
      printf("Polynomial Evaluation\n\n");
      printf("Enter the Order of Polynomial (max. 10): ");
      scanf("%s",&error);
      n=atoi(error);
                                         
    double poly[n+1];
    //Enter values of the constants
    for(m=n;m>=0;m--){
      printf("Please enter the constant for X raised to the %dth: ", m);
      scanf("%s",&error);
      poly[m]=atof(error);
      }
    printf("Enter the value of X: ");
    scanf("%s",&error);
    x=atof(error);
   
    if(n>10||n<1){
      system("cls");
      printf("Invalid input. Please try again!!!\n");
      printf("Please press the spacebar to continue!!");
      getche();
      }
    if(m>10000||m<-10000){
      system("cls");
      printf("Invalid input. Please try again!!!\n");
      printf("Please press the spacebar to continue!!");
      getche();
      }
    if(x>10000||x<-10000){
      system("cls");
      printf("Invalid input. Please try again!!!\n");
      printf("Please press the spacebar to continue!!");
      getche();
      }
      }while(n>10||n<1||m>10000||m<-10000||x>10000||x<-10000);
......
......


This is my full program
Code:

//This program can evaluate a 10th order linear polynomial equation. This
//program can also solve for statistical algorithms such as mean, summation and
//standard deviation of up to 10 maximum data and solve for matrix operations
//of 3 by 3 matrices such as addition, multiplication and transpose of matrix.

#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

//Prototypes for Polynomial Evaluation
double Polynomial();

//Prototypes for Statistical Algorithms
double Mean();
double Standard();
double Summation();

//Prototypes for Matrix Operations
double Add();
double Multi();
double Trans();

//Start Program
main(){
      int selection;
      char error[128];
      //Variables for Polynomial Evaluation
      double polysum=0, x;
      int n, QA, m;
      //Variables for Statistical Algorithms
      int SA,a,b,sum=0,data[10];
      float mean;
      double calc,sd;     
      //Variables for Matrix Operation
      int MO,i,k,j,c1,c2,r1,r2;
      int m1[3][3],m2[3][3],m3[3][3];
      //Menu Selection Screen   
      do{
        system("cls");
        printf("Menu Selection Screen\n\n");
        printf("1 - Polynomial Evaluation\n");
        printf("2 - Statistical Algorithms\n");
        printf("3 - Matrix Operation\n");
        printf("4 - Exit\n");
       
        scanf("%s",&error);
        selection=atoi(error);
        switch(selection){
          case 1://This evaluates the polynomial
            Polynomial();
            break;
          case 2:
            do{
              sum=0;
              mean=0;
              calc=0;
              sd=0;
              system("cls");
              printf("Statistical Algorithms\n\n");
              printf("1 - Mean\n");
              printf("2 - Standard Deviation\n");
              printf("3 - Summation of data\n");
              printf("4 - Go Back to Menu Selection Screen\n");
                                                                 
              scanf("%s",&error);
              SA=atoi(error);
              switch(SA){
                case 1:
                  Mean();
                  break;
                case 2:
                  Standard();
                  break;
                case 3:
                  Summation();
                  break;
                case 4://Return to the selection screen
                  main();
                  break;
                default://The program redirects invalid inputs to this part
                  system("cls");
                  printf("Invalid Input. Please try again!!!\n");
                  printf("Please press the spacebar to continue!!");
                  getche();
                  break;
                }
              }while(SA!=4);
            break;
          case 3:
            do{
              system("cls");
              printf("Matrix Operation\n\n");
              printf("1 - Matrix Addition\n");
              printf("2 - Matrix Multiplication\n");
              printf("3 - Transpose of Matrix\n");
              printf("4 - Go Back to Menu Selection Screen\n");
             
              scanf("%s",&error);
              MO=atoi(error);
              switch(MO){
                case 1:
                  Add();
                  break;
                case 2:
                  Multi();
                  break;
                case 3:
                  Trans();
                  break;
                case 4:
                  main();
                  break;
                default://The program redirects invalid inputs to this part
                  system("cls");
                  printf("Invalid Input. Please try again!!!\n");
                  printf("Please press the spacebar to continue!!");
                  getche();
                  break;
                }
                getche();
                }while(MO!=4);               
            break;
          case 4://Exit
            return 0;
            break;
          default://The program redirects invalid inputs to this part
            system("cls");
            printf("Invalid input. Please try again!!!\n");
            printf("Please press the spacebar to continue!!");
            getche();
            break;
          }
        }while(selection>4 || selection<=0);
}

//Polynomial Function
double Polynomial(){
  //Variables for Polynomial Evaluation
  double polysum=0, x;
  int n, QA, m;
  char error[128];
 
  do{
    do{
      system("cls");
      printf("Polynomial Evaluation\n\n");
      printf("Enter the Order of Polynomial (max. 10): ");
      scanf("%s",&error);
      n=atoi(error);
                                         
    double poly[n+1];
    //Enter values of the constants
    for(m=n;m>=0;m--){
      printf("Please enter the constant for X raised to the %dth: ", m);
      scanf("%s",&error);
      poly[m]=atof(error);
      }
    printf("Enter the value of X: ");
    scanf("%s",&error);
    x=atof(error);
   
    if(n>10||n<1){
      system("cls");
      printf("Invalid input. Please try again!!!\n");
      printf("Please press the spacebar to continue!!");
      getche();
      }
    if(m>10000||m<-10000){
      system("cls");
      printf("Invalid input. Please try again!!!\n");
      printf("Please press the spacebar to continue!!");
      getche();
      }
    if(x>10000||x<-10000){
      system("cls");
      printf("Invalid input. Please try again!!!\n");
      printf("Please press the spacebar to continue!!");
      getche();
      }
      }while(n>10||n<1||m>10000||m<-10000||x>10000||x<-10000);
   
    double poly[n+1];
    for(m=n;m>=0;m--)
      polysum+=poly[m]*pow(x,m);
    printf("The evaluated value of the %dth Order Polynomial  %.2lf\n", n, polysum);//Print answer
    printf("Please press the spacebar to continue!!\n");
    getche();
    do{//This used to ask the user if s/he wants to repeat the evaluation
      system("cls");
      printf("Do you want to repeat the computation?\n\n");
      printf("1 - Yes\n");
      printf("2 - No\n");
      scanf("%s",&error);
      QA=atoi(error);
      switch(QA){
        case 1:
          break;
        case 2://Return to the selection screen
          main();
          break;
        default://The program redirects invalid inputs to this part
          system("cls");
          printf("Invalid input. Please try again!!!\n");
          printf("Please press the spacebar to continue!!");
          getche();
          break; 
          }
      }while(QA>2||QA<1);
      polysum=0;
    }while(QA==1);   
}

//Mean Function
double Mean(){
  int SA,a,b,sum=0,data[10];
  float mean;
  double calc,sd;
  char error[128];
 
  do{//This solves for the Mean
    system("cls");
    printf("Mean\n\n");
    printf("Enter the Number of Data (max. 10): ");
    scanf("%s",&error);
    b=atoi(error);
    if(b>10||b<1){
      system("cls");
      printf("Invalid input. Please try again!!!\n");
      printf("Please press the spacebar to continue!!");
      getche();
      }
    }while(b>10||b<1);
  for(a=0;a<b;a++){//Enter values of the data
    printf("Enter the Actual Values of Data %d: ", a+1);
    scanf("%s",&error);
    data[a]=atoi(error);
    }
  for(a=0;a<b;a++){
    sum=sum+data[a];
    }
  mean=(float)sum/a;
  printf("The Mean of the Data is %f\n",mean);//Print answer
  printf("Please press the spacebar to continue!!");
  getche();
}

//Standard Deviation Function
double Standard(){
  int SA,a,b,sum=0,data[10];
  float mean;
  double calc,sd;
  char error[128];
 
  do{
    system("cls");
    printf("Standard Deviation\n\n");
    printf("Enter the Number of Data (max. 10): ");
    scanf("%s",&error);
    b=atoi(error);
    if(b>10||b==0){
      system("cls");
      printf("Invalid input. Please try again!!!\n");
      printf("Please press the spacebar to continue!!\n");
      getche();
      }
    }while(b>10||b==0);
  for(a=0;a<b;a++){
    printf("Enter the Actual Values of Data %d: ", a+1);
    scanf("%s",&error);
    data[a]=atoi(error);
    }
  if(b==1){
    printf("The Standard Deviation the Data is 0\n");
    printf("Please press the spacebar to continue!!");
    getche();
    }
  else{
    for(a=0;a<b;a++){
      sum=sum+data[a];
      }
    mean=(float)sum/a;
    for(a=0;a<b;a++){
      calc+=pow((data[a]-mean),2);
      }
    sd=sqrt(calc/(b-1));
    printf("The Standard Deviation the Data is %lf\n",sd);
    printf("Please press the spacebar to continue!!");
    getche();
  }
}

//Summation Function
double Summation(){
  int SA,a,b,sum=0,data[10];
  float mean;
  double calc,sd;
  char error[128];
 
  do{//This solves for the Summation
    system("cls");
    printf("Summation of Data\n\n");
    printf("Enter the Number of Data (max. 10): ");
    scanf("%s",&error);
    b=atoi(error);
    if(b>10||b<1){
      system("cls");
      printf("Invalid input. Please try again!!!\n");
      printf("Please press the spacebar to continue!!");
      getche();
      }
    }while(b>10||b<1);
  for(a=0;a<b;a++){//Enter values of the data
    printf("Enter the Actual Values of Data %d: ", a+1);
    scanf("%s",&error);
    data[a]=atoi(error);
    }
  for(a=0;a<b;a++){
    sum=sum+data[a];
    }
  mean=(float)sum/a;
  printf("The Summation of the Data is %d\n",sum);//Print answer
  printf("Please press the spacebar to continue!!");
  getche(); 
}

//Matrix Operation Functions
//Martices Addition Function
double Add(){
  int MO,i,k,j,c1,c2,r1,r2;
  int m1[3][3],m2[3][3],m3[3][3];
  char error[128];
 
  system("cls");
  printf("Enter the number of rows of the first matrix (max 3): ");
  scanf("%s",&error);
  r1=atoi(error);
  printf("Enter the number of columns of the first matrix (max 3): ");
  scanf("%s",&error);
  c1=atoi(error);
  printf("Enter the number of rows of the second matrix (max 3): ");
  scanf("%s",&error);
  r2=atoi(error);
  printf("Enter the number of columns of the second matrix (max 3): ");
  scanf("%s",&error);
  c2=atoi(error);
  if((r1==r2)&&(c1==c2)){
    printf("Addition is possible:\n");
    printf("Input Matrix one:\n");
    for(i=0;i<r1;i++){
      for(j=0;j<c1;j++){
        printf("Enter an element (row:%d column: %d): ",i+1,j+1);
        scanf("%s",&error);
        m1[i][j]=atoi(error);
        }
      }
    printf("You have entered the First Matrix as follows:\n");
    for(i=0;i<r1;i++){
      for(j=0;j<c1;j++)
        printf("%d\t",m1[i][j]);
        printf("\n");
      }
    printf("Input Matrix two:\n");
    for(i=0;i<r2;i++){
      for(j=0;j<c2;j++){
        printf("Enter an element (row:%d column: %d): ",i+1,j+1);
        scanf("%s",&error);
        m2[i][j]=atoi(error);
        }
      }
    printf("You have entered the Second Matrix as follows:\n");
    for(i=0;i<r2;i++){
      for(j=0;j<c2;j++)
      printf("%d\t",m2[i][j]);
      printf("\n");
      }
    /* Addition of Matrix*/
    for(i=0;i<r1;i++){
      for(j=0;j<c1;j++)
        m3[i][j]=m1[i][j]+ m2[i][j];
      }
    printf("The sum is:\n");
    for(i=0;i<c1;i++){
      for(j=0;j<r1;j++)
        printf("%5d",m3[i][j]);
        printf("\n");
      }
  }
  else
    printf("\n Addition is not possible:-");
  }

//Matrices Multiplication Function
double Multi(){
  int MO,i,k,j,c1,c2,r1,r2;
  int m1[3][3],m2[3][3],m3[3][3];
  char error[128];
 
  system("cls");
  printf("Enter the number of rows of the first matrix (max 3): ");
  scanf("%s",&error);
  r1=atoi(error);
  printf("Enter the number of columns of the first matrix (max 3): ");
  scanf("%s",&error);
  c1=atoi(error);
  printf("Enter the number of rows of the second matrix (max 3): ");
  scanf("%s",&error);
  r2=atoi(error);
  printf("Enter the number of columns of the second matrix (max 3): ");
  scanf("%s",&error);
  c2=atoi(error);
  if(c1==r2){
    printf("Multiplication is possible:\n");
    printf("Input value of Matrix one:\n");
    for(i=0;i<r1;i++){
      for(j=0;j<c1;j++){
        printf("Enter an element (row:%d column: %d): ",i+1,j+1);
        scanf("%s",&error);
        m1[i][j]=atoi(error);
        }
      }
    printf("You have entered the First Matrix as follows:\n");
    for(i=0;i<r1;i++){
      for(j=0;j<c1;j++)
        printf("%d\t",m1[i][j]);
        printf("\n");
      }
    printf("Input value of Matrix two:\n");
    for(i=0;i<r2;i++){
      for(j=0;j<c2;j++){
        printf("Enter an element (row:%d column: %d): ",i+1,j+1);
        scanf("%s",&error);
        m2[i][j]=atoi(error);
        }
      }
    printf("You have entered the Second Matrix as follows:\n");
    for(i=0;i<r2;i++){
      for(j=0;j<c2;j++)
        printf("%d\t",m2[i][j]);
        printf("\n");
      }
    for(i=0;i<r1;i++)
      for(j=0;j<c2;j++){
        m3[i][j]=0;
        for(k=0;k<c1;k++)
          m3[i][j]=m3[i][j]+m1[i][k]*m2[k][j];
        }
    /*Displaying final matrix*/
    printf("Multiplication of Matrix:\n");
    for(i=0;i<r1;i++){
      for(j=0;j<c2;j++)
        printf("\t%d",m3[i][j]);
        printf("\n");
      }
    }
  else
    printf("Multiplication is not possible");
}

//Matrix Transpose Function
double Trans(){
  int MO,i,k,j,c1,c2,r1,r2;
  int m1[3][3],m2[3][3],m3[3][3];
  char error[128];
 
  system("cls");
  printf("Enter the number of row: ");
  scanf("%s",&error);
  r1=atoi(error);
  printf("Enter the number of coloum: ");
  scanf("%s",&error);
  c1=atoi(error);
  printf("Enter the element\n\n");
  for(i=0;i<r1;i++){
    for(j=0;j<c1;j++){
      printf("Enter an element (row:%d column: %d): ",i+1,j+1);
      scanf("%s",&error);
      m1[i][j]=atoi(error);
      m2[j][i]=m1[i][j];
      }
    }
  /*Displaying transpose of matrix*/
  printf("Transpose of Matrix is:\n");
  for(i=0;i<r1;i++){
    for(j=0;j<c1;j++)
      printf("\t%d",m2[i][j]);
      printf("\n");
    }
}


Gene Poole 1Mar2010 21:23

Re: How to capture wrong inputs using atoi??
 
instead of atoi()/atof() try strtol()/strtod(). They take an additional parameter that tells where the conversion stops. With that info, you can determine if the input is valid.

Here's a demo:
Code:

#include <stdlib.h>
#include <string.h>

int main()
{
  char test1[]="1.0225";
  char test2[]="1.0225 with junk";
  char test3[]="All junk";
 
  double d;
  char *p;
  int len;

  len=strlen(test1);
  d=strtod(test1,&p);
  if(p==test1){
    printf("No conversion tool place. Garbage: %s\n",p);
  }else if((p-test1)<len){
    printf("partial converions took place.  Value: %lf, garbage: %s\n",d,p);
  }else{
    printf("Complete conversion took place: %lf\n",d);
  }

  len=strlen(test2);
  d=strtod(test2,&p);
  if(p==test2){
    printf("No conversion tool place. Garbage: %s\n",p);
  }else if((p-test2)<len){
    printf("partial converions took place.  Value: %lf, garbage: %s\n",d,p);
  }else{
    printf("Complete conversion took place: %lf\n",d);
  }

  len=strlen(test3);
  d=strtod(test3,&p);
  if(p==test3){
    printf("No conversion tool place. Garbage: %s\n",p);
  }else if((p-test3)<len){
    printf("partial converions took place.  Value: %lf, garbage: %s\n",d,p);
  }else{
    printf("Complete conversion took place: %lf\n",d);
  }


  return 0;
}


askmewhy25 4Mar2010 19:26

Re: How to capture wrong inputs using atoi??
 
Sir I understand what your demo want to say but what should be the values of the tests array if there would be no range of values to be inputted and the only thing that the program should capture if the input is a character or a string

Gene Poole 4Mar2010 19:58

Re: How to capture wrong inputs using atoi??
 
strtol and strtod will take any amount of leading whitespace up to the number. It will then try to convert the number using a simple set of rules which allow leading zeros, +/-, and with wcstod, will accept ieee exponential notation (like "3.04e-12"). At the first sign of something NOT able to be converted (like the "1.0225 with junk" after the '5'), it stops converting and sets the second argument to the place where it stopped. This can then be used (as I show in the demo) to determine if the number completely converted, partially converted, or didn't convert at all. You can use the logic as above to re-prompt or warn the user, etc.


All times are GMT +5.5. The time now is 10:59.