Thread: Newbie problem with scanf function...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    Newbie problem with scanf function...

    This may sound rather basic but here goes....

    I'm trying to use the scanf function to allow the user to input a 6 digit number. Just as an example lets say its 123456. What I need to be able to do is have the user input the number without any spaces inbetween the digits, but if I do scanf( "%i%i%i%i%i%i", odd1, even1, odd2, even2, odd3, even3) the function assigns the entire number just to odd1. If I place spaces inbetween the %i's it works just fine, but then the user has to input the number with spaces Anyone got any ideas?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       int number;
       fputs("Enter a 6-digit number: ", stdout);
       fflush(stdout);
       if ( scanf("%6d", &number) == 1 )
       {
          printf("number = %d\n", number);
       }
       return 0;
    }
    Or perhaps
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char text[7];
       fputs("Enter a 6-digit number: ", stdout);
       fflush(stdout);
       if ( scanf("%6s", text) == 1 )
       {
          printf("text = %s\n", text);
       }
       return 0;
    }
    Last edited by Dave_Sinkula; 11-13-2005 at 10:36 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Newbie problem with function coding..
    By jstevanus in forum C++ Programming
    Replies: 4
    Last Post: 12-09-2004, 09:12 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM