0

I have a assignment in which I am supposed to take in a user's input in the form of:

double, char('C' for Celsius or 'F' for Fahrenheit)

and convert it into the other temp scale. To do this task, I wrote this program:

The Program

/*
  The purpose of these program is to convert a user's input and use it in a tempurate conversion programs.
  By Paramjot Singh

  Psuedocode:

  import all needed files

  ask for user's input.

  convert user's imput into 2 varibles: a double and a char

  if the char is 'c' convert the double to farinhiet
  if the char is 'c' convert the double to celius

  display the result.
*/

 #include <stdio.h>
 int main( )
 {
    char input[7];
    printf("Welcome to the Tempurate Conversion Enter a number followed by C or F, depending on if you what to convert a Celuis Temp to Farinheit or vice versa.");
    fgets(input, 7, stdin);
    /*if (in == 'C' || in == 'c') commnented out code
    {
        int f = 9 / 5 (inint + 32);
        printf("Input ", in, " Output: " f, " F");
    }
     else (inint == 'F' || inint == 'f')
    {
        int c = 5 / 9 (inint - 32);
        printf("Input ", in, " Output: " c, " C");
    }
     else 
    {
       printf("I told you to enter c or f. Restart the program.");
    } */

     /*to test what was entered*/
     printf(input);
     return 0;
}

My question is what would I do to convert part of the character array to a double.

4
  • 1
    Why not simply scanf("%lf,%c",&inint,&in); ? Commented Aug 9, 2013 at 17:43
  • What format is the input supposed to be? I'm assuming, e.g., "30F" but you didn't say. All of the suggestions so far assume that the input has a comma. Commented Aug 9, 2013 at 17:48
  • @P0W Because one should not use scanf(). Commented Aug 9, 2013 at 23:36
  • Blanket statements like "don't use scanf" without reasons or alternatives are not very useful. Commented Sep 23, 2014 at 5:34

2 Answers 2

1

Per the printf statement:

printf("Welcome to the Tempurate Conversion Enter a number followed by C or F, depending on if you what to convert a Celuis Temp to Farinheit or vice versa.");

It looks like you expect the user to enter (for example): 23.5c

This implies a: scanf("%lf%c", &inDouble, &inChar); statement with NO intervening comma.` Note the change to the variable names.

Also, the tolower(inChar) will simplify some of your if logic.

Hope this helps.

Sign up to request clarification or add additional context in comments.

8 Comments

+1 but you forgot to mention that fgets() and strtod() should be used instead of scanf(). Also, why not inDouble, if we are scanning doubles?
Will edit the variables name. There is nothing wrong with scanf() when used as directed.
@H2CO3 i guess everyone can have an opinion, but it certainly is NOT deprecated and new programmers writing console i/o will always use it. So, we might as well teach them how to use it correctly.
I never said it was deprecated!
@H2CO3: The first one isn't particularly surprising given that scanf was designed for field-separated data (use fgets for lines). %s overrunning your buffers is definitely a problem that is too easy to make, so I'll accept that.
|
0

Scan your input with sscanf:

sscanf(input, "%lf, %c", &inint, &in);

Edit: inint is a bit of a misleading name. You're supposed to read in a double, right?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.