scanf a null charactor

This is a discussion on scanf a null charactor within the C++ Programming forums, part of the General Programming Boards category; I am trying to update a file the user is asked to enter the new reorder level (integer) if they ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    3

    scanf a null charactor

    I am trying to update a file the user is asked to enter the new reorder level (integer) if they do not require to update this field they press enter. The code below is what i am using, it works if the customer enters a number, if they enter through the program fails at the scanf. Is there a way to get around this.


    printf("\nWhat is the reorder level?");
    fflush(stdin);
    scanf("%d", &temp_s.reorderLevel);
    if(temp_s.reorderLevel != NULL)
    (*new_s).reorderLevel = temp_s.reorderLevel;

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    29
    scanf("%d", &temp_s.reorderLevel);
    if(temp_s.reorderLevel != NULL)
    (*new_s).reorderLevel = temp_s.reorderLevel;

    What is temp%s.reorderLevel?
    If it's a pointer, then the scanf won't work(you try to save an integer %d to adress of pointer temp_s.reorderLevel,
    and if it's an integer, then temp_s.reorderLevel != NULL is very queer compararison..

    I think I didn't solve your problem because in my opinion you want this: if user press Enter, the default value will be used(e.g. last setting of reorderLevel). Maybe you can read a char, not an int, if user press Enter twice, you will get '\n' character so you know that user hit Enter. And if you get a digit, you can read the rest of the digits from input and then use a function like atoi().
    It's complicated, so if someone know better solution, please post it here, it's quite interisting problem..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  3. Why am I getting these errors??
    By maxthecat in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2006, 12:00 PM
  4. Tweakable Radar...
    By DoraTehExploda in forum Game Programming
    Replies: 8
    Last Post: 06-07-2005, 10:49 AM
  5. . . . . . . - . . . - -
    By The Brain in forum C++ Programming
    Replies: 17
    Last Post: 05-17-2005, 04:01 AM

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