Need assistance with detecting improper input

D

david.w.anderson1

This is a homework assignment.
The code works but does not "catch" all the improper input.
The requirement is to be able to accept one input currency, which is
error checked as a valid entry, and then display its equivalency in US
dollars.

The program does detect when a letter is entered as the 1st digit.
I need the program to detect when a number-letter combination is
entered (example 1a) and show the error.

I need the program to detect when a comma is entered (example: 12,34
instead of 12.34) and show the error.

Thanks in advance.

Here is the code:

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

#define PHP 54.36000
#define AFA 45.54600
#define KRW 1051.50
#define JPY 118.740
#define GBP 0.58011
#define TRUE 1
#define FALSE !TRUE
int main(void)
{

int intNum;
char userInput[20];
float USD;


printf("CURRENCY CONVERSION\n");
printf("This program will show currency conversions to US
Dollars.\n\n");
printf("1 US Dollar = %4.2f \t Phillipine Pesos\n", PHP);
printf("1 US Dollar = %4.2f \t Afghanistan Afghani\n", AFA);
printf("1 US Dollar = %4.2f \t Korean Won\n", KRW);
printf("1 US Dollar = %4.2f \t Japanese Yen\n", JPY);
printf("1 US Dollar = %4.2f \t British Pounds\n\n\n", GBP);
printf("Press any key to continue...\n\n");
getch (); // Waits for keystroke before proceeding

printf("To show an example of this program, we will convert:\n");
printf("British Pounds to U.S. Dollars.\n\n");
printf("Enter the amount of British Pounds to convert then select
the ENTER key:\n\n");
getch ();

while(TRUE)
{
gets(userInput);
USD = atof(userInput);

if( toupper(userInput[0])=='Q')
{
printf("\n\n\nThank you for using this program.\n");
printf("Please press any key to close this window\n");
getch();
break;
}

else if( USD <= 0.0)
{
printf("\nYou have entered an invalid amount. Your
entry was: %s\n",userInput);
printf("Please enter a valid amount and then select the
ENTER key:\n");
}
else
{
printf("\n\n\n\n\n");
printf("You entered %s British Pounds:\n\n", userInput);
printf("The conversion of %s British Pounds to US
Dollars is:\n\n\n", userInput);
printf("\t\t $%.2f\n\n\n\n", USD / GBP);
printf("Enter another amount or press q to exit.\n");
}

}
}
 
B

Barry Schwarz

This is a homework assignment.
The code works but does not "catch" all the improper input.
The requirement is to be able to accept one input currency, which is
error checked as a valid entry, and then display its equivalency in US
dollars.

The program does detect when a letter is entered as the 1st digit.
I need the program to detect when a number-letter combination is
entered (example 1a) and show the error.

I need the program to detect when a comma is entered (example: 12,34
instead of 12.34) and show the error.

Thanks in advance.

Here is the code:

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

A non-standard header which will prevent others from helping and from
which you receive almost no benefit.
#include <stdlib.h>
#include <ctype.h>

#define PHP 54.36000
#define AFA 45.54600
#define KRW 1051.50
#define JPY 118.740
#define GBP 0.58011
#define TRUE 1
#define FALSE !TRUE
int main(void)
{

int intNum;
char userInput[20];
float USD;


printf("CURRENCY CONVERSION\n");
printf("This program will show currency conversions to US
Dollars.\n\n");
printf("1 US Dollar = %4.2f \t Phillipine Pesos\n", PHP);
printf("1 US Dollar = %4.2f \t Afghanistan Afghani\n", AFA);
printf("1 US Dollar = %4.2f \t Korean Won\n", KRW);
printf("1 US Dollar = %4.2f \t Japanese Yen\n", JPY);
printf("1 US Dollar = %4.2f \t British Pounds\n\n\n", GBP);
printf("Press any key to continue...\n\n");
getch (); // Waits for keystroke before proceeding

Why use a non-standard function when getchar is available?
printf("To show an example of this program, we will convert:\n");
printf("British Pounds to U.S. Dollars.\n\n");
printf("Enter the amount of British Pounds to convert then select
the ENTER key:\n\n");
getch ();

while(TRUE)
{
gets(userInput);

There is no way to prevent gets from overflowing your buffer if the
user enters too much input. Consider using fgets.
USD = atof(userInput);

atof provides no information about any errors it encounters. Look up
the strtod() function in your manual. It has features which allow you
to do what you want.
if( toupper(userInput[0])=='Q')

This is a little late. You should do this before attempting to
convert the input to a numeric value.
{
printf("\n\n\nThank you for using this program.\n");
printf("Please press any key to close this window\n");
getch();
break;
}

else if( USD <= 0.0)

This will change when you use strtod.
{
printf("\nYou have entered an invalid amount. Your
entry was: %s\n",userInput);
printf("Please enter a valid amount and then select the
ENTER key:\n");
}
else
{
printf("\n\n\n\n\n");
printf("You entered %s British Pounds:\n\n", userInput);
printf("The conversion of %s British Pounds to US
Dollars is:\n\n\n", userInput);
printf("\t\t $%.2f\n\n\n\n", USD / GBP);
printf("Enter another amount or press q to exit.\n");
}

}
}


<<Remove the del for email>>
 
M

Malcolm

gets(userInput);
Make this
fgets(userInput, 20, stdin);
Now if the user types more than 20 characters, the buffer will fill up and
there will be no newline (with gets() the buffer will overflow and the
computer will probably crash, maybe give a confusing error, depending what
gets hits).
So check.
if(strchr(userInput, '\n') == 0)
{
fprintf(stderr, "Line too long"):
/* easiest thing is to quit, but you can try to read the rest of the
line if you want */
/* I'll leave that for you you implement */
exit(EXIT_FAILURE);
}
USD = atof(userInput);
atof() won't catcvh errors
declare a char *endptr.
USD = strtod(userInput, &endptr);
The endptr points to then end of the readable, floating point input. If is
is whitespace or NUL, you are OK, if it contains printable characters then
user typed a number followed by garbage. If endptr equals the start, the
user typed garbage, maybe followed by a valid number, maybe not.
if(endptr == userInput)
/* cannot convert at all */
if( endptr != 0 && isspace(*endptr) == 0)
/* garbage after number */
if( toupper(userInput[0])=='Q')
{
printf("\n\n\nThank you for using this program.\n");
printf("Please press any key to close this window\n");
getch();
break;
}

else if( USD <= 0.0)
{
printf("\nYou have entered an invalid amount. Your
entry was: %s\n",userInput);
printf("Please enter a valid amount and then select the
ENTER key:\n");
}
else
{
printf("\n\n\n\n\n");
printf("You entered %s British Pounds:\n\n", userInput);
printf("The conversion of %s British Pounds to US
Dollars is:\n\n\n", userInput);
printf("\t\t $%.2f\n\n\n\n", USD / GBP);
printf("Enter another amount or press q to exit.\n");
}

}
}
 
M

Mark McIntyre

On 26 Nov 2005 19:22:34 -0800, in comp.lang.c ,
gets(userInput);
USD = atof(userInput);

Don't use gets(), and avoid the ato... family.

Read the FAQ for the right way to do this. By the way it will also
help your problem of dealing with invalid input.
 
D

david.w.anderson1

Thank you all for the responses to my problem.
You have given me some great advice and guidance.
Much appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top