?
Solved

Program in Miracle C

Posted on 2006-04-24
15
Medium Priority
?
593 Views
Last Modified: 2011-10-03
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
Comment
Question by:martinez1112
  • 6
  • 4
  • 3
  • +1
15 Comments
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16531987
Its seems fine.
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 16531988
Do you have any specific question ?
0
 

Author Comment

by:martinez1112
ID: 16532002
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:martinez1112
ID: 16532003
I'm running out of time and I did not realize that part.
0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 16532416
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

by:Jase-Coder
ID: 16532434
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

by:rajeev_devin
rajeev_devin earned 400 total points
ID: 16532505
>> /*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

by:martinez1112
ID: 16532555
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
 
LVL 11

Expert Comment

by:WelkinMaze
ID: 16532694
hi,

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

Author Comment

by:martinez1112
ID: 16532717
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

by:WelkinMaze
ID: 16532727
try
if (ret <= 0.0)
or
'if ((int)ret <= 0)
0
 

Author Comment

by:martinez1112
ID: 16534567
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

by:
WelkinMaze earned 1600 total points
ID: 16534713
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

by:martinez1112
ID: 16534786
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

by:WelkinMaze
WelkinMaze earned 1600 total points
ID: 16534873
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question