0

I have a program to introduce temperatures, and I need to ask what scale he wants to insert the data, and then receive a list of temperatures. Then I need to convert that list. But what would I put for a stop condition in the while since 0 can be a temperature?

int main(int argc, char const *argv[]) {
    int n = 0;
    int temperaturas[TAMANHO];
    char escala;

    printf("Escala: ");
    scanf("%c", &escala);
    puts("Para PARAR Prima 0");
    while (temperaturas[n - 1] != 0) {
        if (escala == 'C') {
            printf("\n\tCelcius: ");
            scanf("%d", &temperaturas[n]);
        } else
        if (escala == 'K') {
            printf("\n\tKelvin: ");
            scanf("%d", &temperaturas[n]);
        }
        n++;
    }

    escreverTemperaturas(temperaturas, escala, n);

    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    `temperaturas[n - 1]` is invalid when *n* is 0, you go out of the array with an undefined behavior – bruno Dec 28 '20 at 15:43
  • One option: use strings and convert to int (`strtol`) unless it is "quit" or something. – 001 Dec 28 '20 at 15:44
  • Enter something invalid such as `Q` and check the return value from `scanf()` - which you should always be doing anyway. – Weather Vane Dec 28 '20 at 15:56
  • do not forget you have **2** reasons to stop to read the list, the special value enter by the user to signal he wants to end, but also the fact you already read *TAMANHO* temperatures – bruno Dec 28 '20 at 15:59
  • Change scanf("%c", &escala); to scanf(" %c", &escala); – MED LDN Dec 28 '20 at 16:13
  • Read into a temporary buffer, stop on empty line or some non-numeric input, convert to number otherwise. – Arkku Dec 28 '20 at 16:39

2 Answers2

0

From what i understood from your question , you want to the user to stop typing the temperatures whenever he wants , or when the the list is full . Basiclly , you can simply switch your program a bit by asking the user if he wants to continue typing , otherwise when n > TAMANHO .

while ((n<= TAMANHO) || (continue != N))
{
 if (escala == 'C')
 {
  printf("\n\tCelcius: ");
  scanf("%d", &temperaturas[n]);
 }
 else if (escala == 'K')
 {
  printf("\n\tKelvin: ");
  scanf("%d", &temperaturas[n]);
 }
 printf("Would you like continue typing the temparutres ? ");
 scanf(" %c",&continue);
 n++;
}

Tips : You should control the inputs as if he doesn't type C or K it should be another condition as printing a message to inform ( same for &continue); also i advice you to read this previousely asked question about how

scanf("%c", &escala);

should be replaced with

scanf(" %c", &escala);
0

Read this, about scanf: "On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file." http://www.cplusplus.com/reference/cstdio/scanf/

So you can just use the value returned by scanf.

do {
    printf("Escala: ");
    scanf(" %c", &escala);
    
} while( escala != 'C' && escala != 'K');


do {
    escala == 'C' ? printf("\n\tCelcius: ") : printf("\n\tKelvin: ");
    
} while( n < TAMANHO && scanf("%d", &temperaturas[n++]) == 1 );

When there's nothing left to read (or the input is not a number) scanf returns 0 breaking the loop.

anotherOne
  • 1,513
  • 11
  • 20