# Single Currency Converter

Hi All,
I am having trouble with a school assignment that I sure could use some help with;-)
I am required to write a C program that will accept user input representing the number of Swiss Francs they would like to convert to US Dollars.  I need to validate the user input and display it's US Dollar equivalence.  I am not sure how to write the "calculation" portion of the program or how to "loop" it if user input is invalid.  Would greatly appreciate any help you can provide;-)  Here is what I have so far...

/*This begins CurrencyConversionII; program will accept input representing the value of
the Swiss Franc, validate that input, and display its equivalence in US Dollars.*/

/*Standard Preprocessor Instructions*/
#include <stdio.h>
#include <system.h>

/*Hard-coded value of Swiss Franc and US Dollar values*/

#define CHF 0.795471     /*CHF is the accepted abreviation for the Swiss Franc*/

int main(void)     /*Program Begins*/

{

int USER_INPUT;
float RETURNED_VALUE;

printf("Currency Conversion II\n");

printf("\n"); /* inserts blank line before next line */

printf("This program will convert the Swiss Franc amount entered to US Dollars.\n");
printf("The value of one Swiss Franc in comparison to one US Dollar is 0.795471\n");

printf("\n"); /* inserts blank line before next line */

printf("Please enter the Swiss Franc amount (in whole numbers) that you would\n");

printf("\n"); /* inserts blank line before next line */

scanf("%d, &USER_INPUT"); /*User enters the number of Swiss Francs to be converted to US Dollars*/

RETURNED_VALUE = scanf("%d","&USER_INPUT");  /*Store return value of scanf() function*/

/*If user input is less than zero, equal to zero, equal to float, or equal to char,

if("&USER_INPUT < 0", "&USER_INPUT == 0", "&USER_INPUT != %f", "&USER_INPUT != %c",
"&USER_INPUT != %s\n");  /*Program loop if invalid user input*/
{

printf("This is an invalid entry and cannot be exchanged.\n");

}

/*If user input is greater than zero, not equal to zero, not equal to float,
and not equal to char, proceed to calculation*/

if("&USER_INPUT >0", "&USER_INPUT != 0", "&USER_INPUT != %f", "%USER_INPUT != %c",
"&USER_INPUT != %s, \n");

{

printf("Press the Enter key to exit.");

}

getch();

return(0);

}
###### Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x

Commented:
Ahh, my bad. Scanf actually doesn't touch the value of USER_INIT if the user doesn't input an integer (ie inputs "g")

Well, it can be solved. scanf returns EOF on an I/O error, or otherwise, the number of items assigned.

So, you want to save the return value of scanf("%d", &USER_INIT), then check if it equals "EOF". If it is, an error has occured somewhere. Then, you check it against the number of items you wanted to assign: 1 in this case. If the user hasn't entered a valid integer, the return value will be 0 (ie no items assigned), and otherwise 1 (user_init assigned).

So really there are 3 possibilities, EOF (error), 0 (no integers read) or 1 (user_init read and assigned). I will have to leave it up to you do decide how to implement your error-checking. (Hint: any value that is not 1 indicates an error, whether it's an actual system I/O error or the fact that the user typed garbage.)
0

Commented:
There are numerous problems with the above code - shouldn't it be more like

/*This begins CurrencyConversionII; program will accept input representing the value of
the Swiss Franc, validate that input, and display its equivalence in US Dollars.*/

/*Standard Preprocessor Instructions*/
#include <stdio.h>
#include <conio.h>

/*Hard-coded value of Swiss Franc and US Dollar values*/

#define CHF 0.795471     /*CHF is the accepted abreviation for the Swiss Franc*/

int main(void)     /*Program Begins*/

{

int USER_INPUT;
float RETURNED_VALUE;

printf("Currency Conversion II\n");

printf("\n"); /* inserts blank line before next line */

printf("This program will convert the Swiss Franc amount entered to US Dollars.\n");
printf("The value of one Swiss Franc in comparison to one US Dollar is 0.795471\n");

printf("\n"); /* inserts blank line before next line */

printf("Please enter the Swiss Franc amount (in whole numbers) that you would\n");

printf("\n"); /* inserts blank line before next line */

scanf("%f", &USER_INPUT); /*User enters the number of Swiss Francs to be converted to US Dollars*/

/*If user input is less than zero, equal to zero, equal to float, or equal to char,

if(USER_INPUT < 0.0 || USER_INPUT == 0.0 )  /*Program loop if invalid user input*/
{

printf("This is an invalid entry and cannot be exchanged.\n");

}

/*If user input is greater than zero, not equal to zero, not equal to float,
and not equal to char, proceed to calculation*/

if(USER_INPUT > 0.0, USER_INPUT != 0.0);

{

printf("Press the Enter key to exit.");

}

getch();

return(0);

}

?
0

Author Commented:
Hi jkr,

Would this scenario render the input invalid if the user entered a zero (no period), a char, or a string?  Also, how did you enter the "logical OR" from your keyboard?

I tried to compile and this is the error message I am getting:

C:\Program Files\Miracle C\CCEE1.c: line 40: unrecognised types in comparison
'if(USER_INPUT < 0.0 || USER_INPUT == 0.0 )   {     printf("This is an invalid entry and cannot be exchanged.\n")'
aborting compile
0

Commented:
>>Would this scenario render the input invalid if the user entered a zero (no period), a char, or a
>>string?

No, but that's part of *your* homework :o)

BTW, if you make that line read

if(USER_INPUT < 0.0)   {     printf("This is an invalid entry and cannot be exchanged.\n")

it should work also
0

Author Commented:
About the my homework thing...(LOL), that is what I was trying to do with this jumbled-up mess...render the input invalid if any of the below were "true".

if("&USER_INPUT < 0", "&USER_INPUT == 0", "&USER_INPUT == %f", "%USER_INPUT == %c", "&USER_INPUT == %s, \n");
0

Commented:
And all of them equal to 'non-zero' (i.e. 'true') somehow :o)
0

Commented:
To be serious- just enter '-1' and the program should 'break'
0

Author Commented:
Looks like I'm going around by "Laura's House", huh?  I'm going to have to think about the last thing you said: "To be serious- just enter '-1' and the program should 'break'".
0

Author Commented:
jkr,
I found the problem with this line:

C:\Program Files\Miracle C\CCEE1.c: line 40: unrecognised types in comparison
'if(USER_INPUT < 0.0 || USER_INPUT == 0.0 )   {     printf("This is an invalid entry and cannot be exchanged.\n")'
aborting compile

I "tweaked" it a little (as well as the other below it in your original response) to this:

if("&USER_INPUT < 0.0 | &USER_INPUT == 0.0\n" );

From my reading(s), I understand || to be a Logical OR; whereas, | is an Inclusive OR.
I can now compile, build, and execute, but my logic is still wrong...I can enter 45 and it tells me that my entry is invalid.

0

Commented:
Yes, because the condition inside the if is "&USER_INPUT < 0.0 | &USER_INPUT == 0.0\n" which is a const char* and its value is not null and hence it is always true. To really make your program work you need to do exactly

if( ( USER_INPUT < 0.0 )  || ( USER_INPUT == 0.0 ) )

If somebody is having problem with the | charecter, they can use ??! as a substitute. But I don't know how many of compilers support the trigraph sequences. so the check becomes

if( ( USER_INPUT < 0.0 ) ??! ( USER_INPUT == 0.0 ) )

Now rest of the things remaining same, compile the code and let us know th results.

cheers!!!
0

Commented:
Hello julielou,

Congratulations on your code. It looks better than most of the "first-time" codes I have seen. The parts you coded wrong are, clearly, the parts you didn't understand. Hopefully, these will clear things up a bit:

First of all, start your code by including stdlib.h. It's the header standard library and it must always be included in your C code.

Another reminder: Use all-capital names for preprocessor definitions (such as CHF) and other special stuff. Use lowercase or mostly lowercase in ordinary variable names. (i.e. rename USER_INPUT to user_input or userinput or userInput...)

Here is your first 'gotcha': The variable USER_INPUT is of type int, yet you are reading a float (scanf("%f"...)) into it:
Normally, it is a bad idea to try to store a floating-point value in an int.
In C, it is an error to try to assign an integer a floating-point value without a cast.
In this case, it is a _fatal_ error because you are not simply trying to assign a floating-point value to an integer. Look at the second parameter to scanf: &USER_INIT. You may or may not have covered this yet, but the unary operator & gives you the address of its operand; and therefore in this case, you are giving scanf the address of an integer variable where it is expecting the address of a floating-point variable. It is impossible for USER_INIT to get assigned the right value, even with a cast, because floating-points are stored in memory with a format much different than of integers. If the user enters "4.5", and floating-point 4.5 is represented as "101010" in the computer's memory, and "101010" is the representation of "999" for an integer, USER_INIT will equal to 999, which will be nonsense. Either change USER_INIT to a floating-point variable:

float USER_INIT;

or, give the address of a floating-point variable to scanf, and then convert that value to int:

int USER_INIT;
float temp;
scanf("%f", &temp);
USER_INIT = (int) temp; /* the (int) here is an explicit cast */

Some more things:
Since "%f" as the first parameter to scanf will read a floating-point value and a floating-point value only into USER_INIT, it will be impossible for you to know exactly what the user has entered. For example, the user may enter "0.01" or "0.01000", and since both will assign the value "0.01" to USER_INIT, you will not be able to know which the user entered, or even if it was one of the two above, and not "0.0100000".
Similarly, if the user enters a "character", a "string" or anything other than a number, USER_INIT will (as far as I know) be assigned a value of zero. Thus, you will not be able to know if the user entered an actual "0", a "0.0", or just any garbage. This is both good and bad; good because you can keep the number comparisons short:
if (USER_INIT == 0.0) /* The user either entered 0 or garbage */
and bad if your assignment requires you to explicitly check for "character"s, "strings" and other garbage. (If this is the case, you will need to read an actual string from the user, compare it to a "character" and similar garbage, and then parse it as a floating-point value if it is not. (which I don't think will be required of a fresh student, so feel free to ignore the "bad" part altogether :))

More things:
Do not enclose your 'if' conditions within "quotes". Actually, do not enclose ANYTHING within quotes unless it is a string, because enclosing something in quotes will make it a string. "USER_INIT == 0" along with the quotes is nothing but a string, and if you don't believe me you can give it to printf as a parameter and see what you get:
printf( "USER_INIT == 0" ); /* will print 'USER_INIT == 0' */

You also use commas to seperate your 'if' conditions, which will do things hard to explain here. It is obvious that you want to mean "if this OR that OR this", for which you should use the C OR operator: ||.
if (USER_INIT == 0 || USER_INIT < 0.0)
will be legal, and
if ( (USER_INIT == 0) || (USER_INIT < 0.0) )

If you are not bored of reading my comment already, (then great! You will spend the rest of your life writing senseless stuff such as "int i = 0;", picking out syntax and user errors, and generally being a programmer! *ahem*, anyways,) here is one more thing which would make your code nicer:

#define CHF 0.795471     /*CHF is the accepted abreviation for the Swiss Franc*/
/* ... more code in between ... */
printf("The value of one Swiss Franc in comparison to one US Dollar is 0.795471\n");

What if the conversion rate changes? (OK, the economy might be stable and all, but stil...)
Do you want to have to update two lines all the time? Try this:

#define CHF 0.795471     /*CHF is the accepted abreviation for the Swiss Franc*/
/* blah blah */
printf("The value of one Swiss Franc in comparison to one US Dollar is %f\n", CHF);

This will 'parametrize' the printf call, and make it print the value of CHF, whatever it is, instead of a constant "0.795...", so from then on you will only have to update the value of CHF!

Wow, I think that is all...
Good luck and have fun.
0

Author Commented:
Greetings to all who have responded to my dilemma;-)  I have incorporated suggested changes to my program, but cannot compile...I am getting this error message:

c:\program files\miracle c\ccee1.c: line 41: unrecognised types in comparison
'if(user_input < 0.0 || user_input == 0.0)  {     printf("This is an invalid entry and cannot be exchanged.\n")'
aborting compile

This is a copy of my latest Rev; I still could really use some guidance on this;-)

/*This begins CurrencyConversionII; program will accept input representing the value of
the Swiss Franc, validate that input, and display its equivalence in US Dollars.*/

/*Standard Preprocessor Instructions*/
#include <stdio.h>
#include <stdlib.h>
#include <system.h>

/*Hard-coded value of Swiss Franc and US Dollar values*/

#define CHF 0.795471     /*CHF is the accepted abreviation for the Swiss Franc*/

int main(void)     /*Program Begins*/

{

int user_input;   /*User input is an integer*/
float returned_value;   /*Returned value can be in US dollars and cents*/

printf("Currency Conversion II\n");

printf("\n"); /* inserts blank line before next line */

printf("This program will convert the Swiss Franc amount entered to US Dollars.\n");
printf("The value of one Swiss Franc in comparison to one US Dollar is %f\n", CHF);

printf("\n"); /* inserts blank line before next line */

printf("Please enter the Swiss Franc amount (in whole numbers) that you would\n");

printf("\n"); /* inserts blank line before next line */

scanf("%d", &user_input); /*User enters the number of Swiss Francs to be converted to US Dollars*/

/*If user input is less than zero, equal to zero, equal to float, or equal to char,

if(user_input < 0.0 || user_input == 0.0) /*Program loop if invalid user input*/
{

printf("This is an invalid entry and cannot be exchanged.\n");

}

/*If user input is greater than zero, not equal to zero, not equal to float,
and not equal to char, proceed to calculation*/

if(user_input > 0.0 || user_input != 0.0)

{

printf("Press the Enter key to exit.");

}

getch();

return(0);

}

0

Commented:
It is because you have changed user_init to be and be read as an integer (good!) yet you are comparing it to "0.0" which is a floating-point value:

if(user_input < 0.0 || user_input == 0.0) /*Program loop if invalid user input*/

Normally, there would be several ways to solve this problem:

1) Promote user_input to a floating-point variable with an cast (actually, the compiler should do this automatically for you and not give you any problems):
if( (float) user_input < 0.0 || (float) user_input == 0.0)

2) Demote 0.0 to an integer value:
if(user_input < (int) 0.0 || user_input == (int) 0.0)

3) But "(int) 0.0" looks rather silly, don't you think? Why write it out as a floating-point value and then convert it to an integer, while you could just use an integer value? :
if(user_input < 0 || user_input == 0)
0

Author Commented:
Hi aib 42!  And thank so much for taking time to respond!  I changed the "if" statement to read integer values as you suggested; and the program compiles just fine now; however, that is just one of several problems that I am experiencing now;-)

For example, I can enter the letter g from the keyboard and the program skips all the way to:  printf("Press the Enter key to exit.");
It is not recognizing that g is an invalid value...
If I enter 0, it tells me that this is an invalid entry but when I enter 0 again and press Enter, I exit from the program entirely.  It doesn't seem to be "looping" whenever invalid data is entered...any ideas? ;-)
0