what has mistaken with the switch command in C? [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Here i just experienced with the switch command and if command at C. I have omitted the if command by comment and wrote the same command by using switch command. But its now working as i gave the instruction. Where i have mistaken? As i am a learner please pardon my mistakes..
#include <stdio.h>
#include<stdlib.h>
int main()
{
char card_name[3];
puts("enter the card name: ");
scanf("%2s", card_name);
/*int val=0;
if (card_name[0]=='K') {
val=10;
} else if (card_name[0]=='Q'){
val=10;
} else if (card_name[0]=='J'){
val=10;
} else if (card_name[0]=='A'){
val=11;
}else{
val=atoi(card_name);
}*/
int val=0;
switch (card_name[0]) {
case 'K':
case 'Q':
case 'J':
val=10;
break;
case 'A':
val=11;
default:
val=atoi(card_name);
break;
}
if (val>2 && val<7) {
puts("the count has gone up!");
} else if(val>=10){
puts("The count has gone down");
}
return 0;
}

You're missing another break in the 'A' case. Your switch statement then should look something like this (I added indentation for you).
switch (card_name[0]) {
case 'K':
case 'Q':
case 'J':
val=10;
break;
case 'A':
val=11;
break; // you were missing a break statement here
default:
val=atoi(card_name);
break;
}

There is no break; for case 'A' .. Is that intentional ?

You are missing a break statement in case 'A':,before the default label. You should break after each statement in a case statement unless it was your intent to have the other cases execute, as well as a given case.

Related

Need insight with grading in C using switch, and case tags [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 21 days ago.
Improve this question
printf("On a scale of (A - F) rate your experience with our Consortium's HR Department\n\n");
char Grading;
printf("\nEnter a grade: \n\n");
scanf("%c", &rate);
switch (Grading) {
case 'A':
printf("Perfect!\n\n");
break;
case 'B':
printf("You did good!\n\n");
break;
case 'C':
printf("You did okay\n\n");
break;
case 'D':
printf("At least not bad\n\n");
break;
case 'E':
printf("Bad\n\n");
break;
case 'F':
printf("Awful\n\n");
break;
default:
printf("Please enter only valid grades\n\n");
}
return 0;
}
This is just the section i need help with in my codeblocks. Doing this separately on a different visual studio tab works, but together with my previous codes;it keeps saying enter only valid grades
I tried making a grading on C. My code actually works, i mean that exact grading code block works on a different tab. But when added to my previous code, the project ima working on, it keeps saying enter only valid grades. Ima kinda asking if there's a finna conventional method of adding the C grading block to a project in C
In your switch statement you are checking the value of Grading, but scanf writes its output in rate.
You should change your switch case like so:
printf("On a scale of (A - F) rate your experience with our Consortium's HR Department\n\n");
char rate; //rename Grading to rate
printf("\nEnter a grade: \n\n");
scanf("%c", &rate);
switch (rate) { //rename Grading to rate
case 'A':
printf("Perfect!\n\n");
break;
case 'B':
printf("You did good!\n\n");
break;
case 'C':
printf("You did okay\n\n");
break;
case 'D':
printf("At least not bad\n\n");
break;
case 'E':
printf("Bad\n\n");
break;
case 'F':
printf("Awful\n\n");
break;
default:
printf("Please enter only valid grades\n\n");
}
return 0;
}
Use &Grading instead of &rate
Or
Declare char rate instead of Grading
You have declared Grading and scanf uses rate

How to compare enum values in switch-case? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
Why does the default case fire for the following:
typedef enum {VALUE_ONE, VALUE_TWO} someValue;
typedef struct {
someValue value;
} myStruct;
---main() BELOW---
myStruct* myPtr = malloc(sizeof(myStruct));
myPtr->value = VALUE_ONE;
switch (myPtr->value) {
case VALUE_ONE:
...;
case VALUE_TWO:
...;
default:
...;
}
If I use if-statements, the code will work properly. Currently, I would like to use the switch-case to print out value specific phrases.
UPDATE: Forgot break statement. Thanks everyone!
Use break statement in switch case so that when a break statement is reached, the switch terminates and the flow of control jumps to the next line following the switch statement.
switch (myPtr->value) {
case VALUE_ONE:
...;
break;
case VALUE_TWO:
...;
break;
default:
...;
}
While using the switch case statement, you have to do something like this to get the correct result
switch ( <variable> ) {
case 1:
Code to execute if <variable> == this-value
break;
case 2:
Code to execute if <variable> == that-value
break;
...
default:
Code to execute if <variable> does not equal the value following any of the cases
break;
}
suppose you missed the break statement of case 2, so whenever the switch-case 2 will be true, it will enter in all the other cases(which comes after case 2) utill the code encounters a break statement.
Break statement is used to come out the case after performing the desired action.
Hope so it's clear.

break statement doesn't work in a loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I saw a tutorial that explaining how to use the "break" statement in a loop
but every time i'm trying to use it i'm getting a compilation error saying:
"break statement not within loop or switch
break;"
This is my code:
if (finalFirstChar > 6 || finalFirstChar < 1)
{
printf("You didn't entered a proper number! \n");
break;
}
FWIW, if is a condition (selection statement, to be exact), and break works in a loop (iteration statement)/switch-case statement.
As per C11, chapter §6.8.6.3
A break statement shall appear only in or as a switch body or loop body.
In any case, you don't need a break in a if statement body.
OK:
while (cond1) {
if (cond2) break; /* or continue; */
if (cond3) return [something];
}
for (init; cond4; after) {
if (cond5) break; /* or continue; */
if (cond6) return [something];
}
do {
if (cond7) break; /* or continue; */
if (cond8) return [something];
} while (cond9);
if (cond10) return [something];
NOT OK:
if (cond11) { /* not in any while/do/for loop, or case switch */
break; /* or continue; */
}

Case/Break and '{' in my function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm working on my latest new function. Any explanations would be great since I'm new to programming. I couldn't figure out how to fix this because the errors I got are:
18 syntax error before '{' token
20 case label not within a switch statement
21 (same as above)
22 (same as above)
23 `default' label not within a switch statement
29 [Warning] `return' with a value, in function returning void
32 [Warning] assignment makes pointer from integer without a cast
34 [Warning] `return' with a value, in function returning void
void moveCar(char board[], char vehicle, char direction, int distance)
{
int i;
int position, newPosition;
int offset;
for (i = 0; i < size; i++) //the main loop for the vehicles and user's input
{
if(isalpha(vehicle))//vehicles for all letters but 'x'
{
if(board[i] == vehicle)//vehicles on board
{
swtich(direction)
{
case 'r': offset = 1; break;
case 'l': offset = -1; break;
case 'u': offset = -8; break;
case 'd': offset = 8; break;
default: printf("invalid direction"); break;
newPosition = position + offset;
if(newPosition != '.')
{
printf("invalid move.");
return 0;
}
board = '.';
board[newPosition] = vehicle;
return 1;
}
}
}
}
It looks like you have a typo of Switch as Swtich, the compiler won't recognize it, and the case statements will fail to compile.
Additionally you close your switch statement too early. If you want those commands to execute only in default move them before the break. If you want them to execute for every case move them outside of the switch statement entirely

About switch case in C, why it does not print out? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Code before the first ‘case’ in a switch-statement
I have the following set of code in C:
void isFindValue(int value1, int value2)
{
switch (value1)
{
case 1:
printf("value1 is found!\n");
break;
case 2:
printf("value1 is found!\n");
break;
case 3:
switch(value2)
{
printf("aaaaaaaaaaaaa\n");
case 6:
printf("bbbbbbbbbbbb\n");
printf("value2 is found!\n");
break;
}
default:
break;
}
if I call the function as is isFindValue(3,6); the printf of bbbbbbbbbbbb show up, but aaaaaaaaaaaaa does not, why this will happen? should we not do something before the case?
Because switch () works using labels. It jumps to the label of which the condition is satisfied. So when reaching
switch(value2) {
printf("aaaaaaaaaaaaa\n");
case 6:
the control flow immediately jumps to the label case 6: so that it skips the call to printf().
The solution would be placing it correctly, outside of the inner switch:
case 3:
printf("aaaaa\n");
switch (value2) {
etc.
"aaaaaaaaaaaaa" is not showing up because it is misplaced (it's not contained in a label so it is unreachable). You need to move it up to before the second switch statement:
case 3:
printf("aaaaaaaaaaaaa\n");
switch(value2)
{
// body
}
The call to the printf function in the statement :
printf("aaaaaaaaaaaaa\n");
cannot be reached because it is before the first case statement of the second switch.
see Code before the first 'case' in a switch-statement
"aaaaaaaaaaaaa\n" is not being printed within any case. Your code is not properly structured.
Try moving the statement out of the switch block like this:
printf("aaaaaaaaaaaaa\n");
switch(value2)
{
case 6:
printf("bbbbbbbbbbbb\n");
printf("value2 is found!\n");
break;
}
break;
switch always look for case and that's why aaaaaaaaaaaa was skipped. It will execute the statement of related case block.

Resources