Sss
Sss

Reputation: 1529

While loop not working as expected to take various cases as input at terminal)

I am trying to develop my concepts in programming, so i decided to implement queue using linked list.

Probably my logic is correct for both enqueue and dequeue. But the main reason to ask this question here is that:

(1) I want to read the input at terminal like:

Enter 1 to enqueue
Enter 2 to Dequeue
Enter 3 to break

For the first time if user press "1" then it will read the value to be enqueued and then enqueue it.

And then the same question is asked at terminal

Enter 1 to enqueue
Enter 2 to Dequeue
Enter 3 to break

and this way i wanna build linked list which must perform enqueue and dequeue operations and finally can break out when pressed "3".

I have tried to write this procedure of reading the input at terminal in while loop with different if-conditions. and inside those if conditions i call enqueue and dequeue functions. The problem is after reading the first value to enqueue it breaks out of the program, I mean i am not able to have the three options "1","2","3" again to "enqueue", " dequeue" and "break".

The output is as follows (to explain more):
 Enter 1 to Enqueue 
 Enter 2 to Dequeue 
 Enter 3  to Break
1
Enter the element to be enqueued 
34
hp@ubuntu:~/Desktop/Internship_Xav/Huf_pointer$ //Here it breaks it don't ask again for 3 options.

Could anyone please help me to create a linked list which could do enqueue and dequeue and break on entering "3". (I know it's more about while/switch-case, but i am not able to understand how toimplement that).

My full code is here : (I have marked the problem creating part in the code)

#include <stdio.h>
#include <stdlib.h> 
#include <malloc.h> 
#include <string.h>
struct node
 {
    int freq;
    struct node * next;
};
typedef struct node node;
node * rear;
node * front;
// Function declarations
void Enqueue(int val);
void dequeue(void);

// Function definitions 
void dequeue() 
{
    node * temp = front;
    if (front == NULL)
    {
        printf("sorry ,The Linked list is empty\n");
        exit(0);
    }
    front = front -> next;
    free(front);
}
 //Function definitions
    void Enqueue(int val) 
    {
        node * temp = (node * ) malloc(sizeof(node));
        if (front == NULL && rear == NULL)
        {
            front = rear = temp;
            return;
        }
        temp -> freq = val;
        temp -> next = NULL;
        rear -> next = temp;
        rear = temp;
    }

    main()
     {
        int ch, val;
    //Problem creating part is below
            while (ch != 3) 
            {
                printf(" Enter 1 to Enqueue \n Enter 2 to Dequeue \n Enter 3  to Break\n");
                scanf("%d", & ch);
                if (ch == 1) 
                {
                    printf("Enter the element to be enqueued \n");
                    scanf("%d", & val);
                    Enqueue(val);
                }
                if (ch == 2) 
                {
                    printf("Trying to Dequeue\n");
                    dequeue();
                }
                if (ch == 1)
                {
                    break;
                } else 
                {
                    printf("Entered wrong choice, Sorry try again\n");
                    return (-1);
                }
            }
        }

Upvotes: 0

Views: 79

Answers (2)

vonbrand
vonbrand

Reputation: 11831

A while back I wrote a small routine to ask questions, called like menu("E)nqueue D)equeue Q)uit", "edq"), which returns the selected option or EOF if the user terminates input:

#include <stdio.h>
#include <ctype.h>

int menu(const char *prompt, const char *options)
{
    char c, opt;
    const char *p;

    printf("%s: ", prompt);
    for (;;) {
            while (isspace(c = getchar()))
                    ;
            if (c == EOF) {
                    putchar('\n');
                    return EOF;
            }
            opt = tolower(c);
            while ((c = getchar()) != EOF && c != '\n')
                    ;
            for (p = options; *p && opt != tolower(*p); p++)
                    ;
            if (*p)
                    return opt;
            printf("%s: ", options);
    }
}

This little routine has seen use in several of my (modest) programs over the years. The idea is to call it e.g. in a switch().

Upvotes: 1

TerraOrbis
TerraOrbis

Reputation: 91

This part breaks out of the loop.

if(ch == 1)
{
    break;
}

The first if works fine but since you entered 1 it also runs this if breaking out of the while loop. Also to print out "Entered wrong choice..." it needs to be a series of if else or a switch statement. For example:

            if (ch == 1) 
            {
                printf("Enter the element to be enqueued \n");
                scanf("%d", & val);
                Enqueue(val);
            }
            else if (ch == 2) 
            {
                printf("Trying to Dequeue\n");
                dequeue();
            }
            else if(ch == 3)//I assume you meant to put 3
            {
                break;
            }
            else 
            {
                printf("Entered wrong choice, Sorry try again\n");
                return (-1);
            }

Here's how I would implement it with if elses though I think a switch would possibly be better but that's more a matter of style in this case.

while(ch != 3)
{
    printf(" Enter 1 to Enqueue \n Enter 2 to Dequeue \n Enter 3  to Break\n");
    scanf("%d", & ch);

    //If not 1, 2, or 3 print invalid and go through loop again.
    if(!(ch == 1 || ch == 2 || ch == 3))
    {
        printf("Invalid!!");
    }
    else if(ch == 1)
    {
        printf("Enter the element to be enqueued \n");
        scanf("%d", & val);
        Enqueue(val);
    }
    else if(ch == 2)
    {
        printf("Trying to Dequeue\n");
        dequeue();
    }
    //no need to check for 3 since it will end the loop
}

Upvotes: 2

Related Questions