there is a very frustrating problem with my queue linked list. the functions all work its just that after enqueue, dequeue or display the menu displays along with "Enter choice : Only one of the above can be your choice ". whats wrong?

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

#define TRUE 1
#define FALSE 0


void enqueue ();
void dequeue();
void list_node();

struct node
{
    char item_name[30];
    char item_code[30];
    float item_price;
    struct node *ptrnext;
};

struct node *front, *rear , *thisptr , *newptr;

void main ()
{
    char ch;
    front=NULL;
    do
    {
        printf("\n\n [----------Queue Menu----------]");
        printf("\nA - Enqueue queue");
        printf("\nB - Dequeue queue");
        printf("\nD - Display queue content");
        printf("\nX - Exit \n");

        printf("\nEnter Choice : ");
        scanf("%c",&ch);
        ch=toupper(ch);
        switch(ch)

        {
            case 'A' : enqueue(); break;
            case 'B' : dequeue();break;
            case 'D' : list_node();break;
            case 'X' : printf("Exit system");break;

            default : printf("\nOnly one of the above can be your choice");
        }
    }while(ch != 'X');
}

void enqueue()
{
    newptr=(struct node *)malloc(sizeof (struct node));

    fflush(stdin);
    printf("\nEnter Item Name :");
    scanf("%[^\n]s",&newptr->item_name);
    fflush(stdin);
    printf("\nEnter Item Code : ");
    scanf("%[^\n]s",&newptr->item_code);
    fflush(stdin);
    printf("\nEnter Item Price : ");
    scanf("%f",&newptr->item_price);

    newptr->ptrnext=NULL;

    if (front==NULL)
    {
        front=newptr;
        rear=newptr;
    }
    else
    {
        rear->ptrnext=newptr;
        rear=newptr;
    }
    list_node();
}

void dequeue()
{
    thisptr = front;
    if (front==NULL)
    {
        printf("\nEmpty list\n\n");
        return;
    }
    else
    {
        front=front->ptrnext;
        free(thisptr);
    }
}

void list_node()
{
    if (front==NULL)
    {
        printf("\nEmpty list\n\n");
        return;
    }

    thisptr=front;
    do
    {

        printf("\n\nItem Name : %s",thisptr->item_name);
        printf("\n\nItem Code : %s",thisptr->item_code);
        printf("\n\nPrice of Item : %.2f",thisptr->item_price);
        printf("\n");
        thisptr=thisptr->ptrnext;
    }while(thisptr != NULL);
}

Try lines 58 and 61 with an extra space in them, like this.

58: scanf(" %[^\n]s",&newptr->item_name);
61: scanf(" %[^\n]s",&newptr->item_code);

Or even better, without the bad argument type,

58: scanf(" %[^\n]s",newptr->item_name);
61: scanf(" %[^\n]s",newptr->item_code);

Edited 2 Years Ago by Moschops

This article has been dead for over six months. Start a new discussion instead.