Like Tree1Likes
  • 1 Post By Salem

Scanf pit of doom

This is a discussion on Scanf pit of doom within the C Programming forums, part of the General Programming Boards category; Hi, I have included ample explanatory comments in my code, so I think it will be most efficient just to ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    25

    Scanf pit of doom

    Hi, I have included ample explanatory comments in my code, so I think it will be most efficient just to post it to you:

    Code:
    #include "Queue.h"
    
    int main(void)
    {
        QueueT Queu;
        QueueT *pQS;
        pQS = &Queu;
        initQueue(pQS);
        char insChr;
        int menuSelection = 0;
        int *ptrMS = &menuSelection;
    //main menu
        printf("\n\nProgram has been initialized.\n");
        printf("Size:%d\nHead:%d\nTail:%d\n",pQS->size,pQS->head,pQS->tail);
    while (menuSelection != 7)
    {
        printf("\n\nMenu:\n1:insertQueue\n2:removeQueue\n3:peekQueue\n4:isEmptyQueue\n");
        printf("5:isFullQueue\n6:displayQueue\n7:Exit program\n");
        printf("Please enter your selection followed by the enter key.\n");
    
        scanf("%d",&menuSelection);
        //ATTN: coding help section comment
        //There is a DEATH PIT for scanf character statements here. I simply cannot get any to
        //function here or anywhere beyond here in this while statement.
        //Let me be clear that any reasonable place before the above 
        //scanf("%d",&menuSelection); statement is working fantastic but the minute I
        //go past this line, it no longer works.
    
        switch(menuSelection)
        {
            case 1:
    //ATTN: coding help section comment
    //The below two statements don't work at all. In the program, it just skips this completely.
                printf("Please enter the character to be added to the queue:\n");
                scanf("%c",&insChr);
                //debugging printf below to test for performance;fails, also skipped in output.
                printf("%c",insChr);
                
                insertQueue(pQS,insChr);
                //If I try to go ahead and do these operations in the inserQueue function
                //instead, it continues to fail miserably.
                //I can declare the variables inside of the function, but
                //any time I try to access any part of "insChr", the operation immediately
                //resumes failing.
                //I would declare them inside of the function and say crisis
                //averted but this is for a project.
                break;
            case 3:
                 printf("The next item in the queue is:\n");
                     peekQueue(pQS);
                 break;
            case 4:
                 printf("1 indicates empty, 0 indicates that the queue is not empty\n%d\n",isEmptyQueue(pQS));
                 break;
            case 5:
                 printf("1 indicates full, 0 indicates the queue is not full\n%d\n",isFullQueue(pQS));
                 break;
            case 6:
                 printf("The queue contains the following items:\n");
                     displayQueue(pQS);
                break;
    
        }
                    system("Pause");
    }
        
        //Tests the size, head and tail to make sure Circular Buffer was initialized
        //properly.
    
        
    
        return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    > scanf("%d",&menuSelection);
    Will leave a \n

    > scanf("%c",&insChr);
    Always takes the next character. %c does NOT do the normal skip leading whitespace that all the other conversion formats do.

    If you want to read the next non-whitespace character, then try
    scanf(" %c",&insChr); // note the initial space
    Derek Lake likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    25
    Thank you for your time Salem! This worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doom 3 has gone gold
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 43
    Last Post: 08-11-2004, 12:20 AM
  2. doom
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-17-2002, 07:58 PM
  3. Doom 3
    By GaPe in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-09-2002, 08:00 AM
  4. Doom
    By Xterria in forum C++ Programming
    Replies: 19
    Last Post: 09-29-2001, 05:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21