J
Just starting out
I am very new to C code and I'm having a lot of trouble with a homework
assignment.
This program is supposed to take the amount of Euros that the user
enters and convert it to US dollars.
It runs fine if the user enters a number, but if the user enters a
letter it loops.
I have been working on this for 3 hours now, trying different things
left and right. I'm sure it has something to do with the isdigit
function. So, I tried adding in a char cResponse and switching the
value to fResponse after checking for a number.
I am fried, please help!
---------------------------------------
#include <stdio.h>
#include <system.h>
#include <ctype.h>
main ()
{
//set variables
float fUSD, fEUR, fResponse;
//initialize variables
fUSD = 0;
fEUR = 0;
fResponse = 0;
//set values
fUSD = 1.00;
fEUR = .7435;
//print headers for output screen
printf("\n***Currency Conversion***\n");
printf("\nConverts Euro into US dollar\n");
printf("\nPlease enter the amount in Euro: ");
//get user input
scanf("%f", &fResponse);
//check for number greater than zero
while (fResponse<=0.0) {
if (isdigit(fResponse)) {
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}
if (isdigit(fResponse)==0)
printf("\nYou entered a letter.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}
printf("\nThe value of the Euro you entered converts to $%.2f US
dollars.\n", (fUSD/fEUR) * fResponse);
printf("\n\nPress any key to close this window. . .");
//getch function to leave results on screen until any key is chosen
getch ();
}
assignment.
This program is supposed to take the amount of Euros that the user
enters and convert it to US dollars.
It runs fine if the user enters a number, but if the user enters a
letter it loops.
I have been working on this for 3 hours now, trying different things
left and right. I'm sure it has something to do with the isdigit
function. So, I tried adding in a char cResponse and switching the
value to fResponse after checking for a number.
I am fried, please help!
---------------------------------------
#include <stdio.h>
#include <system.h>
#include <ctype.h>
main ()
{
//set variables
float fUSD, fEUR, fResponse;
//initialize variables
fUSD = 0;
fEUR = 0;
fResponse = 0;
//set values
fUSD = 1.00;
fEUR = .7435;
//print headers for output screen
printf("\n***Currency Conversion***\n");
printf("\nConverts Euro into US dollar\n");
printf("\nPlease enter the amount in Euro: ");
//get user input
scanf("%f", &fResponse);
//check for number greater than zero
while (fResponse<=0.0) {
if (isdigit(fResponse)) {
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}
if (isdigit(fResponse)==0)
printf("\nYou entered a letter.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}
printf("\nThe value of the Euro you entered converts to $%.2f US
dollars.\n", (fUSD/fEUR) * fResponse);
printf("\n\nPress any key to close this window. . .");
//getch function to leave results on screen until any key is chosen
getch ();
}