Thread: Grrrr!

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    Grrrr!

    In the code below what I want to do is ask the user if he wants to play the game or not but the scanf doesn't work can you please tell me whats going wrong
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    
    int main()
    {
    clrscr();
    char name[50], a;
    
    
    printf("\nPROJECT BEASTS\n");
    printf("Welcome to Project Beasts please enter your name:");
    scanf("%s", &name);
    printf("\nOk %s do you want to play the game (y/n):", name);
    scanf("%c", &a);
    printf("%c", a);
    
    return 0;
    }

  2. #2
    no-one
    Guest
    that code works for me, what kind of problems/errors is it giving you, specifically.

  3. #3
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    scanf("%s", &name);

    I do not think you need the & in front of name, I do not know if that is making it not work though, it shouldn't.

    [edit]
    if you are getting a compile error, put clrscr() after you declare name and a.
    [/edit]
    Last edited by JoshG; 08-16-2002 at 07:00 PM.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Try looking up fgets( )
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    The problem is

    When i execute the program it does everything correctly apart from the last scanf. The program just doesn't peform the last scanf.

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Most probably because there's something left in the input buffer.

    After a call to scanf(), or any input function for that matter, you should flush the input stream, iow get rid of all unnecessary newline characters and such unwanted things.

    Read this. Prelude's post explains it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Coverting a string into upper or lowercase
    By TheShaggmeister in forum C++ Programming
    Replies: 13
    Last Post: 07-13-2003, 02:21 AM
  2. time.h making me wad grrrr
    By qwerty in forum C++ Programming
    Replies: 6
    Last Post: 01-22-2002, 05:46 PM