• C

Program in Miracle C

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");


}
martinez1112Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rajeev_devinCommented:
Its seems fine.
0
rajeev_devinCommented:
Do you have any specific question ?
0
martinez1112Author Commented:
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
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

martinez1112Author Commented:
I'm running out of time and I did not realize that part.
0
Jase-CoderCommented:
you could do something like

if( isdigit(input) && input < 0)
   fprintf(stderr, "You entered a digit or a negative number");
0
Jase-CoderCommented:
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
rajeev_devinCommented:
>> /*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
martinez1112Author Commented:
Hello rajeev_devin


I did it but is giving me this error


c:\program files\miracle c\currency convertion programwk4up3.c: line 45: variable 'ret' not found
'ret = scanf("%f", &USD)'
aborting compile
0
WelkinMazeCommented:
hi,

you have to declare the cariable 'ret'.
for example:
float ret = 0.0;
0
martinez1112Author Commented:
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
WelkinMazeCommented:
try
if (ret <= 0.0)
or
'if ((int)ret <= 0)
0
martinez1112Author Commented:
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
WelkinMazeCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
martinez1112Author Commented:
Great this is what I was looking for.  
what the "int ret = 0;" do?
does "(stdin)" means- standard input?
what is ret?

0
WelkinMazeCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.