Program always returns the same result in C

Asked

Viewed 269 times

3

The code runs normal but only one way out: sunday! regardless of the number chosen.

What the programme should do

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

If the number read is not in the range 1-7, prints: No valid day number.

The program must remain running until the user enters the number 0. Must 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;
        }
    }
}
  • 1

    0 (1 ...) and '0' ('1' ...) are different

  • 1

    I advise you to use the switch case instead of this amount of ifs and elses just do, case 1,case2 etc... and default

2 answers

7


There are three problems in your code:

  1. You are using the assignment operator (=), and not the comparison operator (==). The condition of while and the last if are ok, because 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 this: first he assigns ch for 1; then the whole expression evaluates in 1, and this value is used as the condition of the if. Like 1 is considered "true" (only 0 is false), then he enters the if and prints "Sunday".

  2. You are reading characters (char) and comparing them with whole numbers (int), and this is not only incorrect in this case but will fail silently - without showing any error message - as C converts automatically char for int when both are used in the same operation.

    A read keyboard character will come encoded using character encoding (encoding) standard of your terminal. Probably Cp1252, if it is a Windows system, or UTF-8 (Unicode) if it is a Linux system. In both cases, the code for the numbers of 0 to 7 is identical to its code ASCII, that is, the character '0' shall be deemed to be equal to the whole 48 (30 hexadecimal), the character '1' at the 49, etc..

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

    if ( ch == domingo ) {
    

    And when there is no code, always compare character with character, it is more guaranteed:

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

    (although in the latter case, it would be better to do only one else - without if - because it may also happen of the character to be minor than '0': for example '+', '-', '!'...)

  3. You only read a keyboard character once, then enter the loop while. In that loop you test the character, do something, then use break. This makes you come out of the loop, which is not quite what you want (unless the chosen option is the 0). It is necessary to remove these breakbut first add a line at the end of the while to read a new keyboard character (if you don’t do this, the value of ch will never change and your program will enter a loop infinite).

    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
    }
    

1

@mgibsonbr has said it all. The code below serves only to highlight the (questionable) 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 also had to change the "conio. h" (which is platform dependent, there is no such thing in the C of my linux)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.