c Help with the next step

Write your question here.
I am trying to work out how I take the result from the question asked in the below code and then do a calculation I am a bit stuck. I have compiled this and it seems to work

When you run it asks for a corresponding letter to be entered to know which currency you want to convert to US$. you can enter the letter but from there I am a bit stuck.

What I am trying to do is once you have entered the letter it says so you want to convert to x to US$, at an exchange rate of y.

Then it asks you the question how many x's do you want to convert to US$

The user then enters the amount and the answer is returned

I just need to know which step to learn next I am not asking someone to do it for me.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

int a =2;
int b =4;
int c =6;
int d =8;
char name[20];
  main()
 {
 
 printf("\n Enter the letter1 of the currency you "); 
 printf("\n want to convert into US$");
 printf("\n a = Australian dollars.");
 printf("\n b = British Pounds.");
 printf("\n c = Japenes yen.");
 printf("\n d = Vitnames dollars.");
 printf(" Enter amount to convert: ");
 scanf("%i");


 return 0;
 }
I would guess that next you need a switch statement.

you will first have to save the user response in a variable.

1
2
char money;
cin >> money;


edit

hold on a moment. you mean "c" code? I think I am messing you up.
never mind my post I don't know what I am talking about.
Last edited on
I'd say you first need to get the letter code of the currency as input from the user, and then get the amount.

Before doing that, you need to define two variables, I'd suggest type char for the currency code. and type double or float for the amount.

Note that it is perfectly ok to use a char in a switch-case statement, just remember that a character literal is written using single quotes, for example 'a'.
Last edited on
I have changed my code below and tried to add an if statement, I think my confusion is taking the inserted result and using it in the if statement that's where my confusion is I think. I suppose it would also be good to confirm if the actual code was correct.

#include <stdio.h>

char a;
char b;
char c;
char d;
char name[20];
main()
{

printf("\n Enter the letter from the below list ");
printf("\n of the currency you want to convert into US$");
printf("\n a = Australian dollars.");
printf("\n b = British Pounds.");
printf("\n c = Japenes yen.");
printf("\n d = Vitnames dollars.");
printf(" Enter letter: ");
scanf("%c");
// if("%c"=a){
// printf("you want to convert Australin dollars to US$\n"):
// }


return 0;
}
The scanf() statement is not correct. You have specified that you want to input a character, but haven't supplied any variable to put it in.

The commented out if statement if("%c"=a) is also not correct.
The = operator is used for assigning a value, instead you need the == for comparing two values. "%c" is a string, not a character so it isn't appropriate here.

Something of this style should work:
1
2
3
4
5
6
7
8
9
    char letter;
    
    printf(" Enter letter: ");
    scanf("%c", &letter);
    
    if (letter == 'a')
    {
        printf("you want to convert Australian dollars to US$\n");
    }


Instead of a series of if-else statements, you could use switch-case, sometimes one is better than the other, or it may be a matter of individual preference.

1
2
3
4
5
6
7
8
9
    switch (letter)
    {
        case 'a':
            printf("you want to convert Australian dollars to US$\n");
            break;
            
        default:
            printf("%c is not a valid currency\n", letter);
    }


Note: I did try not to do too much of the job for you, but sometimes the only way to explain clearly is by giving an example. However, given the problems with this code, I think it would help if you study a good book on the subject, or refer to some online tutorials.
Last edited on
Topic archived. No new replies allowed.