Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

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?


0
alwright1975
Asked:
alwright1975
  • 4
  • 3
  • 2
  • +3
2 Solutions
 
UrosVidojevicCommented:
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.
0
 
UrosVidojevicCommented:
"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);
0
 
alwright1975Author Commented:
How do I find out how to do a loop. I could not find it in my course book.
0
Independent Software Vendors: 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!

 
abithCommented:
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.
0
 
peetmCommented:
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;
}
0
 
Infinity08Commented:
>> How do I find out how to do a loop. I could not find it in my course book.

Take a look at the loops section in this tutorial :

        http://www.cplusplus.com/doc/tutorial/control.html


>> 4. Does is there anything else that I may be missing or may have changed that is not right?

Your code looks quite ok (except for the two things that UrosVidojevic already pointed out : add a loop and a way to show the correct currency string).

There are a few small things though :

1) It's best to initialize variables the moment you define them :

        int choice = 0;
        double coef = 0.0, amount = 0.0, res = 0.0;


2) You should only ask for a value and show the calculated output in case the choice was valid. So, this :

        scanf("\t%lf", &amount);
   
        res = amount * coef;
   
        printf("Amount in American dollars: %6.2Lf.", res);

    should only happen if the choice is between 1 and 5.
0
 
peetmCommented:
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;
}
0
 
itsmeandnobodyelseCommented:
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
0
 
peetmCommented:
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.
0
 
alwright1975Author Commented:
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);
0
 
itsmeandnobodyelseCommented:
>>>> 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.
0
 
UrosVidojevicCommented:
"UrosVidojevic, is this what you mean by same as coef?"

Yes, but you got few mistakes:

1. char curname; should be char* curname, since you want to use string variable.
2. switch is written with small 's'.

switch (choice) {
...
}

3. curname = Euro should be curname = "Euro", since you want ot use string constant.

So it should look like this:

char* curname
switch (choice) {
           case 1: coef = 1.28; curname = "Euro";
                 printf("\nPlease enter the amount in Euro:\n"); break;
           case 2: coef = 1.87; curname = "UK Pound";
                 printf("\nPlease enter the amount in UK Pounds:\n"); break;
              // ...
}
printf("%6.2lf %s = %6.2lf US Dollars\n", amount, curname, res);
0
 
UrosVidojevicCommented:
Sorry, I missed ";" after char* curname

It should be

char* curname;
0
 
Infinity08Commented:
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.
0

Featured Post

Independent Software Vendors: 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!

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now