• C

Error Checking with a Separate Function

Ok, so now I have this program:

#include <stdio.h>
#include <stdio.h>

main()
{

     /* Variables */
     float yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */
     char currency; /* the currency/money the user defined */

     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("Please select a currency conversion to display: ");
     scanf("%c",&currency);
     

     /* User selects which currency to convert. */

     switch (currency)
     {
          case '1':      /* Japanese Yen Selection */
               yen = 0.00848104;
               printf("\n");
               printf("1 Japanese Yen = %f",yen);
               printf(" US Dollar\n");
               break;

          case '2':      /* Canadian Dollar Selection */
               canada = 0.863782;
               printf("\n");
               printf("1 Canadian Dollar = %f",canada);
               printf(" US Dollar\n");
               break;

          case '3':      /* European Euro Selection */
               euro = 1.1882;
               printf("\n");
               printf("1 European Euro = %f",euro);
               printf(" US Dollar\n");
               break;

          case '4':      /* Brazilian Real Selection */
               real = 0.47259;
               printf("\n");
               printf("1 Brazilian Real = %f",real);
               printf(" US Dollar\n");
               break;

          case '5':      /* Australian Dollar Selection */
               aussie = 0.738002;
               printf("\n");
               printf("1 Australian Dollar = %f",aussie);
               printf(" US Dollar\n");
               break;

          default:     /* Default message in the event user does not enter 1 through 5 selection */
               printf("\n");
               printf("Please rerun the program and enter a number between 1 and 5.");
               printf("\nWould you want to continue [Y for Yes, anything else for No]?");
               fflush(stdin);
               currency = getch();

               if(currency == 'y' || currency == 'Y') /*Error checking to continue or end the program */
               {
                    main();
                    return 0;
               }
               else
                    return 0;

     }

     /* Display the results on the screen */
     fflush(stdin);
     printf("\nPress any key to end.");
     getche();
 
 }


Current my program calls the existing main program, which cannot happen. I need a separate function that checks for user input for all situations, like decimals, negatives, letters, characters, etc. What should I do and can you write out the code for me? Thank you.
byrianrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

byrianrAuthor Commented:
Ok, I am making some changes. I have the program as follows with a function, but when I put in a decimal, it does not run well. HELP!!!! I need to keep this structure, but make it decimal proof.

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

int userinfo(int);

#define TRUE 1
#define FALSE 0

int main()
{

     /* Variables */
     float yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */
     int currency; /* the currency/money the user defined */


   while (currency != 6)
   {
      /*Call Input Validation function*/
      currency = userinfo(currency);
     
     
     switch (currency)
     {
          case 1:      /* Japanese Yen Selection */
               yen = 0.00848104;
               printf("\n");
               printf("1 Japanese Yen = %f",yen);
               printf(" US Dollar\n");
               break;

          case 2:      /* Canadian Dollar Selection */
               canada = 0.863782;
               printf("\n");
               printf("1 Canadian Dollar = %f",canada);
               printf(" US Dollar\n");
               break;

          case 3:      /* European Euro Selection */
               euro = 1.1882;
               printf("\n");
               printf("1 European Euro = %f",euro);
               printf(" US Dollar\n");
               break;

          case 4:      /* Brazilian Real Selection */
               real = 0.47259;
               printf("\n");
               printf("1 Brazilian Real = %f",real);
               printf(" US Dollar\n");
               break;

          case 5:      /* Australian Dollar Selection */
               aussie = 0.738002;
               printf("\n");
               printf("1 Australian Dollar = %f",aussie);
               printf(" US Dollar\n");
               break;
               
          case 6:      /*User exits program*/
                printf("\n");
                printf("Thank You. Have a nice day. Now exiting the program.");
                break;

          default:     /* Default message in the event user does not enter 1 through 5 selection */
               printf("\n");
               printf("Please rerun the program and enter a number between 1 and 5.");
               printf("\nWould you want to continue [Y for Yes, anything else for No]?");
               fflush(stdin);
               currency = getch();

               if(currency == 'y' || currency == 'Y') /*Error checking to continue or end the program */
               {
                    main();
                    return 0;
               }
               else
                    return 0;

     }
}

     /* Display the results on the screen */
     fflush(stdin);
     printf("\nPress any key to end.");
     getche();
 
 }
 
 int userinfo()
{
      char ucurrency [50]= "";
      int i = 0;
      int flag = TRUE;
      int currency = 0;
     

     
     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("6) Exit Program\n");
     printf("Please select a currency conversion to display: ");
     gets(ucurrency);
     
      while(1)
      {
         for( i=0; ucurrency[i] != '\0'; i++)
                {
                   if(!isdigit(ucurrency[i]) || ucurrency[i] != '.')
                       flag = FALSE;
                }
         if (flag = FALSE)
            {
               printf("\nYou have entered an invalid input. Please enter a value between 1-5.");
            }
         else if  (flag = TRUE)
            {
               currency = atoi(ucurrency);
            }
         if(flag = TRUE)
                 break;
       }
   return(currency);
}  
0
sunnycoderCommented:
1. replace
gets(ucurrency);

with
fgets (ucurreny, 50, stdin);

2.      /* Display the results on the screen */
     fflush(stdin);
this fflush is undefined .. perhaps you meant fflush(stdout);

3. Use strtol() to validate your input. It will save you a lot of sweat

char * temp;
ret_val = strtol (ucurrency, &temp, 10);
If there is no error, then check tha value pointed by temp. If it is \n or \0 or whitespace, your input was correct.

Cheers!
sunnycoder
0
fridomCEO/ProgrammerCommented:
Using gets is always a fault
fflush(stdin) invoked undefined behaviour.

Are you forced you use such case statments? Have you considered putting the information in some structur? Have you learned about structures?

Separate the menu section from the section on how much you like to convert.

This currency problem seems to be a real all-time favourite. I suggest you check other messages about it.

Here's a suggestion for  a menu, IIRC I posted this code some months ago also.

enum {BUF_SIZE=128, MAX_CHOICE=2};
struct info {
  double conversion_factor;
  char *currency_symbol;
};


static struct info currencies[MAX_CHOICE] = {{0.853446, "EUR"},
                                             {1.15, "Can-$"}};

static int menu (void){
  char buf[BUF_SIZE];
  int i_rval = 0;
  char *pc = NULL;
  int i;
 
  int result;
  do {
    printf("%s", "Converting from US-$ to other currencies\n\n");
    for (i = 0; i < MAX_CHOICE; ++i){
      printf("%d for converting to %s\n", i, currencies[i].currency_symbol);
    }
    printf("%d for Quit\n", MAX_CHOICE);
    printf("\nPlease choose ");
    i_rval = fflush(stdout);
    pc = fgets(buf, BUF_SIZE, stdin);
    if (NULL == pc) {
      exit(EXIT_FAILURE);
      /* good enough for an example */
    }
    i_rval = sscanf(buf, "%d", &result);
    if (i_rval != 1 || (result < 0 || result > MAX_CHOICE)) {
      i_rval = 0;
      printf("Not a valid choice please retry\n");
    }
  } while ((0 == i_rval) || result < 0 || result > MAX_CHOICE);
  return result;
}

Feel free to improve it.

Regards
Friedrich
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

byrianrAuthor Commented:
This is what I have. And the Decimal, like 3.4 returns the 3rd option, versus saying it is invalid. What should I do?

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

int userinfo(int);

#define TRUE 1
#define FALSE 0

int main()
{

     /* Variables */
     float yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */
     int currency; /* the currency/money the user defined */


   while (currency != 6)
   {
      /*Call Input Validation function*/
      currency = userinfo(currency);
     
     
     switch (currency)
     {
          case 1:      /* Japanese Yen Selection */
               yen = 0.00848104;
               printf("\n");
               printf("1 Japanese Yen = %f",yen);
               printf(" US Dollar\n");
               break;

          case 2:      /* Canadian Dollar Selection */
               canada = 0.863782;
               printf("\n");
               printf("1 Canadian Dollar = %f",canada);
               printf(" US Dollar\n");
               break;

          case 3:      /* European Euro Selection */
               euro = 1.1882;
               printf("\n");
               printf("1 European Euro = %f",euro);
               printf(" US Dollar\n");
               break;

          case 4:      /* Brazilian Real Selection */
               real = 0.47259;
               printf("\n");
               printf("1 Brazilian Real = %f",real);
               printf(" US Dollar\n");
               break;

          case 5:      /* Australian Dollar Selection */
               aussie = 0.738002;
               printf("\n");
               printf("1 Australian Dollar = %f",aussie);
               printf(" US Dollar\n");
               break;
               
          case 6:      /*User exits program*/
                printf("\n");
                printf("Thank You. Have a nice day. Now exiting the program.");
                break;

          default:     /* Default message in the event user does not enter 1 through 5 selection */
               printf("\n");
               printf("Please rerun the program and enter a number between 1 and 5.");
               printf("\nWould you want to continue [Y for Yes, anything else for No]?");
               fflush(stdin);
               currency = getch();

               if(currency == 'y' || currency == 'Y') /*Error checking to continue or end the program */
               {
                    main();
                    return 0;
               }
               else
                    return 0;

     }
}

     /* Display the results on the screen */
     fflush(stdout);
     printf("\nPress any key to end.");
     getche();
 
 }
 
 int userinfo()
{
      char ucurrency [50]= "";
      int i = 0;
      int flag = TRUE;
      int currency = 0;
     

     
     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("6) Exit Program\n");
     printf("Please select a currency conversion to display: ");
    // gets(ucurrency);
     fgets (ucurrency, 50, stdin);
     
      while(1)
      {
        for( i=0; ucurrency[i] != '\0'; i++)
                {
                   if(!isdigit(ucurrency[i]) || ucurrency[i] != '.')
                       flag = FALSE;
                }
         if (flag = FALSE)
            {
               printf("\nYou have entered an invalid input. Please enter a value between 1-5.");
            }
         else if  (flag = TRUE)
            {
               currency = atoi(ucurrency);
            }
         if(flag = TRUE)
                 break;
       }
   return(currency);
}  
0
sunnycoderCommented:
     while(1) //why do you need this loop? The for loop is sufficient to determine if there was an invalid character and requires only one if else to act on this judgement .. what is the purpose of this infinte loop?
      {
          for( i=0; ucurrency[i] != '\0'; i++)
          {
                  if(!isdigit(ucurrency[i]) || ucurrency[i] != '.')
                        flag = FALSE;
           }
           if (flag = FALSE) //comaprison operator is == and not = this is assignment
           {
                  printf("\nYou have entered an invalid input. Please enter a value between 1-5.");
           }
           else if  (flag = TRUE) //comaprison operator is == and not = this is assignment
           {
                currency = atoi(ucurrency);
           }
           if(flag = TRUE) //comaprison operator is == and not = this is assignment. If this was comparison operator, you would be stuck in infinte loop forever in case of incorrect input
                 break;

Idea is

flag = true
retval=0

scan input char by char
        if there was invalid input
              mark flag to false
               break out of loop

if flag == true
       retval = convert input to int
else
       print error

return retval

Cheers!
sunnycoder
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
byrianrAuthor Commented:
That does not work. When the choice from the menu is entered, the program ends.
0
sunnycoderCommented:
show me the code
0
sunnycoderCommented:
I just did the modifications I proposed at my end and it worked fine.

A few observations though::

1. userinfo() prototype declaration and definition are different

2. currency is uninitialized when it is first used in while loop for comparison. You should either use do while loop instead of while loop or initialize it to proper value

3. you dont have to recursively call main .. continuing the loop would serve your purpose

4. Your initialization of yen,canada,euro etc is static and can be done outside the loop. You do not have to perform the initializaion in the loop.

5. Heed to compiler warnings ... More often than not, they are indicative of potential problems.

Cheers!
sunnycoder
0
byrianrAuthor Commented:
Here is my code:

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

int userinfo(int);

#define TRUE 1
#define FALSE 0

int main()
{

     /* Variables */
     float yen,canada,euro,real,aussie; /* 5 conversion variables - each variable is the US dollar equivalent to 1 foreign currency */
     int currency; /* the currency/money the user defined */


   while (currency != 6)
   {
      /*Call Input Validation function*/
      currency = userinfo(currency);
     
     
     switch (currency)
     {
          case 1:      /* Japanese Yen Selection */
               yen = 0.00848104;
               printf("\n");
               printf("1 Japanese Yen = %f",yen);
               printf(" US Dollar\n");
               break;

          case 2:      /* Canadian Dollar Selection */
               canada = 0.863782;
               printf("\n");
               printf("1 Canadian Dollar = %f",canada);
               printf(" US Dollar\n");
               break;

          case 3:      /* European Euro Selection */
               euro = 1.1882;
               printf("\n");
               printf("1 European Euro = %f",euro);
               printf(" US Dollar\n");
               break;

          case 4:      /* Brazilian Real Selection */
               real = 0.47259;
               printf("\n");
               printf("1 Brazilian Real = %f",real);
               printf(" US Dollar\n");
               break;

          case 5:      /* Australian Dollar Selection */
               aussie = 0.738002;
               printf("\n");
               printf("1 Australian Dollar = %f",aussie);
               printf(" US Dollar\n");
               break;
               
          case 6:      /*User exits program*/
                printf("\n");
                printf("Thank You. Have a nice day. Now exiting the program.");
                break;

          default:     /* Default message in the event user does not enter 1 through 5 selection */
               printf("\n");
               printf("Please rerun the program and enter a number between 1 and 5.");
               printf("\nWould you want to continue [Y for Yes, anything else for No]?");
               fflush(stdin);
               currency = getch();

               if(currency == 'y' || currency == 'Y') /*Error checking to continue or end the program */
               {
                    main();
                    return 0;
               }
               else
                    return 0;

     }
}

     /* Display the results on the screen */
     fflush(stdout);
     printf("\nPress any key to end.");
     getche();
 
 }
 
 int userinfo()
{
      char ucurrency [50]= "";
      int i = 0;
      int flag = TRUE;
      int currency = 0;
     

     
     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("6) Exit Program\n");
     printf("Please select a currency conversion to display: ");
    // gets(ucurrency);
     fgets (ucurrency, 50, stdin);
     
     while(1)
      {
        for( i=0; ucurrency[i] != '\0'; i++)
                {
                   if(!isdigit(ucurrency[i]) || ucurrency[i] != '.')
                       flag = FALSE;
                }
         if (flag = FALSE)
            {
               printf("\nYou have entered an invalid input. Please enter a value between 1-5.");
            }
         else if  (flag = TRUE)
            {
               currency = atoi(ucurrency);
            }
         if(flag = TRUE)
                 break;
       }
   return(currency);
}  

Show me your code with modifications.
0
sunnycoderCommented:
You did not make any modifications which I had suggested .. let me copy paste and explain them again for reference

      while(1) ----- >>>> why do you need this loop? The for loop is sufficient to determine if there was an invalid character and requires only one if else to act on this judgement .. what is the purpose of this infinte loop? Remove it !!
      {
          for( i=0; ucurrency[i] != '\0'; i++)
          {
                  if(!isdigit(ucurrency[i]) || ucurrency[i] != '.')
                        flag = FALSE; ------->>> you can break out of the for loop as soon as you set the flag to false
           }
           if (flag = FALSE) //comaprison operator is == and not = .... this is assignment  ... to compare change it to  if (flag == FALSE) .... note the extra =
           {
                  printf("\nYou have entered an invalid input. Please enter a value between 1-5.");
           }
           else if  (flag = TRUE) //comaprison operator is == and not = ... this is assignment same as above
           {
                currency = atoi(ucurrency);
           }
           if(flag = TRUE) //comaprison operator is == and not = this is assignment. If this was comparison operator, you would be stuck in infinte loop forever in case of incorrect input ... this if can be removed
                 break;

Algorithm is ...

flag = true
retval=0

scan input char by char  ....... (the for loop)
        if there was invalid input     .... if (
              mark flag to false              ..... flag =
               break out of loop

if flag == true                              .... if ( flag == true
       retval = convert input to int            ... atoi
else
       print error

return retval

Cheers!
sunnycoder
0
deepu chandranCommented:
what you did in this program is complicated way.
you can make this code more simple.

i just rewrite ur userinfo function,
 int userinfo()
{
      int ucurrency;
     /* Display the five possible curreny conversions */
     printf("\nCurrency Conversion \n");
     printf("1) Japanese Yen\n");
     printf("2) Canadian Dollar\n");
     printf("3) European Euro\n");
     printf("4) Brazilian Real\n");
     printf("5) Australian Dollar\n");
     printf("6) Exit Program\n");
     printf("Please select a currency conversion to display: ");
     printf(" %d",ucurrency);
     if((ucurrency<1) &&(ucurrency>6))
     {
        return(ucurrency);
      }
     else
     {
      return -1;//  you can add error message here
     }
}  
 i think this wil help u

cheers
deepu
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.