473,795 Members | 2,861 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need assistance with detecting improper input

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("CURRENC Y 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(userInp ut[0])=='Q')
{
printf("\n\n\nT hank 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");
}

}
}

Nov 27 '05 #1
4 1990
On 26 Nov 2005 19:22:34 -0800, da************* **@gmail.com wrote:
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("CURRENC Y 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(userInp ut[0])=='Q')
This is a little late. You should do this before attempting to
convert the input to a numeric value.
{
printf("\n\n\nT hank 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>>
Nov 27 '05 #2

<da************ ***@gmail.com> wrote
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(userI nput, '\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_FAILU RE);
}
USD = atof(userInput) ;
atof() won't catcvh errors
declare a char *endptr.
USD = strtod(userInpu t, &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(userInp ut[0])=='Q')
{
printf("\n\n\nT hank 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");
}

}
}

Nov 27 '05 #3
On 26 Nov 2005 19:22:34 -0800, in comp.lang.c ,
da************* **@gmail.com wrote:
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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 27 '05 #4
Thank you all for the responses to my problem.
You have given me some great advice and guidance.
Much appreciated.

Nov 27 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1775
by: Csaba Gabor | last post by:
If I detect an empty textbox, I fill it with a value ('Dad'). If I do this twice, the second time around IE 6 fails to notice that I've cleared the textbox again, thus leaving the textbox cleared. Is there a recommended workaround? Opera 7.23 is detecting the second deletion fine. Thanks, Csaba Gabor The page snippet below illustrates what I mean:
3
1857
by: raptor | last post by:
hi, how to detect opera..it seems that even opera8 doesnt support xmlhttp fully (.i.e. sendRequestHeader). I ask this 'cause opera seems to mimic IE, at least in the preferences ?! I havent used opera till now, but it seems very buggy piece of software !! I have one very annoyng problem, fighting already ~4 hours. I found that if I use something like this in table (test are
6
1913
by: Luke Wu | last post by:
Whenever one runs across programs that use the return value from getchar() to read input, it's almost always accepted into an int-defined variable. I've read explanations on this and have always used this form. Recently, someone asked me why an int was needed, since -1 (usually EOF) can be safely represented in a signed char value. In fact, any negative number can be safely represented in a signed char value, and wouldn't conflict with...
6
1959
by: Phillip N Rounds | last post by:
I have an application which is heavily graphics intensive, all the graphics being custom. Scattered throughout by app, I have MyView->OnDraw( this->GetDC() ); Apparently, each call to this->GetDC() creates a GDI object and, 16,000 or so calls to OnDraw() results in the Application hanging because it can no longer create any new GDI objects ( I know, 16,384 )
16
6159
by: Basil Fawlty | last post by:
Hi everyone, I have an assignment, to create a simple VB program that computes the volume of a cylinder. The Form is pretty simple, it has a label and text box for base radius, another for height and another for volume with an OK button. I have the code to put into the OK button (Which I've done): Private Sub OK_Click( ) --not this line r = Val(radius.Text) --input this line h = Val(hght.Text) --input this...
1
1195
by: PJ6 | last post by:
I have a table that changes its cell (input text) colors when their contents have changed (actually on keypress) and shows a couple of buttons automatically, UDPATE and CANCEL. This works partially, though detecting enter and escape doesn't seem to work, or parhaps calling the button click events directly doesn't (see below). In addition to getting the ENTER and ESC keypresses to fire their appropriate button clicks, I need to be able to...
0
2263
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional assistance. I say additional because I've already had help which is greatly appreciated. I do try to take the time and understand the provided script in hopes on not having to trouble others on those. But here it goes...
5
8781
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not work. I am sure that this probably fairly simple, but I have not been able to figure it out. Z.K.
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10448
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9046
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.