0

I'm not really sure how to execute this...

double fahrenheit;

fahrenheit = 0;

while (fahrenheit != "q")

I'm going to use a scanf to get the value of fahrenheit. fahrenheit is a double and q is a character so obviously it isn't really liking it... Can someone please tell me how to make this one work?


Thank you guys for trying and explaining it to me but I'm really in the early stages of learning C or just programming in general. It's a simple exercise in our book and I haven't really learned all those stuff yet. Basically, if they input q, nothing happens. If they input anything else but q, something happens. Is there like a REALLY SIMPLE like 1+1=2 kind of simple formula here for me to execute this?

bhroask
  • 27
  • 1
  • 8

3 Answers3

2

scanf() shown below returns 0 when you pass character so when you enter q while loop will terminate

double fahrenheit;
while(scanf("%lf",&fahrenheit) == 1)
{
 // Do your stuff
}

Else

  1. Read the input into char array using fgets()
  2. Use strcmp() to check whether the input is q
  3. If the input is not q use strtod() to convert string to double and assign that value to your variable.
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Gopi
  • 19,784
  • 4
  • 24
  • 36
1

A better way to do it is to read a line of text, and parse it to see if it's the q letter or if it can be converted to a number, something like this

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    char line[100];

    printf("Enter Temperatur (F) > ");
    if (fgets(line, sizeof(line), stdin) != NULL)
    {
        size_t length;
        double fahrenheit;
        char *endptr;
        /* get the length of the input string */
        length = strlen(line);
        if (length == 0)
            return -1;
        if (line[length - 1] == '\n') /* remove the '\n' possibly read by fgets */
            line[--length] = '\0';
        /* check if the input string equals "q" */
        if (strcmp(line, "q") == 0)
            return 0;
        /* convert the input string to a double */
        fahrenheit = strtod(line, &endptr);
        /* check if the conversion was successful */
        if (*endptr != '\0')
        {
            printf("invalid input\n");
            return -1;
        }
        printf("value = %f\n", fahrenheit);
    }
    return 0;
}

you can retry on invalid input and process other commands too apart from "q" if you need to, this gives you more control on the input.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 1
    http://stackoverflow.com/questions/27491005/how-can-i-get-a-string-from-input-without-including-a-newline-using-fgets – Gopi Jan 22 '15 at 13:41
  • @Gopi Sure you can write an `fgets()` imitation that skips the `'\n'` but using the existing one and removing it afterwards is I think, a simple solution. – Iharob Al Asimi Jan 22 '15 at 13:43
  • @Gopi sure, I can't think if that's possible now because I am busy working, but you are probably right, although I think in such case, `fgets()` would return `NULL` which i checked. – Iharob Al Asimi Jan 22 '15 at 13:49
0

For a simple solution with minimal error checking.

Read user input as a string.
Test if q was entered.
Otherwise convert to double.

double fahrenheit;
while (1) { // repeat forever
  char buffer[100];
  fputs("Enter temperature in Fahrenheit:", stdout);
  if (fgets(buffer, sizeof buffer, stdin) == NULL) break;  // exit on IO error or EOF
  if (buffer[0] == 'q') break; 
  if (sscanf(buffer, "%lf", &fahrenheit) == 1) {  // was scan successful?
    printf("Success: %f\n", fahrenheit);
  } else {
    printf("Failed: '%s'\n", buffer);
  }
}

What is missing for a robust solution:
1) Distinguish between leaving the loop due to q or EOF.
2) Will quit loop on "q", "quit", "qxyz", but not quit on "Q", " q".
3) Will succeed on input like "123 C".
4) No degree range check like failing for "-1000".
5) Does not complain about crazy input > 100 chars.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256