views:

25

answers:

1

I have a program with which dreamlax worked a lot with me on, which uses Objective-C to convert temperatures between the Fahrenheit, Celsius, Kelvin and Rankine temperature scales, but converting console-input into Kelvin, and then from Kelvin to the end-user's desired temperature scale.

Now, I have an idea I would like to implement for the final prompt of the converted temperatures once the calculations are done. It is set up currently to only display like so:

x degrees temperature-scale

Where x = the final converted temperature, and temperature-scale = the scale the user wishes to have his temperature converted to.

Suppose the end-user selected Fahrenheit as his/her source temperature, wishing to convert 212 degrees into his/her target temperature scale of Celsius. The conversions should equal 100 degrees Celsius obviously, but I think the program would be better of displaying the result like this:

212 degrees Fahrenheit is 100 degrees Celsius.

Now, I've made the values that need to be replaced by variables in bold. I have the 212 and 100 values solved easily, because 100 variable has been there in the first place, and 212 can easily be remedied by replacing it with the string formatter of the sourceTemp variable, the variable which contains the users original inputted temperature.

Now, the Fahrenheit string is a bit different.

I have tried to establish something new in the original switch like so:

switch (prompt) 
{
    case 1:
        //end-user chooses Fahrenheit
        [converter setFahrenheitValue:sourceTemp];
        sourceTempText = 1;
        break;

    case 2:
        //end-user chooses Celsius
        [converter setCelsiusValue:sourceTemp];
        sourceTempText = 2;
        break;

    case 3:
        //end-user chooses Kelvin
        [converter setKelvinValue:sourceTemp];
        sourceTempText = 3;
        break;

    case 4:
        //end-user chooses Rankine
        [converter setRankineValue:sourceTemp];
        sourceTempText = 4;
        break;
}

OK, so I have added to each different case, setting a new variable named sourceTempText to either 1-4, the same value that the end-user chose to pick his/her source temperature.

Now, here is how I tried to display the final prompt to the end-user with the final switch:

switch (prompt2) 
{
    case 1:
        //end-user chooses Fahrenheit
        printf("%lf degrees sourceTempText is %lf degrees Fahrenheit\n", sourceTemp, [converter fahrenheitValue]);
        break;

    case 2:
        //end-user chooses Celsius
        printf("%lf degrees sourceTempText is %lf degrees Celsius\n", sourceTemp, [converter celsiusValue]);
        break;

    case 3:
        //end-user chooses Kelvin
        printf("%lf degrees sourceTempText is %lf degrees Kelvin\n", sourceTemp, [converter kelvinValue]);
        break;

    case 4:
        //end-user chooses Rankine
        printf("%lf degrees sourceTempText is %lf degrees Rankine\n", sourceTemp, [converter rankineValue]);
        break;
}

I am not sure now, if I can insert sourceTempText into the string like I have here, instead, maybe I have to use a string formatter, but I'm not sure. It should be an easy fix, I just wanted to throw it out here! :)

P.S. sorry for the kind of messy question formatting, I kind of didn't know how to phrase it so please ask for clarification if needed.

+2  A: 

Replace sourceTempText with %s, and above the switch statement declare the possible strings like so:

char *scales[4] = { "Fahrenheit", "Celsius", "Kelvin", "Rankine" };

Then add and argument to each printf, after the sourceTemp, like so:

printf("%lf degrees %s is %lf degrees Fahrenheit\n",
                   sourceTemp, scales[prompt-1], [converter fahrenheitValue]);

That's prompt-1 because counting starts at zero, and you start at 1.


ps. I see now you set sourceTempText = 1/2/3/4; - I just used prompt, since it has the same value. What you could do, is setting

char *scales[5] = { "Error!", "Fahrenheit", "Celsius", "Kelvin", "Rankine" };

and using scales[sourceTempText], while making sure that before the switch, sourceTempText = 0. That would make a fine example of input sanitizing: whatever the user manages to put in prompt, your program will always display a valid text.

mvds
Wow that worked great! Just a few question though, what exactly is the char *scales[4]? What does that mean, I understand your logic by giving it the possible strings for sure. Also, I'm guessing %s is the string formatter for string?
BOSS
Yes, %s is for string (not NSString, but that's another topic). `char *scales[4] = {}` says: There are 4 pointers of type `char *`. A `char*` pointer is what you would call "string" here; it points to the memory where the string starts, it's a memory address. By saying `scales[0]` you actually say: give me the first ("zero-th") pointer in there. The %s then looks in that location in memory. To experiment, you could try to replace `scales[prompt-1]` with `scales[prompt-1]+1`, which will make the string print from the second character. ("ahrenheit", "elsius", "elvin" and "ankine")
mvds
Great, thanks again! Now, in your update, you mentioned my sourceTempText. Just one thing that I'm confused about, is prompt = to the same things as sourceTempText because of cases 1-4? Now, what should I display if sourceTemptText = 0? How could a user even get that to occur? Oh, I should point out that my code actually does handle incorrect input by setting a result variable = to scanf. If scanf returns 0, then there is an error and if it returns EOF, there is another error. I'm not sure if that is the same thing as what you brought up..
BOSS
I don't know your other code, I just see these lines here. So as far as I can tell, `prompt` can be any number. (you may have that covered, but let's assume that it can be any number for the sake of argument) Putting in "any number" in `scales[anynumber]` is quite a scary thing to do, since the program doesn't actually check if anynumber goes out of bounds, so a "malicious user" could try and let you print out any piece of memory by stuffing the right number in there. If you were building a login system, such a bug could be a disaster! Therefore, it is always a good thing to check input.
mvds
And yes, you effectively say "if prompt is 1, make sourceTempText 1", and the same for 2, 3 and 4. That is maybe just a little bit useless, why not use prompt then to select the text? ;-)
mvds
OK haha, thanks.
BOSS