Thread: Grrrr!

  1. #1
    Registered User
    Join Date
    Aug 2002


    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
    #include <stdio.h>
    #include <conio.h>
    int main()
    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
    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
    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.

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

  4. #4
    Registered User
    Join Date
    Apr 2002
    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

    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
    Cape Town
    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