Inside Switch Case - Variable Definition Related - c

In this code, why I am getting i = "some garbage value" as output? I see that i is being declared but value = 10, not assigned. Why ?
main()
{
int a =1;
switch (a)
{
int b = 10;
case 1: printf ("b = %d \n", b);
break;
}

b is not being initialized. The assignment is outside of any case in the switch, so it picks whatever was in the stack at that point.

If you want a variable inside a case statement, the right way to do it is:
switch(a)
{
case 1:
{
int b=10; //start a new block scope
printf("b=%d",b);
}
break;
}

Related

How to stop a loop and a switch case in C? [duplicate]

This question already has answers here:
How to break out of a loop from inside a switch?
(20 answers)
Closed 2 months ago.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
while(1)
{
scanf("%d", &a);
switch(a)
{
case 0;
break;
}
}
return 0;
}
If I have a loop and a switch inside it, I want to stop both of them. That's because a break just stops the switch.
In the program, there is a loop that asks for a variable and then a switch case where if the variable is 0, the loop and the switch case must stop.
I tried to add a variable for the loop and change it in the case for stopping the loop, but it doesn't work.
There are multiple ways to do this. Three follow.
Use a goto:
while (1)
{
scanf("%d", &a);
switch (a)
{
case 0:
goto AfterWhile;
}
}
AfterWhile:
Build code into the loop:
_Bool KeepGoing = 1;
while (KeepGoing)
{
scanf("%d", &a);
switch (a)
{
case 0:
KeepGoing = 0;
break;
}
}
Encapsulate in a function and use return:
void DoTheLoop(void)
{
while (1)
{
int a;
scanf("%d", &a);
switch (a)
{
case 0:
return;
}
}
}

Switch case basic

Below mentioned program is a basic form of switch case. I'm trying to print the macros variable in the out put. For more details refer the output. Its not printing the macros variable.
Switch case
#include<stdio.h>
#define a 4
#define d 5
int main()
{
int x = 10;
switch (x)
{
case a: printf("Number is 40");
break;
case d: printf("Number is 50");
break;
default: printf("Default case");
break;
}
}
Expect:
Number is 40
Actual:
Default case
Kindly refer the Point 1 & 2.
#include<stdio.h>
#define a 4
#define d 5
int main()
{
int x = 4; //(Point1)Here I'm declared the x value as 4 which is equal to the above defined one of the macros. Hence the output has printed the expected value as said by #ed heal.
switch (x)
{
case a: printf("Number is 40"); //(Point 2)User can be expect anything as dummy output.
break;
case d: printf("Number is 50");
break;
default: printf("Default case");
break;
}
}
You're setting x to 10. You're switching on x, that is 10, and neither a nor b evaluate to 10, so the default case statement is used.

remembering the value of a counter

#include <stdio.h>
int main()
{
int input, counter,value;
int ABC[3];
counter = 0;
scanf("%d", &input);
switch (input)
{
case 1:
if (counter >=4)
{
printf("Error\n");
}
scanf("%d", &value);
ABC[counter]= value;
printf("ABC[%d] is %d \n", counter, ABC[counter]);
counter++;
main();
break;
case 2: //do anything
main();
break;
default:
printf("a is anything\n");
break;
}
return 0;
}
I want to put in array ABC every time i choose case 1 a value, until array ABC is full. My problem is, that i can only enter in this programm values into ABC[0] . Is there any way, to remember the value of counter, so it's not always 0? Maybe using if-statement? But how to formulate an if-statement in this programm, which is only once in the beginning true?
But ABC should also be allowed to have empty space
After "counter++" you are re-calling your "main" function, that reinitializes your counter to 0. You should use a loop:
int counter = 0;
do
{
// Your scanf goes here
switch(scanf_result)
{
// Add your case labels here
default: // Incorrect input? Let's start again!
continue;
};
counter++;
}
while(counter < 3);
i think that declaring counter outside the main function, would make it global scope.
in this way everytime you call main(); you reset the value to 0.
so write int counter = 0 before the main declaration, at row number 2.

Repetition using switch statement inside a for loop

I am trying to use a loop to print out a repetitive song, "this old man"
The first verse is:
This old man, he played one
He played knick-knack on my thumb
This old man came rolling home
This song repeats to ten, varying the two terms in italicize
one -> two++ and thumb -> another item such as shoe, knee, etc.
Here is my code so far:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
string change1 (int i);
int main (void)
{
for (int i = 1; ; 1 < 11; i++)
{
printf ("This old man, he played ");
change1(i);
printf("He played knick-knack on my %s\n\n", s1);
}
return 0;
}
string change1(int i)
{
string s1;
switch(i)
{
case 1:
{
printf("one\n");
s1 = "thumb";
}
break;
case 2:
{
printf("two\n");
s1 = "shoe";
}
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
printf("ill add these cases later");
}
}
This gives me an error message of: "control reaches end of non-void function"
I also got an undeclared variable s1 error but I declared it in the function.
You could simplify your program to an actual C program, rather than C++
int main (void)
{
int i;
char* items[] = {"thumb", "shoe", "", "", "", "", "", "", "", ""};
char* numbers[] = {"one", "two", "three","four","five","six","seven","eight","nine","ten"};
for (i = 0; i < 10; i++)
{
printf ("This old man, he played %s\n", numbers[i]);
printf("He played knick-knack on my %s\n\n", items[i]);
}
return 0
}
In C++ variables have scope. A variable is generally visible inside the curly braces where it is declared; outside these brackets the variable does not exist.
That is why you cannot use s1 from change1 inside the loop: you need to return a value (best choice in your situation), or use a variable that is in scope in both change1 and main.
printf ("This old man, he played ");
printf("He played knick-knack on my %s\n\n", change1(i));
...
string change1 (int i) {
string s1;
switch (i) {
...
}
return s1;
}
Note that you do not need a switch statement to implement change1: when the code is so uniform, you may be better off with an array:
const char *strings[] = {"thumb", "shoe", ...};
change1 needs to return the string it decided on. And main has to assign the return value to a variable, because as originally written s1 is local to the change1 function.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
string change1 (int i);
int main (void)
{
for (int i = 1; ; 1 < 11; i++)
{
printf ("This old man, he played ");
string s1 = change1(i);
printf("He played knick-knack on my %s\n\n", s1);
}
return 0;
}
string change1 (int i)
{
string s1;
switch (i)
{
case 1:
{
printf("one\n");
s1 = "thumb";
}
break;
case 2:
{
printf("two\n");
s1 = "shoe";
}
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
printf("ill add these cases later");
}
return s1;
}
use return statement at the end of switch class
return s1;

Switch: Declared Variable outside switch and use it in it

The following isn't the it's just the part causing problem:
int s,p;
scanf("%d",s);
switch(s)
{
case 1:
{
p=10;
break;
}
case 2:
{
p=15;
break;
}
}
printf("%d",p);
The problem is that p prints a random and very large number, What is causing it?
So i used some of your advice and know i have the following code:
int s,p=0;
scanf("%d",&s);
switch(s)
{
case 1:
{
p=10;
break;
}
case 2:
{
p=15;
break;
}
default:
{
printf("Number invalid");
return 0;
}
}
printf("%d",p);
Now i it's always going to default even though i only enter 1 or 2
Ok now it worked Thank You All!
You have two problems: (i) p is uninitialised and (ii) you're passing s to scanf where the address of s is required.
Change:
int s,p;
scanf("%d",s);
to:
int s, p = 0;
scanf("%d", &s);
"int p" declaration assigns p to an arbitrary value: whatever happened to be in memory. Now, if s doesn't equal 1 or 2, that value never changes, and that's what you see. What you can do is
add default: clause to your switch() and assign p to something meaningful there
declare p as "int p = 0;"
What number are you scanning in? You probably you don't have a switch case for that number, which means that p is uninitialized (and hence random). For example if you enter 3, there is no case statement for 3 and p will contain a random value. I would suggest that you detect invalid input with an default case where you assign a value of 0 to p.
default:
p = 0;
Corrected code:
int s,p;
scanf("%d", &s);
switch(s)
{
case 1:
{
p=10;
break;
}
case 2:
{
p=15;
break;
}
default:
{
p=0;
break;
}
}
printf("%d",p);
Edit: Fixed scanf problem per Paul R
You can use default and then try to figure out what was scanned in

Resources