Program always returns the same result in C

Question:

The code runs normal but only gives one exit: Sunday ! regardless of the number chosen.

what the program should do

Implement a program that has as input a number ( 1-7 ) that corresponds to one of the days of the week and prints the name of the corresponding day on the screen ( Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday ).

If the number read is not in the range 1-7 , print: Invalid day number .

The program must remain running until the user enters the number 0 . It is mandatory to use a test at the beginning.

Code

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

void main(void){
    char ch;
    char domingo = '1',segunda = '2',terca = '3',quarta = '4', quinta = '5',sexta = '6',sabado = '7';
    printf("digite um numero que corresponde a um dia da semana: n");
    ch = getchar();
    while(ch!= 0){
        if (ch=1){
            printf(" domingo n");
            break;
        }
        else if (ch=2){
            printf(" segunda n");
            break;
        }
        else if (ch=3){
            printf(" terca  n");
            break;
        }
        else if (ch=4){
            printf(" quarta n");
            break;
        }
        else if (ch=5){
            printf(" quinta n");
            break;
        }
        else if (ch=6){
            printf(" sexta  n");
            break;
        }
        else if (ch=7){
            printf(" sabado n");
            break;
        }
        else if (ch>7 && ch!=0){
            printf(" numero de dia nao valido n");
            break;
        }
    }
}

Answer:

There are three problems with your code:

  1. You are using the assignment operator ( = ), not the comparison operator ( == ). The condition of the while and the last if are ok, since you use different ( != ) and greater ( > ), the others must be adjusted to use this operator.

     if ( ch == 1 ) {

    By the way, when you do if ( ch = 1 ) what happens is the following: first it sets ch to 1 ; then the entire expression evaluates to 1 , and that value is used as the condition of the if . Since 1 is considered "true" (only 0 is false), then it enters the if and prints "Sunday".

  2. You're reading characters ( char ) and comparing them to integers ( int ), and not only is this incorrect in this case, it will fail silently – without showing any error messages – because C automatically converts char to int when both are used in the same operation.

    A character read from the keyboard will be encoded using your terminal's default encoding . Probably Cp1252 , if it's a Windows system in Portuguese, or UTF-8 (Unicode) if it's a Linux system. In both cases, the code for the numbers 0 to 7 is identical to their ASCII code, that is, the character '0' will be considered equal to the integer 48 ( 30 in hexadecimal), the character '1' to 49 , etc. .

    At the start of your program you defined constants ( domingo , segunda , etc) to store each day's code. Why not use them then instead of repeating the number in the code?

     if ( ch == domingo ) {

    And when you don't have a code, always compare character to character, it's more guaranteed:

     else if (ch>'7' && ch!='0'){

    (although in the latter case, it would be better to just do an else – no if – as it may also happen that the character is less than '0' : for example '+' , '-' , '!' …)

  3. You only read a character from the keyboard once, after that enter the while loop. In this loop you test the character, do something, then use break . This makes you fall out of the loop, which is not quite what you want (unless the chosen option is 0 ). You need to strip these break s, but first add a line at the end of the while to read a new character from the keyboard (if you don't, the value of ch will never change and your program will go into an infinite loop ).

     while ( ch != '0' ) { if ( ch == domingo ) { printf(" domingo \n"); // break; } else if ( ch == segunda ) { printf(" segunda \n"); // break; } ... else { printf(" numero de dia nao valido \n"); // break; } ch = getchar(); // Lê outro caractere do teclado }
Scroll to Top