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;
}
}
}
0
(1
...) and'0'
('1'
...) are different– pmg
I advise you to use the switch case instead of this amount of
ifs
andelses
just do, case 1,case2 etc... and default– Gabriel Rodrigues