How is the calculation in the following program done in switch case? Why is the last case evaluated in this code?
I am new to learn C language and I can't get exact defination.
C code:
#incluse<stdio.h>
int main()
{
int ch ='a'+'b';
switch(ch)
{
case 'a':
case 'b':
printf("you enterd b\n");
case 'A':
printf("a as in ashar\n");
case 'b'+'a':
printf("you enterd a and b\n");
}
getch();
return 0;
}
I think you forgot to use of break in switch case :
switch(ch)
{
case 'a':
// some expressions
break;
case 'b':
printf("you enterd b\n");
break;
case 'A':
printf("a as in ashar\n");
break;
case 'b'+'a':
printf("you enterd a and b\n");
break;
}
The reason is when the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
Please visit this page
In your program int ch = 'a' + 'b' is the same case 'b'+'a':, so the printf("you enterd a and b\n"); expression will be result.
you can Try online
In your code, switch(ch) causes control to jump to the case label whose expression matches the value of ch (if there is one). Since ch has the value 'a'+'b', and 'a'+'b' equals 'b'+'a', control jumps to the case 'b'+'a'.
Incidentally, when one wants each section of code in a switch statement to be separate, one must put a break; statement at the end of each section (or some other jump statement), as in:
switch (ch)
{
case 'a':
case 'b':
printf("You entered a or b.\n");
break;
case 'A':
printf("A as in ashar.\n");
break;
case 'b'+'a':
printf("You entered a and b.\n");
break;
}
Also note that you will not obtain the value 'a'+'b' when a user enters the characters “a“ and “b”. In C, 'a'+'b' is the sum of the numerical code for the character a and the numerical code for the character b. It is not a string concatenation of the two characters.
Switch case works like this, if ch == 'a' then case 1 will get executed else if ch == 'b' case 2 will get executed and so on. After each case break statement is kept to stop the overflow in between cases.
Related
I’m new to C and currently doing a Jack ‘N Poy that requires the project to restart when the user prompt it like by answering y. However, I can’t seem to grasp its pattern. Have tried it on C++ and it worked but not on C. Can anyone help? I want the user to have the ability to play again after finishing the game.
Here is the code:
#include<stdio.h>
int main (){
char firstPlayer, secondPlayer, again;
do{
printf("Jack 'n Poy)\nEnter Player 1 input: ");
scanf("%c ", &firstPlayer);
printf("Enter Player 2 input: ");
scanf("%c", &secondPlayer);
switch (firstPlayer){
case 'x':
switch (secondPlayer){
case 'x':
printf("Draw");
break;
case 's':
printf("Player 2 wins");
break;
case 'p':
printf("Player 1 wins");
}
break;
case 's':
switch (secondPlayer){
case 'x':
printf("Player 1 wins");
break;
case 's':
printf("Draw");
break;
case 'p':
printf("Player 2 wins");
}
break;
case 'p':
switch (secondPlayer){
case 'x':
printf("Player 2 wins");
break;
case 's':
printf("Player 1 wins");
break;
case 'p':
printf("Draw");
}
break;
default:
printf("Invalid input");
}
printf("\nPlay again? (y/n)");
scanf("%c", &again);
}
while (again == 'y');
printf("Thank you for playing");
return 0;
}
Have also tried adding a substitute variable for the y and yes it does loop, but the loop skips the first question.
Your help are very much appreciated. Thank you very much!
You need to change the format to:
scanf(" %c", ...);
in all scanfs. Note the ' ' before %c
Your again is a string. You can use strcmp(expectedString,inputString) as the condition at while. You will need to include the string.h header file to be able to do this.
Alternatively, change again to char type, i.e. char again;
And then use %c in the last scanf, i.e. scanf("%c", &again);. You may face skipped character scenario if spaces or newlines are entered. In that case, refer to this answer for guidance.
while(Game == 0)
{
gotoxy(100,40);
setForeColor(MY_COLOR_WHITE);
printf("A- Jogar uma partida de Anabi\n");
gotoxy(100,42);
printf("B- Carregar partida anterior\n");
gotoxy(100,44);
printf("C- Descri%c%co do jogo\n", 135, 198);
gotoxy(100,46);
printf("D- Sair do jogo\n");
scanf("%c", &choice);
switch(choice)
{
//This part of the code should break my loop
case 'A':
case 'a':
gotoxy(100, 52);
printf("Please enter your name bellow\n");
gotoxy(98, 53);
printf("->");
scanf("%s",NamePlayer);
Game = 1;
break;
case 'B':
case 'b':
// in here I am going to call a function, the function that reads the file previously saved
//also here I am going to know if there is any saved files, and give an output to the user
break;
case 'C':
case 'c':
//This gets me to the description of the game, rules, etc.. and its working fine with the loop
DescricaoDoJogo();
break;
case 'D':
case 'd':
exit(0);
break;
default:
//This default is giving me trouble, should I use and If, else? Because when i choose the option C, and I get back to the menu what is inside the default executes
printf("Wrong choice.. Try again.. A, B, C or D..");
scanf(" %c", &choice);
system("cls");
break;
}
}
This question already has answers here:
abnormal behavior of scanf [duplicate]
(3 answers)
Closed 3 years ago.
I´m trying to make a program that tells you if a letter is a vowel o consonant and then ask the user if he/she wants to use the program again.
Using just the switch statement works totally fine. My problem comes when inserting the do-while loop. On the first try, the program works smoothly, but in the second and following loops, after asking the user if he wants to try again by typing 1, the program "jumps" the part where the user writes its input (scanf("%c", &letter)) and executes the rest of the program considering the previously typed 1 as the input and messing up everything.
I´ve tried looking for the answer on similar questions and videos, but I´m quite new and I just can´t grasp it. I have another program with a similar problem. I will really appreciate your help :)
int main()
{
char letter;
int new_try;
do {
printf("Is you letter a vocal or a consonant?\n");
printf ("\nPlease submit a letter:\n");
scanf("%c", &letter);
switch(letter) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("This letter is a vowel\n");
break;
default : printf("This letter is a consonant\n");
break;
}
printf("Do you wish to try again?\n [1.Yes 2. No]\n");
scanf("%d", &new_try);
}
while(new_try != 2);
return 0;
}
You need to clean the input buffer. Otherwise, as long as there are characters in your input buffer, the program will not wait for new input from user. Here is the solution.
#include <stdio.h>
int main() {
char letter;
int new_try;
do{
printf("Is you letter a vocal or a consonant?\n");
printf ("\nPlease submit a letter:\n");
scanf("%c", &letter);
while ((getchar()) != '\n');
switch(letter) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': printf("This letter is a vowel\n");
break;
default : printf("This letter is a consonant\n");
break;
}
printf("Do you wish to try again?\n [1.Yes 2. No]\n");
scanf("%d", &new_try);
}while(new_try != 2);
return 0;
}
Update
As #David C. Rankin mentioned, using scanf(" %c", &letter); can also addresss your issue. However, it doesn't address a matching failure. In the above case, you also need to validate the input.
How can I make a switch-case statement to not be case sensitive?
Say I made something like this:
#include <stdio.h>
char choice;
int main ()
{
char choice;
printf("Will you choose A,B, or C?\n>");
scanf(" %c", &choice);
switch(choice)
{
case 'A':
printf("The First Letter of the Alphabet");
break;
case 'B':
printf("The Second Letter of the Alphabet");
break;
case 'C':
printf("The Third Letter of the Alphabet");
break;
}
}
It would only respond to capital letters. How do I make it respond to lower case letters?
toupper in <ctype.h> converts a character to uppercase:
#include <stdio.h>
#include <ctype.h>
char choice;
int main ()
{
printf("Will you choose A,B, or C?\n>");
scanf(" %c", &choice);
switch(toupper(choice)) // Changed line
{
case 'A':
printf("The First Letter of the Alphabet");
break;
case 'B':
printf("The Second Letter of the Alphabet");
break;
case 'C':
printf("The Third Letter of the Alphabet");
break;
}
You simply need this :-
switch(choice)
{
case 'A':
case 'a':
printf("The First Letter of the Alphabet");
break;
case 'B':
case 'b':
printf("The Second Letter of the Alphabet");
break;
case 'C':
case 'c':
printf("The Third Letter of the Alphabet");
break;
}
and so on to continue your series.
Actually,what it does is that it bypasses(skims) upto bottom until it finds the first break statement matching the case thereby executing all the cases encountered in between!!!
Before the switch(), add:
choice = toupper(choice);
And if you haven't already got it, #include <ctype.h> to get the prototype.
You can give 2 cases one by one,
switch(choice)
{
case 'A':
case 'a':
printf("The First Letter of the Alphabet");
break;
case 'B':
case 'b':
printf("The Second Letter of the Alphabet");
break;
case 'C':
case 'c':
printf("The Third Letter of the Alphabet");
break;
}
although default part mustn't run,
it runs al case, I thanks for all helps at now, what should i do? This code all runs i writes on screen default case.
do
{
scanf("%c",&choice);
switch(choice)
{
case 'T':
printf("Enter edges of Triangle1\n");
scanf("%d%d",&edge1,&edge2);
Triangle1(edge1,edge2);
break;
case 't':
printf("Enter edges of Triangle1\n");
scanf("%d%d",&edge1,&edge2);
Triangle2(edge1,edge2);
break;
case 'R':
printf("Enter edges of square\n");
scanf("%d%d",&edge1,&edge2);
Rectangle(edge1,edge2);
break;
case 'S':
printf("Enter one edge of square\n");
scanf("%d",&edge);
Square(edge);
break;
case 'C':
printf("Enter radius of circile\n");
scanf("%d",&radius);
Circle(radius);
break;
default:
printf("Wrong input\n");
break;
}//end of switch
printf("\n");
}while(choice!='e');
The newline gets passed as well as input. Try calling "getc(stdin)" after the first scanf() and it should work :)
Check out for example: http://home.datacomm.ch/t_wolf/tw/c/getting_input.html#newline