#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Calling a character in c


    I am trying to call a character into my code so that I can show it as a declaration before it goes into a calculation. I am not sure how to do this. What I am trying to do is make a simple currency converter. Please see below my code I have included as much information as I can.

    #include <stdio.h>
    #include <math.h>
    int main()
    {
    char n1[20];// rand
    char n2[20];// pounds
    char n3[20];// yen
    char Rand = 10.3;

    puts("What currency do you want to convert to US$:\n");
    scanf("%s",n1,n2,n3);
    printf("\n");
    printf("You want to convert %s to US$\n",n1,n2,n3);
    printf("\n");
    printf("%s to US$= ifRand\n"); // calls the rate of exchange.
    return(0);
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    It look like you need to study up on C-strings, you may want to start with these links:

    Character Strings

    More on Character Strings

    Jim
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0
    thanks very much for the direction it makes sense when you have some guidance
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    printf("You want to convert %s to US$\n",n1,n2,n3);
    The format string has only one conversion specifier, "%s", and you pass it three strings. Only n1 gets displayed and n2 and n3 are ignored. If you want to display them as well, then you need to add their conversions to the format string.

    The number of conversions (ie, the % parts) in the format string must match the number of values that you pass in. In this case, the extra values are simply ignored, but when you don't have enough values, then things get really weird as printf starts grabbing the garbage past the end of the argument list.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Uh, excuse me, but "calling a character" makes absolutely no sense whatsoever!

    I realize that not everybody here speaks English as their native language, but English is still the language that we must use here. And discussing C programming requires us to be very precise in the English terminology that we use. We "call a function", but we never "call a character"; I cannot even begin to imagine what that is supposed to mean.

    piczim's problem was in his printf statement. It might even be in his scanf, assuming that the user doesn't know that he must enter three values.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0
    Originally Posted by dreamztechusa
    Please go through the coding, I think it may help you

    #include <stdio.h>

    int main(int argc, char *argv[])
    {
    float inputamount, result;

    printf("\n\n Enter amount to be converted");
    printf("\n in the form 2.22 AU, where the");
    printf("\n where the two letters represent");
    printf("\n a country.");
    printf("\n\n Enter amount to convert: ");

    switch(inputamount)
    {
    case "au":
    result = inputamount * 1.5;
    break;
    case "ro":
    result = input amount * 0.3;
    break;
    /* Add in as many possibilities as you like */
    default:
    result = inputamount * 1.0;
    break;
    }
    printf("\n\n In our currency that is: %f", result);
    getch();
    return 0;
    }

    You may wish to add a "tolower" function above the switch and make all the country codes lower case, or upper, whichever you prefer.
    This looks like exactly what I am looking for, so without a due I copied and pasted and compiled and got a few error reports

    1. 13: error: switch quantity not an integer
    2. 15:error: case label does not reduce to an integer constant
    3.18:error: case label does not reduce to an integer constant
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0
    Originally Posted by dwise1_aol
    Uh, excuse me, but "calling a character" makes absolutely no sense whatsoever!

    I realize that not everybody here speaks English as their native language, but English is still the language that we must use here. And discussing C programming requires us to be very precise in the English terminology that we use. We "call a function", but we never "call a character"; I cannot even begin to imagine what that is supposed to mean.

    piczim's problem was in his printf statement. It might even be in his scanf, assuming that the user doesn't know that he must enter three values.
    I am an absolute beginner been teaching myself for a couple of weeks. please excuse the fact that I don't know all the correct terminology. Its forums like this I believe where I will get assistance and learn?

IMN logo majestic logo threadwatch logo seochat tools logo