Link to home
Start Free TrialLog in
Avatar of Cabochick05
Cabochick05

asked on

Yes; another currency conversion question!

Week 3 class assignment:  (like usual from what I've seen here on this site)
Create a program in basic C that will list 5 foreign currency exchange rates to the US Dollar.  Allow the user to choose 1-5 foreign currency; then allow the user to input the amount of foreign currency to be converted to US dollar amount.  Display the foreign currency amount in USD. In other words; "500 pesos = x US Dollars?  Use switch/case statements to offer choice of currency to be converted to USD.

I am having trouble from the get go.  I feel if I can get one case to work I can finish the rest.  I have not used the #define before and am not sure if I'm using it correctly here so I have it commented out for now until I understand how it works. (Not the way our instructor showed us)  Then, I can't seem to get the exchange calculation to return anything in my first case statement.  Syntax or calculation? We have learned (ha=) about the basics only and we are supposed to write our programs with what we've learned.  (if/else, while, do while, subroutines, cnt, switch/case, we have not used 'char' or 'void'. Have only used int and float.)  This is what I have so far.  This is one of 3 assignments due by Tuesday 6p, mst.  Please help me I am not looking for the answer but really want to learn how to do this!


#include <stdio.h>

Main()
{
//#define peso = 1;
//#define yen = 2;
//#define pound = 3;
//#define can = 4;
//#define ruble = 5;

float peso = 0.0885426,yen = 0.00895015,pound = 1.8775,Can = 0.910415,ruble = 0.0374186;

int choice;
float nbr = 0.0;
float total = 0.0;

      printf("\n Foriegn Currency Conversion to US Dollars");
      printf("\n1: 1 Mexican peso = 0.0885426 U.S. dollar");
      printf("\n2: 1 Japanese yen = 0.00895015 U.S. dollar");
      printf("\n3: 1 British pound = 1.8775 U.S. dollar");
      printf("\n4: 1 Canadian dollar = 0.910415 U.S. dollar");
      printf("\n5: 1 Russian ruble = 0.0374186 U.S. dollar");
       
       printf( "\n Enter the number from the list above to convert to US dollars: ");
      scanf ( "%d", &choice);
      
      printf( "Enter the amount to convert to USD: ");
         scanf( "%.2f", &nbr);
         
      switch (choice)
{
   case 1:
            total = (peso*nbr);
            printf("%.2f pesos = $%.2fUSD", nbr,total);
            break;
        
   default:
            printf("You did not make a valid selection");
}/* end switch/case*/      
getchar();
}/*end main program*/
Avatar of F. Dominicus
F. Dominicus
Flag of Germany image

it's always int main (void) or int main(int argc , char* argv[]} And it's definitly not Main.
the sntax foe define is wrong, please check the docs you ahve it should be
#define FOO 1
/* it's some sort of convention (of course not enforced) to give defines a fully upper case name

scanf is  a poor choice for user input.

I do not understand what you mean with:
"Then, I can't seem to get the exchange calculation to return anything in my first case statement."

Why should it return anything, you don't do anything to really return anything.
if you want to return anything (but to whom in your case you just have one function?) use return
You dispatch on the choice, (assuming the scanf worked properly)

and then you can write
switch(choice) {
     case PESO: // do whatever you like.


The things you have learned so far a enough to get all the things implemented. So I can not see why you whine about the unfair tutor.

Friedrich
Friedrich be nice!

Cabochick05,

>>Main()
This wont work. it should be:

main ( int argc, char * argv[] )

>>//#define peso = 1;
Should be
#define peso 1

Try that and see what else goes wrong. Post what you try and tell us what you think is wrong.

Paul
Avatar of cwwkie
cwwkie

> >>//#define peso = 1;
> Should be
> #define peso 1

but in that case you shouldn't use peso for something else:

> float peso = 0.0885426,yen = 0.00895015,pound = 1.8775,Can = 0.910415,ruble = 0.0374186;

that will give an error. You can avoid this if you use uppercase for the #define as fridom suggested:

#define PESO 1

and something else, instead of
     printf("\n1: 1 Mexican peso = 0.0885426 U.S. dollar");
you can do
     printf("\n1: 1 Mexican peso = %1.8f U.S. dollar", peso);
So you don't have to enter the rate twice.
Hi Susan,

The way #defines work is a bit strange. Essentially, a #define is saying "from now on, whenever I use the word xxx I mean the word yyy".

For example:

#define PESO 1

means that later, if you do:

case PESO:

it will be treated as exactly:

case 1:

To help differentiate between #defines and normal 'C' variables, it is customary to define them in all uppercase.

Paul
Avatar of Cabochick05

ASKER

I'm finally up again to work on this.  Thank you for your input.  I'll see what I can do here and update you.  Fredrich, I am so sorry I sounded like I was complaining about my instructor.  I only meant that he is explicit in what he wants us to use.  Fo example, the use of /* is for basic C and the use of // is for another program.  Also, the only thing I'v e learn in class regarding main is main(), not main ( int argc, char * argv[] ).  I don't know what 'argc' is nor have we used char or argv[].  Is there a way to write this program without the use of #define?  and keep main() only?
I'm going to get going now and will check for your input.  Thanks so much.  You all have been wonderful for helping me.  I may not go  crazy now!  Chat soon. Susan
ok.  the program prompts for the choice and the amount to be converted but there is no return of exchange.  For Fredrich's sake, when I referred to the scanf return, I mean't the printf statements within the cases themselves as nothing comes back to show the exchange amount in USD and I can't figure out why.  Here is what I have after implementing the suggestions:

/*5/31/06, POS370 Susan Johnson Individual assignment 2 of 3: Find 5 foreign currencies and display conversion in us dollars*/
/*Offer the user a choice of which foreign currency and how much to exchange into USD*/

#include <stdio.h>

main ( int argc, char * argv[] )

{
#define PESO 1;
#define YEN 2;
#define POUND 3;
#define CAN 4;
#define RUBLE 5;

float peso = 0.0885426,yen = 0.00895015,pound = 1.8775,can = 0.910415,ruble = 0.0374186;

int choice;
float nbr = 0.0;
float total;

      printf("\n Foriegn Currency Conversion to US Dollars");
      printf("\n1: 1 Mexican peso = %1.8f USD", peso);
      printf("\n2: 1 Japanese yen = %1.8f USD", yen);
      printf("\n3: 1 British pound = %1.4f USD",pound);
      printf("\n4: 1 Canadian dollar = %1.6f USD",can);
      printf("\n5: 1 Russian ruble = %1.7f USD",ruble);
       
       printf( "\n\nEnter the number from the list above to convert to US dollars: ");
      scanf ( "%d", &choice);
      
      printf( "\n\nEnter the amount to convert to USD: ");
         scanf( "%.2f", &nbr);
         
      switch (choice)
{/*begin switch/case*/
   case 1:
            total = peso*nbr;
            printf("%.2f pesos = $%.2f USD", nbr,total);
            break;
   case 2:
            total = yen*nbr;
            printf("%.2f yen = $%.2f USD", nbr,total);
            break;
   case 3:
            total = pound*nbr;
            printf("%.2f pounds = $%.2f USD", nbr,total);
            break;
   case 4:
            total = can*nbr;
            printf("%.2f canadian dollars = $%.2f USD", nbr,total);
            break;
   case 5:
            total = ruble*nbr;
            printf("%.2f ruble = $%.2f USD", nbr,total);
            break;
        
   default:
            printf("You did not make a valid selection");
}/* end switch/case*/
 
getchar();
}/*end main program*/
> #define PESO 1;

A #define is not a statement which you should end with a semicolon! But as you do not use it, it does not give a compiler error.

> scanf( "%.2f", &nbr);

You do not need the .2 here, "%f" is just fine. See http://cplusplus.com/ref/cstdio/scanf.html
For enquiring minds...I got rid of the #defines as cwwkie said, I wasn't using them.  I think its working now; however, the case default initiates after the second request for user input is asked, "Enter the amount to convert to USD: ".  I'd like the default: "You did not make a valid selection" to show up after the menu selection is made.  Does anyone have a suggestion for an if or while statement and where to put it to make this happen?  Here is what I have written so far:

/*6/12/06, POS370 Susan Johnson Individual assignment 2 of 3: Find 5 foreign currencies and display conversion in us dollars*/
/*Offer the user a choice of which foreign currency and how much to exchange into USD*/

#include <stdio.h>

main ()
{
float peso = 0.0885426,yen = 0.00895015,pound = 1.8775,can = 0.910415,ruble = 0.0374186;
float nbr;
float total = 0.0;
int choice;

      printf("\nForiegn Currency Conversion to US Dollars\n");
      printf("\n1: 1 Mexican peso = %1.8f USD", peso);
      printf("\n2: 1 Japanese yen = %1.8f USD", yen);
      printf("\n3: 1 British pound = %1.4f USD",pound);
      printf("\n4: 1 Canadian dollar = %1.6f USD",can);
      printf("\n5: 1 Russian ruble = %1.7f USD",ruble);
      
      printf( "\n\nChoose a foregn currency using 1 - 5: ");
      scanf ( "%d", &choice);
      
      printf( "\n\nEnter the amount to convert to USD: ");
         scanf( "%f", &nbr);
   
            switch (choice)  
  {/*begin switch/case*/      
  case 1:
          
           total = peso*nbr;
           printf("\n%.2f pesos = $%.2f USD\n", nbr,total);
           break;
   case 2:
            total = yen*nbr;
            printf("\n%.2f yen = $%.2f USD", nbr,total);
            break;
   case 3:
            total = pound*nbr;
            printf("\n%.2f pounds = $%.2f USD\n", nbr,total);
            break;
   case 4:
            total = can*nbr;
            printf("\n%.2f canadian dollars = $%.2f USD\n", nbr,total);
            break;
   case 5:
            total = ruble*nbr;
            printf("%.2f ruble = $%.2f USD\n", nbr,total);
            break;
        
   default:
           printf("You did not make a valid selection");
}/* end switch/case*/
getchar();
}/*end main program*/
ASKER CERTIFIED SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, I'm thinking on it and will work on the exit!...
yen was missing the \n!
I'm so exicted I can harldy stand it!  This is what I have for the exit.  It includes asking if you would like to try again and gives you the option to exit.  The only thing still is that the case default printf statement comes after the second user entry prompt instead of after the first entry prompt.  It just doesn't flow right.  I've tried to do a if statement in the default itself but I can't make it work...I wrote something like:

default:
                   if (choice <1 || > 5) /* I don't know how to make this work...*/
                   {
                  printf("\nYou did not choose a valid currency 1 -5:\n");
                    }
So I went back to what I have as you see below.  Thanks Paul for helping me.  If you have any suggestions I am open.  I've learned sooo much!  I never thought I would make it through the day.  This morning I was way low thinking I'd have to drop the class (don't worry my kids and pets are still alive) but tonight I feel like there's a light at the end.    I'm just not quite sure how to get there...anyway, here is what I ended up with.  I'm going to start working on my second individual assignment which is something about averaging GPAs.  I'm sure you've probably seen that one on this site as well!
Here is my Currency Conversion Program:

/*5/31/06, POS370 Susan Johnson Individual assignment 2 of 3: Find 5 foreign currencies and display conversion in us dollars*/
/*Offer the user a choice of which foreign currency and how much to exchange into USD*/

#include <stdio.h>

main ()
{
float peso = 0.0885426,yen = 0.00895015,pound = 1.8775,can = 0.910415,ruble = 0.0374186;
float nbr;
float total = 0.0;
int choice;
int prompt;
int x = 0;

      printf("\nForiegn Currency Conversion to US Dollars\n");
      printf("\n1: 1 Mexican peso = %1.8f USD", peso);
      printf("\n2: 1 Japanese yen = %1.8f USD", yen);
      printf("\n3: 1 British pound = %1.4f USD",pound);
      printf("\n4: 1 Canadian dollar = %1.6f USD",can);
      printf("\n5: 1 Russian ruble = %1.7f USD",ruble);

while (x == 0)
{/*begin while loop*/
      printf( "\n\nChoose a foregn currency using 1 - 5: ");
      scanf ( "%d", &choice);

      printf( "\n\nEnter the amount to convert to USD: ");
         scanf( "%f", &nbr);
      
            switch (choice)  
  {/*begin switch/case*/      
  case 1:
           total = peso*nbr;
           printf("\n\n%.2f pesos = $%.2f USD\n", nbr,total);
           break;
   case 2:
            total = yen*nbr;
            printf("\n\n%.2f yen = $%.2f USD\n", nbr,total);
            break;
   case 3:
            total = pound*nbr;
            printf("\n\n%.2f pounds = $%.2f USD\n", nbr,total);
            break;
   case 4:
            total = can*nbr;
            printf("\n\n%.2f canadian dollars = $%.2f USD\n", nbr,total);
            break;
   case 5:
            total = ruble*nbr;
            printf("\n\n%.2f rubles = $%.2f USD\n", nbr,total);
            break;  
   default:
            printf("\nYou did not choose a valid currency 1 -5:\n");
          
}/* end switch/case*/
printf("\n\nWould you like to try again? 1 for yes, 2 for no: ");
scanf("%d", &prompt);
if (prompt == 2)
       x=1;
       }/*end while loop*/
      printf ("\n\nHave a nice day!");
getchar();
}/*end main program*/
Here we go you wrote:
" I only meant that he is explicit in what he wants us to use.  Fo example, the use of /* is for basic C and the use of // is for another program.  Also, the only thing I'v e learn in class regarding main is main(), not main ( int argc, char * argv[] ).  I don't know what 'argc' is nor have we used char or argv[].  Is there a way to write this program without the use of #define?  and keep main() only?"

With all respect you tutor is around 17 years behind dime. /* is the standard C comment but
// is as fine with C99 (the currently actual standard)

main() was ok to C89 times, but isn't any longer. Of course C lasts and so you can get away with it, but it's still wrong.

You can do without defines but as written before magic numbers are a bad idea so I would go for defines or enums. You case would look like:
case PESO:

... then

The otehr point:
 if (choice <1 || > 5) /* I don't know how to make this work...*/
                   {
                 printf("\nYou did not choose a valid currency 1 -5:\n");
                    }

you can make it work with
if (choide < 1 || choice > 5 ...


Regards
Friedrich
Fredrich, thanks the if / else now works!
This is what I think is the final.  The program loops through the choices as long as they are correct, and asks the user if they want to make another choice.  If not, there is an exit.  If the wrong choice is made, it is pointed out and the user is prompted for the correct choice.  

I did get something back from my instructor tonight that again stated to stay simple and with what we've learned in class, to get rid of the defines!  Someone else is using #define and I can see how it works in that program and where it is useful.  Here is what I have for now!

/*5/31/06, POS370 Susan Johnson Individual assignment 2 of 3: Find 5 foreign currencies and display conversion in us dollars*/
/*Offer the user a choice of which foreign currency and how much to exchange into USD*/

#include <stdio.h>

main ()
{
float peso = 0.0885426,yen = 0.00895015,pound = 1.8775,can = 0.910415,ruble = 0.0374186;
float nbr;
float total = 0.0;
int choice;
int prompt;
int x = 0;

      printf("\nForiegn Currency Conversion to US Dollars\n");
      printf("\n1: 1 Mexican peso = %1.8f USD", peso);
      printf("\n2: 1 Japanese yen = %1.8f USD", yen);
      printf("\n3: 1 British pound = %1.4f USD",pound);
      printf("\n4: 1 Canadian dollar = %1.6f USD",can);
      printf("\n5: 1 Russian ruble = %1.7f USD",ruble);

   while (x == 0)
   {/*begin while loop*/
      printf( "\n\nChoose a foreign currency using 1 - 5: ");
      scanf ( "%d", &choice);
      
      if (choice <= 5 && choice >= 1)
      {
      printf( "\n\nEnter the amount to convert to USD: ");
         scanf( "%f", &nbr);
      
      
      switch (choice)
            {/*begin switch/case*/      
        
              case 1:
                       total = peso*nbr;
                       printf("\n\n%.2f pesos = $%.2f USD\n", nbr,total);
                       break;
               case 2:
                        total = yen*nbr;
                        printf("\n\n%.2f yen = $%.2f USD\n", nbr,total);
                        break;
               case 3:
                        total = pound*nbr;
                        printf("\n\n%.2f pounds = $%.2f USD\n", nbr,total);
                        break;
               case 4:
                        total = can*nbr;
                        printf("\n\n%.2f canadian dollars = $%.2f USD\n", nbr,total);
                        break;
               case 5:
                        total = ruble*nbr;
                        printf("\n\n%.2f rubles = $%.2f USD\n", nbr,total);
                        break;  
               default:
                        printf("\nYou did not choose a valid currency 1 -5:\n");
          
      }/* end if*/
            printf("\n\nWould you like to make another exhange? 1 for yes, 2 for no: ");
            
            scanf("%d", &prompt);
                  if (prompt == 2)
                   x=1;
              
      }/* end switch/case*/
      
      else
             printf("\n\n%d is not a valid choice. Please try again", choice);
             
    }/*end while loop*/
 
             printf("\n\nHave a nice day!");
      
      getchar();

}/*end main program*/
Hi Susan,

You're doing great! :-)

I have two small suggestions, neither are important and this code is fine as it is.

>>    if (choice <= 5 && choice >= 1)
I dont know about you but this sort of messes with my head. I am not sure why but this form always feels so much easier to 'see':
    if (choice >= 1 && choice <= 5)

Secondly:

>>              if (prompt == 2)
>>                x=1;
Now this one will probably cause a 'discussion' between us experts but I always recommend to students to use:
              if (prompt == 2)
              {
                x=1;
              }

There are many reasons for this, most of them subtle so it would be a bit difficult to explain. Lets see what the others say.

Good work! I look forward to 'averaging GPA's' with you. How romantic! :-)

Paul
Well the braces have their arguments, but for checking on equality I often fall back to this pattern

if (2 == prompt)

If you write 2= prompt the compiler will bark at you, so one cause of error less.

Regards
Friedrich
Ok, a small comment from me

> int x = 0;

It would be better to use a longer name (to better understand what the meaning is).

And watch your indentation. It makes the program easier to read for others. But the last version is quiet good (much better than the one before). It is much easier if you get yourself used to it from the beginning.

And something very important, you are missing the EURO as currency! :))
EURO....I know I should have put it first!  I will take your comments and update my program prior to sending in tomorrow.  Paul, I look forward to averaging GPAs as well!  Thanks so much to all.  I will let you know what my score is for this!  You've all helped me through a very hard spot.  Am at work now but will be back with more this evening!  Chat soon, Susan
> I will take your comments and update my program prior to sending in tomorrow.
If you're going to update it, be carefull not to introduce a bug because the changes.
Thank you cwwkie!  Note taken and will be CAREFUL!!!  More tonight!  Have a great afternoon all, Susan
Hello!  It's late here around 11:32 but I've finished my GPA assignment and was wondering if you would take a peek and comment.  The assignment was a little vague and I tried to clarify by asking the instructor if I needed to only allow 20 students (I was thinking with a counter of some sort).

I asked : What do you mean by there are up to 20 students?
> That the loop will only
> allow 20 students or is continuous so that 20
> students can input their GPA
> of 0-4?
Instructor responded: The idea is that you learn to interpret what is being
said. A programmer's life is not just about writing
code, it is about understanding what the client wants
versus what they say.
This is a hint that your program needs to go into a
loop at least 20 times.

With that said, I went with a loop that would continue but asks for a new student or to exit.  See below and tell me what you think...=)  Do you really need the default at the end of the switch/case?

/*6/12/06, POS370, Susan Johnson Individual Assignment
*
*Write GPA program using if statement & switch case, include loop.
*There are up to 20 students in class with GPA of 0-4.
*If GPA is 4, display "Excellent Work".
*If GPA is 3, display "Good Work".
*If GPA is 2, display "Fair Work, Study Harder".
*If GPA is 1, display "Danger Will Robinson!".
*If GPA is 0, display "Were You Listening?".*/

#include <stdio.h>

main()
{

int gpa = 0;
int prompt;
int x = 0;
      
      while (x == 0)
      {/*begin while loop*/
            
      printf("\n******************FACULTY GPA COMMENT PROGRAM****************************\n\n");
      printf("\nWelcome Student! To read faculty feedback, \n");
      printf("\nEnter your GPA from 0 to 4: ");
      scanf("%d", &gpa);
            
            if (gpa >= 0 && gpa <=5)
            {/*begin if statement*/
            
            switch (gpa)
            {/*begin switch/case*/
                  
                  case 0:
                        printf("\n\tYour GPA is %d, WERE YOU LISTENING?!?",gpa);
                        break;
                              
                  case 1:
                        printf("\n\tYour GPA is %d, Danger Will Robinson!",gpa);
                        break;
                        
                  case 2:
                        printf("\n\tYour GPA is %d, Fair work, study harder!", gpa);
                        break;
                        
                  case 3:
                        printf("\n\tYour GPA is %d, Good work!", gpa);
                        break;
                        
                  case 4:
                        printf("\n\tYour GPA is %d, Excellent work!", gpa);
                        break;
                                                
                  default:
                        printf("\n\tYou did not enter a valid GPA from 0-4\n");
                        
                        
      }/*end switch/case*/
            
      printf("\n\nFor new student, select 1, to exit select 2: ");
      scanf("%d", &prompt);
            
      if (prompt == 2)
      {/*begin if statement*/
      x=1;
      }/*end if statement*/
      
      }/*end if statement*/
      
      else
            printf("\n\n%d is not a valid GPA, Please enter your GPA from 0-4", gpa);
            
      }/*end while loop*/
      
            printf("\n\n********Be Sucessful! Do Your Homework and Study Hard!*********");
                  
      getchar();

}/*end main program*/
Could we go one question after the other please. This is obviously a new one. So it does not fit this topic IMHO

Friedrich
Sorry Fredrich, additional question posted.
Now that I've read some background....Paul, I didn't realize who you were....such power....=)
Thanks.
Susan
>>...such power...

Thomas Jefferson:
    I hope our wisdom will grow with our power, and teach us, that the less we use our power the greater it will be.

Woodrow Wilson (1856 - 1924), letter to Mary A. Hulbert, September 21, 1913
    Power consists in one's capacity to link his will with the purpose of others, to lead by reason and a gift of cooperation.

Paul
Well said.  I can see this in the way you answer and treat new learners.
To me, that in itself makes you a wonderful instructor; a natural.
Thank you, Susan
Thank you! :-)

Education is the best viaticum of old age.                           Aristotle

Paul