# Thread: SWITCH problem

1. ## SWITCH problem

so i did a array and enter to him numbers with money and i have only 50 . so when i open 1 and put the number their everything ok but if i want to do that again i cant. i mean to press O again
when i press E i out from the program

help?

Code:
```#include <stdio.h>

int main()
{

char x;
double a[50][2];
int count=0;

scanf(" %c",&x);

do{

switch(x)
{
int j;
double y;
int i;

case 'O':
for(i=0;i<50;i++)
{
for(j=1;j<2;j++)
{
if(a[i][j]==1){
count++;}
}
}

if(count<50)
{
for(i=0;i<50;i++)
{
for(j=1;j<2;j++)
{
if(a[i][j]!=1) {
scanf("%lf",&y);
a[i][j-1]=y;
a[i][j]=1;
printf("you number bank is %d and your balance is %.2lf \n",i+901,a[i][j-1]); }
break;
}
break;
}
}

else{ printf("the bank are full\n");}

break;

case 'E': continue;
break;

default:     printf("wrong enter try again\n");
break;

}

printf("please enter again your Transaction type selection\n");
scanf(" %c",&x);

}
while(x!='E');

printf("thank you bye bye\n");

}```

2. Your printf and scanf statements should be inside your do-while loop.
Remove the continue from case 'E' and let it be just break. Your compiler should warn you about unreachable code as of now.
This will get your O and E to work fine but there are other things worrying me.
- looping through uninitialized memory
- j should probably be 0 and not 1 in your first for loop in case 'O' but I don't know because I haven't looked closely at what you intend to do with it
- use of meaningless variable names (x should be something like TransactionType so that someone else reading your code can understand at first glance what you intend to communicate)
- comparison of double value to integer
- indentation of code
- lack of understanding of flow of control (please have a look at a reliable source to learn more and grasp the concepts)
- practice writing code in functions other than main and using those functions elsewhere (typically when you have repetitive code, functions are really powerful)

Try and fix these issues starting with indentation, descriptive variable names and control flow issues. Good luck

3. Originally Posted by Zeus_
Your printf and scanf statements should be inside your do-while loop.
Remove the continue from case 'E' and let it be just break. Your compiler should warn you about unreachable code as of now.
This will get your O and E to work fine but there are other things worrying me.
- looping through uninitialized memory
- j should probably be 0 and not 1 in your first for loop in case 'O' but I don't know because I haven't looked closely at what you intend to do with it
- use of meaningless variable names (x should be something like TransactionType so that someone else reading your code can understand at first glance what you intend to communicate)
- comparison of double value to integer
- indentation of code
- lack of understanding of flow of control (please have a look at a reliable source to learn more and grasp the concepts)
- practice writing code in functions other than main and using those functions elsewhere (typically when you have repetitive code, functions are really powerful)

Try and fix these issues starting with indentation, descriptive variable names and control flow issues. Good luck
thank you i fix my problem but another question
i know that to switch i have to enter a integer
but i enter a char so how that work ?
thanks

4. Look up section 6.8.4.2p5 of the C Standard (C11)

The integer promotions are performed on the controlling expression. The constant expression in each case label is converted to the promoted type of the controlling expression. If a converted value matches that of the promoted controlling expression, control jumps to the statement following the matched case label. Otherwise, if there is a default label, control jumps to the labeled statement. If no converted case constant expression matches and there is no default label, no part of the switch body is executed.
You'll probably also need to look into integer promotions (google that). Also handy to know is section 6.3 (Conversions) and 6.3.1.8 (of C11) Usual arithmetic conversions. Google "Usual arithmetic conversions".

Edit: If you don't have a copy of the c11 standard, here is a link to the draft (which is 99.9999% the same as the released version): https://port70.net/~nsz/c/c11/n1570.html It will be your best friend

5. Do everything as suggested by @Hodor. That'll essentially help you a lot.

Also, char values are essentially just integer values in disguise (you can think of it that way if it simplifies things for you). So, if you are to write something like "case 'A':", it is the same as writing "case 65:" (but here, something called the Implicit Type Conversion takes place automatically by the compiler which you should be reading more about from a reliable source) as character 'A' is 65 in integer ('B' is 66, 'C' is 67, ...). Have a look at the ASCII Table. When you take in a character input from the user, your character variable can be type-casted into an int to retrieve its ASCII Value.

Popular pages Recent additions