Thread: new to c ...please help

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    12

    new to c ...please help

    hello
    first my code

    Code:
    void delayMessage(){
         char delay;
         
         printf("\n Please press enter to return to the main menu\n");
         
         scanf("%c",delay);
         }
    ok so i want this function to take in enter and that is all.........i just do not know how to do it

    its just a delay message

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your code looks about OK to me - what is not working with it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Code:
    void viewBooks(){
     printf("Displaying all books...\n");
     for(int i = 0; i< count;i++){
         
        // printf("%d. Bookname %s \n Book Serial %ld\n",i+1,books[i].title,&books[i].serial);
      printf("Book name : %s \n",books[i].title );
      printf("Book Serial: %ld \n",books[i].serial);
      printf("Authors name : %s \n",books[i].author );
      printf("Book year %d\n",books[i].yearOfPublication);
      }
      delayMessage();
         }

    this is where i call the function


    and this is my main method

    Code:
    int main(){
        char delay;
        int numSelected,i;
        
        
    
     do{
      runMainMenu();
      scanf("%d",&numSelected);
     switch(numSelected){
      
     case 1: addBook(); break;
      case 2: deleteBook(); break;
      case 3: viewBooks(); break;
      case 4: viewABook(); break;
      case 5: printf(" "); break;
    
      }
     }while(numSelected != 5);
    
    
        
        return 0;
    }

    when i select the number 3 it shows the books and the mainmenu comes back.....
    the delay message should come up after showing the boook details

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by matsp View Post
    Your code looks about OK to me - what is not working with it?

    --
    Mats
    I'm shocked. You finally missed something.

    Code:
    scanf("%c",&delay);

  5. #5
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
    void delayMessage(){
         char delay;
         
         printf("\n Please press enter to return to the main menu\n");
         
         scanf("&#37;c",&delay);
    }
    You have to pass a the address of delay to scanf.
    [edit]beat[/edit]
    Don't quote me on that... ...seriously

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    i had that in already....and i just treid it agian...smae thing still happens

    it displays the books and then the message but does not scan

    but right now i realised what was wrong

    and now im happy

    i put a fflush(stdin);

    and now it works fine

    cheers for the brain storming!!!!

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    12
    Code:
    void delayMessage(){
         char delay;
         
         printf("\n Please press enter to return to the main menu\n");
         fflush(stdin);
         scanf("%c",&delay);
         }

  8. #8

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    MacGyver: yes, I guess I missed that one.

    Molleman: I think it's a "dangling newline" that is the problem (aside from what MacGyver points out above). When you use scanf() to read in the menu-choice, there is a "newline" (as a result of the enter key being used) in the input buffer that has not yet been "used up", so when you get to the "delay" function, it just accepts the newline that was left over.

    Whenever you want to use scanf() and then want to wait for a newline, you need to "clear" that from the input buffer before you can wait for another newline. The best way to do that would be to add something like this after your scanf() in the menu [and any other scanf() function call]:
    Code:
       int ch;
       while((ch = getchar()) != '\n' && ch != EOF) ;
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by matsp View Post
    MacGyver: yes, I guess I missed that one.
    Sorry, you're just usually 10 steps ahead of me and at least 2x as fast. Couldn't let this one go by.

Popular pages Recent additions subscribe to a feed