Inputting a letter and a number

This is a discussion on Inputting a letter and a number within the C++ Programming forums, part of the General Programming Boards category; So I'm an absolute beginner writing my first program. It's a simple one which converts Celsius to Fahrenheit and visa ...

  1. #1
    Registered User Hexadakota's Avatar
    Join Date
    Jan 2008
    Posts
    21

    Inputting a letter and a number

    So I'm an absolute beginner writing my first program. It's a simple one which converts Celsius to Fahrenheit and visa versa. I want the input to be something like 32F, 0C, 212F and a 456G to check for error. The problem is, I don't know how to input a letter AND a number, and I don't know how to send it to the function convert to get it to check for "F" or "C".

    This is what I have so far

    convert

    Code:
    int convert(double n, char a)                  //Creating a function to convert degrees which takes in a number and letter
    {
         
            if (a == 'F' || 'f')                   //Check if fahrenheit
               {
               n = (n - 32)/1.8;                   //Convert to Celsius
               a = 'C';                            //Change "F" to "C"
               return(n, a);                       //Return new number and new letter
               };
            if (a == 'C' || 'c')                   //Check if Celsius
               {
               n = (n * 1.8) + 32;                 //Convert to Fahrenheit
               a = 'F';                            //Change "C" to "F"
               return(n, a);                       //Return new number and new letter
               };
            cout << "That isn't a Celsius or Fahrenheit measure\n\n" << endl;   
            
    
    }
    int main

    Code:
    int main()
    {
        double x;
        char y;
        
    cout << "Please enter in a temperature in Fahrenheit (F) or Celsius (C)\n" << endl;
    cin >> x >> y;
    cout << "\nThat's " << convert(x, y) << " degrees\n\n";
    Help?
    Last edited by Hexadakota; 01-21-2008 at 02:37 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    The input code in main looks fine.

    Your convert function has a few problems, though.

    First, you're not using || properly. You need to have a full expression on both sides of the || (in other words, check if a equals 'F' or a equals 'f', don't check if a equals 'F' or 'f').

    Second, you are attempting to return multiple values from your function, but that's not allowed. Also, you have a cout statement in that function that never gets called. You need to decide whether you want the cout statement to be there, or in main, and if you want it in main you need to find a way to return the information without returning more than one value.

  3. #3
    Registered User Hexadakota's Avatar
    Join Date
    Jan 2008
    Posts
    21
    Quote Originally Posted by Daved View Post
    Second, you are attempting to return multiple values from your function, but that's not allowed.
    Are you talking about...

    Code:
    return(n, a);
    ...

    If so, how do I return both the double and the char back? Like if I inputed 212F, how I would get a 100C from the function returned to main?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are two ways you can do that (that I would recommend to a beginner):
    1. Pass "a" in as a reference, so you can modify it. (char &a).
    2. Return a string of the data instead of two different values.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    You can do it with complex solutions (e.g. with a struct/class or pass-by-reference), but it's probably better to find a way without learning those solutions. I think those (and matsp's suggestion of creating the string) all might be too advanced for you based on the task you're attempting to accomplish.

    One possibility is to not return both pieces of data from the same function. Functions should generally have only one purpose, so perhaps that function should just convert and return the converted value. Then you can have other code (possibly in a separate function) that determines the new character for the degree type.

    A simpler solution would be to output the result in the function and not return anything, but that design wouldn't be quite as good.

  6. #6
    Registered User Hexadakota's Avatar
    Join Date
    Jan 2008
    Posts
    21
    Sweet. All the advice worked. The numbers are being properly converted.

    but now...

    Code:
    int degree(char a)                          //Function to change degree sign which takes in a letter
    {
        if (a == 'F' || a == 'f')
        {
              a = 'C';
              return (a);
              };
        a = 'F';
        return (a);    
    }
    It keeps returning a number! Not an F or a C. Anyone know what it is?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int degree(char a)                          //Function to change degree sign which takes in a letter
    Think the type you return from the function may have something to do with it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User Hexadakota's Avatar
    Join Date
    Jan 2008
    Posts
    21
    Haha. Yeah I'm an idiot.

    Thanks a lot guys. Everything worked.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21