Program always returns the same result in C

3

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

What the program should do

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

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

The program must remain running until the user enters the number 0 . Please 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;
        }
    }
}
    
asked by anonymous 11.03.2015 / 04:32

2 answers

7

There are three issues in your code:

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

    if ( ch == 1 ) {
    

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

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

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

    At the beginning of your program you have defined constants ( 49 , domingo , etc) to store the code for each day. Why not use them instead of repeating the number in the code?

    if ( ch == domingo ) {
    

    And when you do not have code, always compare character with character, it is more guaranteed:

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

    (although in the latter case, it would be best to only do a segunda - without else - because the character may also be smaller than if : for example '0' '+' , '-' ...)

  • You only read a single character from the keyboard once, after which it enters the '!' loop. In this loop you test the character, do something, then use while . This causes you to exit the loop, which is not quite what you want (unless the option is break ). You must remove these 0 s, but first add a line at the end of break to read a new keyboard character (if you do not, while 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
    }
    
  • 11.03.2015 / 05:04
    1

    @mgibsonbr already said it all. The code below is for pointing out the (arguable) importance of vertical symmetries in code indentation.

    #include <stdio.h>
    #define clearbuff     while(getchar()!='\n');
    
    int main(void){
      int ch;
      printf("digite um numero que corresponde a um dia da semana: \n");
    
      while((ch=getchar()-'0') != 0){
             if (ch==1){ printf(" domingo\n"); }
        else if (ch==2){ printf(" segunda\n"); }
        else if (ch==3){ printf(" terca\n")  ; }
        else if (ch==4){ printf(" quarta\n") ; }   
        else if (ch==5){ printf(" quinta\n") ; }
        else if (ch==6){ printf(" sexta\n")  ; }
        else if (ch==7){ printf(" sabado\n") ; }
        else           { printf(" numero de dia nao valido\n"); }
    
        clearbuff;
      }
      return 0;
    }
    

    I have yet to change the "conio.h" (which is platform dependent, there is no such thing in the C of my linux)

        
    13.03.2015 / 17:49