Thread: SWITCH problem

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    12

    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;
    
    
    
    
    printf("please enter your Transaction type\n");
    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) {
                 printf("please enter your Initial deposit?\n");
                 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. #2
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    302
    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
    Last edited by Zeus_; 11-24-2019 at 12:31 PM.

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    12
    Quote Originally Posted by Zeus_ View Post
    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. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,767
    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
    Last edited by Hodor; 11-25-2019 at 03:12 AM.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    302
    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 subscribe to a feed

Similar Threads

  1. problem with switch() help!!
    By hugoguan in forum C Programming
    Replies: 3
    Last Post: 12-12-2010, 06:11 AM
  2. Switch Problem
    By ahhak1989 in forum C Programming
    Replies: 35
    Last Post: 04-25-2009, 08:58 AM
  3. switch problem
    By joshhud in forum C Programming
    Replies: 1
    Last Post: 11-08-2008, 12:35 PM
  4. problem on switch
    By toxicherry in forum C Programming
    Replies: 11
    Last Post: 12-31-2007, 05:17 AM
  5. switch problem
    By kocika73 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 11:56 AM

Tags for this Thread