Solved

# Program in Miracle C

Posted on 2006-04-24
583 Views
Hello I Just find out I need to check this program that way the input is only positive number. I been told that I don't need to use a loop in the program. Can some give me a little bit of help.

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

main()
{
/*HARD CODE Information from
the currency converter posted in the course materials*/
float USD = 1.00; /*1.00 USD = 1.00 United States Dollar*/
float EUR = 0.9223357; /*1.00 USD = 0.9223357 Europe EURO*/
float JPY = 118.218; /*1.00 USD = 118.218 Japan Yen*/
float MXN = 10.79889; /*1.00 USD = 10.79889 Mexico Pesos*/
float RUB = 31.9191; /*1.00 USD = 31.9191 Russian Ruble*/
float SEK = 8.48792; /*1.00 USD = 8.48792 Swedish Kronor*/
char chrUserInput[16];
float totalEuros = 0.0;
float totalYen   = 0.0;
float totalPesos = 0.0;
float totalRuble = 0.0;
float totalKronor = 0.0;

/*print title of program*/
printf("\n\tMultiple Currency Conversion");/*In previous Version the Title was just Currency Convertion*/
printf("\n");
/*Ask for amout of \$ to be convewrted*/
printf("\nHow many US Dolars do you want to convert?\t\$\a");
scanf("%f", &USD);

/*Calculation*/

totalEuros = USD * EUR;
totalYen   = USD * JPY;
totalPesos = USD * MXN;
totalRuble = USD * RUB;
totalKronor = USD * SEK;

/*Amount Converted will be show*/

printf ("\n");
printf ("With that amount you will get...\n");/*statement that user will see on the top of the convertions*/
printf ("\n");/*Add a line in between each output*/
printf ("\t%3.2f\tEUR\n", totalEuros);
printf ("\n");
printf ("\t%3.2f\tJPY\n", totalYen);
printf ("\n");
printf ("\t%3.2f\tMXN\n", totalPesos);
printf ("\n");
printf ("\t%3.2f\tRUB\n", totalRuble);
printf ("\n");
printf ("\t%3.2f\tSEK\n\n", totalKronor);

/* End the Program with the message */
printf ("\tConversion complete \"I DID IT AGAIN\"\a");

}
0
Question by:martinez1112

LVL 12

Expert Comment

Its seems fine.
0

LVL 12

Expert Comment

Do you have any specific question ?
0

Author Comment

How can add a failure message when I enter a  negative number or a character. Are there any functions out there that I can use to make sure that the program only accept positive number and not characters nor -Num.

0

Author Comment

I'm running out of time and I did not realize that part.
0

LVL 11

Expert Comment

you could do something like

if( isdigit(input) && input < 0)
fprintf(stderr, "You entered a digit or a negative number");
0

LVL 11

Expert Comment

sorry,
I was supposed to use isalpha in the above example so I should have written:

if( isalpha(input) || input < 0)
fprintf(stderr, "You entered a digit or a negative number");

you could use the isdigit function by doing;

if(!(isdigit(user_input) && use_input > 0))
fprintf(stderr, "You entered a digit or a negative number");

BTW you need to include the following file to use these functions

#include <ctype.h>
0

LVL 12

Assisted Solution

>> /*Ask for amout of \$ to be convewrted*/
>> printf("\nHow many US Dolars do you want to convert?\t\$\a");
>> scanf("%f", &USD);
You can change the above three lines to this.

do
{
/*Ask for amout of \$ to be convewrted*/
printf("\nHow many US Dolars do you want to convert?\t\$\a");
ret = scanf("%f", &USD);
if (ret <= 0)
{
printf("\nInvalid input. Try again\n");
fflush(stdin);
}
}while (ret <= 0);
0

Author Comment

Hello rajeev_devin

I did it but is giving me this error

'ret = scanf("%f", &USD)'
aborting compile
0

LVL 11

Expert Comment

hi,

you have to declare the cariable 'ret'.
for example:
float ret = 0.0;
0

Author Comment

I did and I still getting

c:\program files\miracle c\currency convertion programwk4up3.c: line 50: unrecognised types in comparison
'if (ret <= 0) { printf("\nInvalid input. Try again\n")'
aborting compile
0

LVL 11

Expert Comment

try
if (ret <= 0.0)
or
'if ((int)ret <= 0)
0

Author Comment

Great that work fot he character section but not for the negative number this is what I got.

it does work do not acept characters but still aceting negative number.

#include <stdio.h>
#include <math.h>
#include <ctype.h>

main()
{
/*HARD CODE Information from
the currency converter posted in the course materials*/
float USD = 1.00; /*1.00 USD = 1.00 United States Dollar*/
float EUR = 0.9223357; /*1.00 USD = 0.9223357 Europe EURO*/
float JPY = 118.218; /*1.00 USD = 118.218 Japan Yen*/
float MXN = 10.79889; /*1.00 USD = 10.79889 Mexico Pesos*/
float RUB = 31.9191; /*1.00 USD = 31.9191 Russian Ruble*/
float SEK = 8.48792; /*1.00 USD = 8.48792 Swedish Kronor*/
char chrUserInput[16];
float totalEuros = 0.0;
float totalYen   = 0.0;
float totalPesos = 0.0;
float totalRuble = 0.0;
float totalKronor = 0.0;
float ret = 0.0;

/*print title of program*/
printf("\n\tMultiple Currency Conversion");/*In previous Version the Title was just Currency Convertion*/
printf("\n");
do
{
/*Ask for amout of \$ to be convewrted*/
printf("\nHow many US Dolars do you want to convert?\t\$\a");
ret = scanf("%f", &USD);
if (ret <= 0.0)
{
printf("\nInvalid input. Try again\n");
fflush(stdin);
}
}while (ret <= 0.0);

/*Calculation*/

totalEuros = USD * EUR;
totalYen   = USD * JPY;
totalPesos = USD * MXN;
totalRuble = USD * RUB;
totalKronor = USD * SEK;

/*Amount Converted will be show*/

printf ("\n");
printf ("With that amount you will get...\n");/*statement that user will see on the top of the convertions*/
printf ("\n");/*Add a line in between each output*/
printf ("\t%3.2f\tEUR\n", totalEuros);
printf ("\n");
printf ("\t%3.2f\tJPY\n", totalYen);
printf ("\n");
printf ("\t%3.2f\tMXN\n", totalPesos);
printf ("\n");
printf ("\t%3.2f\tRUB\n", totalRuble);
printf ("\n");
printf ("\t%3.2f\tSEK\n\n", totalKronor);

/* End the Program with the message */
printf ("\tConversion complete \"I DID IT AGAIN\"\a");

}
0

LVL 11

Accepted Solution

change this one:

float ret = 0.0;

with

int ret = 0;

and also replace this:

do
{
/*Ask for amout of \$ to be convewrted*/
printf("\nHow many US Dolars do you want to convert?\t\$\a");
ret = scanf("%f", &USD);
if (ret <= 0.0)
{
printf("\nInvalid input. Try again\n");
fflush(stdin);
}
}while (ret <= 0.0);

with:

do
{
/*Ask for amout of \$ to be convewrted*/
printf("\nHow many US Dolars do you want to convert?\t\$\a");
ret = scanf("%f", &USD);
if (ret <= 0)
{
printf("\nInvalid input. Try again\n");
fflush(stdin);
}
if (USD <= 0.0)
{
printf("\nNegative amount! Try again\n");
fflush(stdin);
}
} while ((ret <= 0) || (USD <= 0.0));
0

Author Comment

Great this is what I was looking for.
what the "int ret = 0;" do?
does "(stdin)" means- standard input?
what is ret?

0

LVL 11

Assisted Solution

ret is used to check the result of scanf operation.
You can see here: http://www.opengroup.org/onlinepubs/007908799/xsh/fscanf.html
"RETURN VALUE
Upon successful completion, these functions return the number of successfully matched and assigned input items; this number can be 0 in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF is returned. If a read error occurs the error indicator for the stream is set, EOF is returned, and errno is set to indicate the error."

so if ret is <= 0 this means that there are not succesfully read items (float numbers)

The first time I've gone very fast trhough the source and at a second glance I've seen that it has to be int not float. So I've said to change its type to int and because 0.0 is a designation for float number to change it to 0.

Yes, you're correct about stdin, stdin is the standart input.
0

## Featured Post

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.