Thread: values not being switched inside the switch case structure

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    3

    values not being switched inside the switch case structure

    Code:
    #include <stdio.h>#include <stdlib.h>
    typedef struct noode
    {
        int data;
        struct noode *next;
    }node;
    void insertf(node **head,int data)
    {
        node* newn=malloc(sizeof(struct noode));
        newn->data=data;
        newn->next=*head;
        *head=newn;
    }
    void insertm(node *prev ,int data)
    {
        node * newn=malloc(sizeof(struct noode));
        if(prev == NULL)
            printf("\nno more space for the data to be inserted");
        newn->data=data;
        newn->next=prev->next;
        prev->next=newn;
    
    
    }
    void insertl(node** head,int data)
    {
         node *last=*head;
        node* newn = malloc(sizeof(struct noode));
        newn->data=data;
        if(*head==NULL)
        {
            *head=newn;
        }
        while(last->next!=NULL)
    
    
            last=last->next;
        last->next=newn;
    }
    void print(node *nod)
    {
        while(nod != NULL)
        {
            printf("%d ",nod->data);
            nod=nod->next;
        }
    }
    int main()
    {
        char choice,data;
        node *head=NULL;
        do
        {
            printf("\n 1 insert element at first");
            printf("\n 2 insert element at middle ");
            printf("\n 3 insert the element at the last");
            printf("\n 4 display");
            printf("\n 5 exit");
            printf("\nenter the choice");
            scanf(" %c",&choice);
            switch(choice)
            {
                case 1: printf("insert element at first");
                        scanf(" %c",&data);
                        insertf(&head,data);
                        break;
    
    
                case 2: printf(" insert element at mid");
                           scanf(" %c",&data);
                           insertm(head->next,data);
                           break;
                case 3: printf("insert the element at last");
                        scanf(" %c" ,&data);
                        insertl(& head,data);
                        break;
                case 4 : print(head);
                           break;
                case 5: break;
                default: printf("not a choice");
                          break;
            }
        }while(choice !=5);
        return 0;
    }
    the values are not being switched inside the switch case structure,it asks for my choice and then always ends up at the default case, no matter what choice you enter . How can I fix this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,913
    Recall that '1' is unlikely to be equal in value to 1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structure in Switch Case
    By willy_907 in forum C Programming
    Replies: 4
    Last Post: 05-06-2015, 11:09 AM
  2. Replies: 6
    Last Post: 05-08-2010, 03:15 PM
  3. Switch case values
    By dac in forum C++ Programming
    Replies: 9
    Last Post: 12-28-2006, 04:58 AM
  4. Need help with switch/case selection structure
    By nickadeemus2002 in forum C++ Programming
    Replies: 13
    Last Post: 06-29-2004, 02:16 PM
  5. switch case/structure...
    By gcn_zelda in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2003, 04:16 PM

Tags for this Thread