Problems with while and switch commands and char variables

Question:
The program should receive a sequence of numbers, if it receives the character S should stop and show the smallest number of them all. Thing is it doesn’t get in the loop, it shows the text to input/receive the variable for continuing excuting or stop, but it doesn’t wait for the inputed value and shows “Invalid value” instead. Would it be a problem about how the While and Switch commandas are declared? Sorry for the bad English, translated by myself.

Repl link:

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

int main(void) {
  int number = 0, Highest = 0, Smallest = 0, value = 1;
  char termination;
  printf("Choose the option below:\n");
  printf("1-Enter with a sequence of numbers;\n");
  printf("S-Close program up;\n");
  termination = getchar();
  while (termination != 'S') {
    switch (termination) {
    case '1':

    {
      printf("Input an integer value>0:\n");
      scanf("%i", &number);
      if (number < 0) {
        printf("Invalid Value!\n");
        termination = 'S';
        value = 0;
      } else {
        if (value == 1) {
          Smallest = number;
          Highest = number;
        }
        value = 0;
        if (number > Highest) {
          Highest = number;
        }
        if (number < Smallest) {
          Smallest = number;
        }
        printf("Choose the option below:\n");
        printf("1-Enter with a sequence of numbers;\n");
        printf("S-Close program up;\n");
        termination = getchar();
      }
    }

    break;
    default:
      printf("Invalid value!\n");
      termination = 'S';
    }
  }
  printf("The smallest number is:%i\n", Smallest);
  printf("You ended this program up!");
  return 0;
}

In C, the scanf() function doesn’t consume the newline character when you press enter.
Your newline character that is left over in the input bufer, so what happen is that later, when you call getchar(), it doesn’t wait for user input but instead grabs the newline character that’s still in the buffer.

You can add a simple loop after your scanf() calls to consume the remaining characters in the input buffer. Like this:

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

int main(void) {
    int number = 0, Highest = 0, Smallest = 0;
    char termination;

    printf("Choose the option below:\n");
    printf("1-Enter with a sequence of numbers;\n");
    printf("S-Close program up;\n");
    termination = getchar();

    while(termination != 'S') {
        switch (termination) {
            case '1': {
                printf("Input an integer value > 0:\n");
                scanf("%i", &number);
                while(getchar() != '\n');  // I add the new line here
                
                if(number < 0) {
                    printf("Invalid Value!\n");
                    termination = 'S';
                } else {
                    if(Smallest == 0 && Highest == 0) {
                        Smallest = number;
                        Highest = number;
                    }
                    
                    if(number > Highest) {
                        Highest = number;
                    }
                    
                    if(number < Smallest) {
                        Smallest = number;
                    }
                }
                
                printf("Choose the option below:\n");
                printf("1-Enter with a sequence of numbers;\n");
                printf("S-Close program up;\n");
                termination = getchar();
                break;
            }

            default:
                printf("Invalid value!\n");
                termination = 'S';
        }
    }

    printf("The smallest number is: %i\n", Smallest);
    printf("You ended this program up!");

    return 0;
}

3 Likes

Hello @WindLother, it simply worked. Thank you very much, I expect to study a little more to understand how a simply line could make it.

Bye bye

Joao de Sousa Luz

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.