Thread: need helpon c program

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    need helpon c program

    Hello
    I am trying learn the use of switch statement in c programming. I wrote following program.
    Code:
    #include <stdio.h>
    int main ()
    { 
    int i = 0;
    switch(i)
    {
     case 1:    
       printf("name"); 
       break;
     case 2:
       printf("age");        
       break;
     case 3:
       printf("address");
       break;
    }
     
       return 0;
    }
    Why does it print only "name" I want to print all one by one. should I create loop for (i=0, i<2; i++);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That's not your real program.

    You set i to 0, yet you claim case 1 executes by printing "name".

    Yes, try a for loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Salem View Post
    That's not your real program.

    You set i to 0, yet you claim case 1 executes by printing "name".

    Yes, try a for loop.
    still confused
    when I apply this for (i=1; i<3; i++); it print address
    when I apply this for (i=1; i<2; i++); it print age
    when I apply this for (i=1; i<1; i++); it print name
    why it happen ?
    Code:
    #include <stdio.h>
    int main ()
    { 
    int i;
     for (i=1; i<3; i++);
    switch(i)
    {
     case 1: 
       
       printf("name"); 
       break;
     case 2:
     
       printf("age");        
       break;
     case 3:
      
       printf("address");
       break;
    }
     
       return 0;
    
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the problem now is the ; at the end of the for loop.

    If you put in some braces to surround the whole switch/case, you'll get what you want.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Salem View Post
    Well the problem now is the ; at the end of the for loop.

    If you put in some braces to surround the whole switch/case, you'll get what you want.
    not working. print only address
    Code:
    #include <stdio.h>
    int main ()
    { 
    int i;
     for (i=1; i<3; i++);
    {
    switch(i)
    {
     case 1: 
        
       printf("name"); 
       break;
     case 2:
      
       printf("age");        
       break;
     case 3:
       
       printf("address");
       break;
    }
      
    }
       return 0;
     
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (i=1; i<3; i++);
    What part of remove the trailing ; is confusing you?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by Salem View Post
    > for (i=1; i<3; i++);
    What part of remove the trailing ; is confusing you?
    ok got it
    Code:
    #include <stdio.h>
    int main ()
    { 
    int i;
    
     for (i=0; i<4; i++)
    
    switch(i)
    {
     case task: 
       { 
       printf("name \n"); 
       }
       break;
     case 2:
       {
       printf("age \n"); 
       }       
       break;
     case 3:
       {
       printf("address \n");
       }
       break;
    }
    }
    now I want add enum with switch case
    Code:
    typedef enum
    {
        male,
        female,
        child,
    }database
    I am not sure that I am using enum in correct way
    Code:
     #include<stdio.h>
    typedef enum
    {
        male,
        female,
        child,
    }database;
    int main ()
    { 
    int i=male;
    
     for (i=0; i<4; i++)
    
    switch(i)
    {
     case 1: 
       { 
       printf("name \n"); 
       }
       break;
     case 2:
       {
       printf("age \n"); 
       }       
       break;
     case 3:
       {
       printf("address \n");
       }
       break;
    }
    return 0;
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Enums are just names for integers representing a finite set of things (enumerate)

    So you could do something like
    Code:
    enum fields {
      name,
      age,
      address
    };
    
    // or if you care about the sequence starting at 1
    enum fields {
      name = 1,
      age,
      address
    };
    
    
    // later
    i = name;
    switch(i)
    {
     case name: 
       { 
       printf("name \n"); 
       }
       break;
     case age:
       {
       printf("age \n"); 
       }       
       break;
     case address:
       {
       printf("address \n");
       }
       break;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    May 2017
    Posts
    129
    does it make any sense. is it correct way to use enum with switch case?
    Code:
    #include <stdio.h>
    enum fields {
      name = 1,
      age,
      address
    };
    int main () 
    { 
    // later
     
    int i = name;
     for (i=0; i<4; i++)
    switch(i)
    {
     case name: 
       { 
       printf("name \n"); 
       }
       break;
     case age:
       {
       printf("age \n"); 
       }       
       break;
     case address:
       {
       printf("address \n");
       }
       break;
    }
    return 0;
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you could use the enums themselves in the loop
    Code:
    enum fields i;
    for (i=name; i<=address; i++)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-21-2015, 07:17 AM
  2. Replies: 2
    Last Post: 09-09-2014, 02:36 PM
  3. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM

Tags for this Thread