Link to home
Create AccountLog in
Avatar of alwright1975
alwright1975

asked on

Codeing problem

This is my assignment:
Expand the ¿Currency Conversion¿ program to allow the use to enter the amount to convert and the program should continue take new inputs until the user decides to exit.  Insert comments in the program to document the program internally. Attach a design flow chart and a version control sheet to the source code of the program.  
Deliverables: C source code and a MS Word document

Currency Conversion
Please select the currency you want to convert
1. Euro
2. UK Pound
3. Japanese Yen
4. Canadian Dollar
5. Australian Dollar
0. Quit

9
Your entry is invalid. Try again.

1

Please enter the amount in Euro.
100


100.00 Euro = 128.26 US Dollar
Please make another selection. Type 0 to quit.

0
Thanks for using the Currency Conversion program.


This is what I have so far:
#include <stdio.h>

int main() {
    int choice;
    double coef, amount, res;
    printf("Currency Conversion\n");
    printf("Please select the currency you want to convert\n\n");
    printf("\t1.) Euro\n");
    printf("\t2.) UK Pound\n");
    printf("\t3.) Japanese Yen\n");
    printf("\t4.) Canadian Dollar\n");
    printf("\t5.) Australian Dollar\n");
    printf("\t0.) Quit\n\n");
   

    printf("Your choice is: ");
    scanf("%d", &choice);
   
    switch (choice) {
           case 1: coef = 1.28;
                 printf("\nPlease enter the amount in Euro:\n"); break;
           case 2: coef = 1.87;
                 printf("\nPlease enter the amount in UK Pounds:\n"); break;
           case 3: coef = 0.01;
                 printf("\nPlease enter the amount in Japanese Yen:\n"); break;
           case 4: coef = 0.82;
                 printf("\nPlease enter the amount in Canadian Dollar:\n"); break;
           case 5: coef = 0.77;
                 printf("\nPlease enter the amount in Australian Dollar:\n"); break;
           case 0:
                 printf("Thanks for using the currency conversion program"); break;
             default:
                   printf("Your entry is invalid. Try Again\n");
    }
   
   
    scanf("\t%lf", &amount);
   
    res = amount * coef;
   
    printf("Amount in American dollars: %6.2Lf.", res);
    printf("please make another selection. Type 0 to quit\n.");
   
   
    getchar();
    getchar();
   
    return 0;
}
How do I get this code to do the following:
1. When chosing any other number give the default responce and then start over again.
2. When the results is printed it is printed with the amount and choice in it like this 100.00 Euro = 128.26 US Dollars.
3. When the results is printed it will start over again and again until the choice is 0. Quit.
4. Does is there anything else that I may be missing or may have changed that is not right?


Avatar of UrosVidojevic
UrosVidojevic
Flag of Serbia image

OK, it is obvious that you need a loop which will prompt the user over and over again after each conversion. So you will probably have to put main segment of current program in that loop.

After reading the choice in each iteration, you will check next:
- If choice is 1-5 you will do the conversion.
- If choice is 0 you will exit the loop.
- If nothing of this isn't true you will go to the next loop iteration without converting.
"2. When the results is printed it is printed with the amount and choice in it like this 100.00 Euro = 128.26 US Dollars"

You can do this same way as you calculated coef.
Define a string (char*) variable curname and in 1st branch assign it value "Euro", in 2nd branch assing it value "UK pounds",...

After that you can use:

printf("%6.2lf %s = %6.2lf US Dollars\n", amount, curname, res);
Avatar of alwright1975
alwright1975

ASKER

How do I find out how to do a loop. I could not find it in my course book.
while(1)
{
     printf("Currency Conversion\n");.....
and after
      scanf("%d", &choice);
write
     if (!choice) break;
then after
  printf("please make another selection. Type 0 to quit\n.");
close this loop.
Here you go:

I've reduced your use of printf - overkill to use printf for simple \n terminated output.  Corrected a long double conversion problem, corrected main() definition, and generally tidied it up - well, I think I have!

For extra marks you might like to think of a more readable and friendly way to have someone alter exchange rates.

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

int main(void)
{
    int choice;
   
    double coef, amount, res;
   
    const char const * p = "\nPlease enter the amount in ";
   
    char again;
   
    do
    {
        puts("Currency Conversion");
        puts("Please select the currency you want to convert\n");
        puts("\t1.) Euro");
        puts("\t2.) UK Pound");
        puts("\t3.) Japanese Yen");
        puts("\t4.) Canadian Dollar");
        puts("\t5.) Australian Dollar");
        puts("\t0.) Quit\n");
       
        printf("Your choice is: ");
       
        scanf("%d", &choice);
       
        switch (choice)
        {
               case 1: coef = 1.28;
                     printf("%s Euro: ", p); break;
                     
               case 2: coef = 1.87;
                    printf("%s UK Pound: ", p); break;
                     
               case 3: coef = 0.01;
                    printf("%s Japanese Yen: ", p); break;
                     
               case 4: coef = 0.82;
                    printf("%s Canadian Dollar: ", p); break;
                     
               case 5: coef = 0.77;
                    printf("%s Australian Dollar: ", p); break;
                     
               case 0:
                    printf("Thanks for using the currency conversion program");
                    exit(0);
                    break;
                     
             default:
                    printf("Your entry is invalid. Try Again\n");
                    break;
        }
       
       
        scanf("\t%lf", &amount);
       
        res = amount * coef;
       
        printf("Amount in American dollars: %6.2lf.", res);
       
        printf("\nConvert another currency? Y/N >");
       
        while(again != '\n' && again != EOF)      // re-use 'a'.
        {
            again = getchar();
        }      
       
        again = toupper(getchar());  
       
    } while(again == 'Y');
       
    return 0;
}
SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Good points Infinity08

Here's a modified version:

However, I've used static to initialize, rather than use the = initializer.

To the OP, you might find this line difficult [but maybe not]

while ((choice < 0 || choice > 5) && printf("\n\nOops - that was a valid choice - try again!\n\n"));

But it would be good if you understood it.


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

int main(void)
{
    static int choice;
   
    static double coef, amount, res;
   
    const char const * p = "\nPlease enter the amount in ";
   
    static char again;
   
    do
    {
        do
        {
            puts("Currency Conversion");
            puts("Please select the currency you want to convert\n");
            puts("\t1.) Euro");
            puts("\t2.) UK Pound");
            puts("\t3.) Japanese Yen");
            puts("\t4.) Canadian Dollar");
            puts("\t5.) Australian Dollar");
            puts("\t0.) Quit\n");
           
            printf("Your choice is: ");
           
            scanf("%d", &choice);

        } while ((choice < 0 || choice > 5) && printf("\n\nOops - that was a valid choice - try again!\n\n"));
       
        switch (choice)
        {
               case 1: coef = 1.28;
                     printf("%s Euro: ", p); break;
                     
               case 2: coef = 1.87;
                    printf("%s UK Pound: ", p); break;
                     
               case 3: coef = 0.01;
                    printf("%s Japanese Yen: ", p); break;
                     
               case 4: coef = 0.82;
                    printf("%s Canadian Dollar: ", p); break;
                     
               case 5: coef = 0.77;
                    printf("%s Australian Dollar: ", p); break;
                     
               case 0:
                    printf("Thanks for using the currency conversion program");
                    exit(0);
                    break;
                     
             default:
                    printf("Your entry is invalid. Try Again\n");
                    break;
        }
       
       
        scanf("\t%lf", &amount);
       
        res = amount * coef;
       
        printf("Amount in American dollars: %6.2lf.", res);
       
        printf("\nConvert another currency? Y/N >");
       
        while(again != '\n' && again != EOF)      // re-use 'a'.
        {
            again = getchar();
        }      
       
        again = toupper(getchar());  
       
    } while(again == 'Y');
       
   
    return 0;
}
peetm,

experts are not allowed to post full code to a homework assignment. You should have recognized that all others only gave help and verbal explanation. It is not only that askers can simply make copy-paste to do their homework thus were "cheating", but that they will not learn to do the required task themselves.


>>>> while ((choice < 0 || choice > 5) && 
>>>>            printf("\n\nOops - that was a valid choice - try again!\n\n"));
>>>> But it would be good if you understood it.

IMO, that is that kind of C programming which should be avoided as it makes a code very badly readable, especially if the text given in the printf was not correct ("that was a *valid* choice").

You easily can make

    while (true)  /* start an infinite loop */
    {
            /* put all output/input statements here */
            ...
            /* check whether the input was ok */
            if (choice >= 0 && choice <= 5)
                break;   /* break the loop if the input is valid */
            printf("\n>>%d<<, invalid choice\n\n", choice);
    }

Regards, Alex
Hi Alex.

>>experts are not allowed to post full code to a homework assignment.

Sorry, I wasn't aware of that rule.

>>IMO, that is that kind of C programming which should be avoided as it makes a code very badly readable, especially if the text given in the printf was not correct ("that was a *valid* choice").

LOL, I missed that :-)

As to whether it's hard to read/understand, well, it's a subjective call - I for one find that kind of thing to be quite readable - but as I say it's a personal thingmy.

Also, I should point out that some compilers will complain about your infinite loop, i.e., they will complain along the lines of 'conditional expression is constant', I think ISO recommend the for(;;) format.
UrosVidojevic, is this what you mean by same as coef?

char curname
 Switch (curname) {
              case 1: curname = Euro; break;
              case 2: curname = UK Pounds; break;
              case 3: curname = Japanese Yen; break;
              case 4: curname = Canadian Dollars; break;
              case 5: curname = Australian dollars; break;
printf("%6.2lf %s = %6.2lf US Dollars\n", amount, curname, res);
>>>> I for one find that kind of thing to be quite readable -
Bjarne Stroustrup, the 'father' of C++, said about such programming 'for that C was beloved and hated for..."

>>>> I think ISO recommend the for(;;) format.
Same thing: while(true) is much clearer and I didn't used a compiler in the last 15 years on various platforms which did complain about.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Sorry, I missed ";" after char* curname

It should be

char* curname;
I wouldn't use a switch for that - just use an array of structs that contains the data at the correct index. A switch has more overhead (jumps, compares etc.), while an array lookup is just one fetch.