dumb question?

This is a discussion on dumb question? within the C Programming forums, part of the General Programming Boards category; i'm trying to return the number of characters inputted into a scanf statement and don't know how to do it. ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    4

    dumb question?

    i'm trying to return the number of characters inputted into a scanf statement and don't know how to do it.

    so if a user inputs "100", i want the program to store the number 3 for later use.

    help??

    thanks!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You could read the input as a string and then count the characters. Or if you already have a number, try a loop.
    Code:
    for(digits = 0; number; number /= 10, digits ++);  /* Untested */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    Not getting it. here's a piece of the code.

    Code:
    #include <stdio.h>
    
    //int numDigits(int);
    
    int main()
    {
     int secnum, answer, guessmax, number;
     
     int start = time(0);
     
        printf("Enter the maximum number for the game.\n");
            scanf("%d", &guessmax);
            secnum=rand()%guessmax;
            printf("Enter your guess!\n");
            scanf("%d", &answer);
         
      while (answer != secnum)
      {
        if (answer < secnum)
         {
          printf("Your guess is too low, try again.\n");
          printf("Enter your guess!\n");
          scanf("%d", &answer);
         }//if (answer <)
        else if (answer > secnum)
         {
          printf("Your guess is too high, try again.\n");
          printf("Enter your guess!\n");
          scanf("%d", &answer);
         }//else if
      } // end while
      printf("Great job! The secret number was %d.\n", secnum);
        int end = time(0);
        int timespent = end - start;
        printf("your code took %d seconds.\n", timespent);
      system ("PAUSE");
      return 0;
    }
    After the user inputs guessmax, i want to take that input, turn it into the number of digits, then take that number, and use it to calculate the users score.

    the equation that should be used with the score is:

    [seconds]/2*[number of digits]

    thanks

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I already posted some comments about your code on a previous thread, but to specifically answer this question :

    Think of the way a number is written. For example,

    123 = 1*10^2 + 2*10^1 + 3*10^0.

    So notice that the power of the last element (namely, 2) is one less then the number of digits. So to get what you're looking for, you could always find that number and add 1. But how do you find that number ?

    Here's a hint. The value you're looking for is the number of times you can divide by 10 before your result becomes smaller than 10.

    Hope that helps you out.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    4
    log10(guessmax) +1??

    I think its a step in the right direction. i tried integrating it into a function and then calling the function after the correct answer is given and i've been getting a very long negative number. Reference previous code and add this function after the
    printf("greatjob...

    Code:
    int numDigits(int number)
    {
     int guessmax;
     return log10(guessmax) + 1;
    }
    returning it seems like the correct thing to do if I want to use it later? I set up a printf to return the value the function returned and its not working correctly.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Something else to consider:
    Code:
    #include <stdio.h>
    
    int main()
    {
       char input[20];
       int value, count;
       fputs ( "enter an integer: ", stdout );
       fflush ( stdout );
       if ( fgets  ( input, sizeof input, stdin ) != NULL &&
            sscanf ( input, "%d%n", &value, &count ) == 1 )
       {
          printf ( "value = %d, count = %d\n", value, count );
       }
       return 0;
    }
    
    /* my output
    enter an integer: 100
    value = 100, count = 3
    */
    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.*

  7. #7
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    int numDigits(int number)
    {
        if (number == 0)
        {
            return(1);
        }
    
        if (number < 0)
        {
            return( 1 + (int) log10( -1.0 * (double) number ) );
        }
        else
        {
            return( 1 + (int) log10( (double) number ) );
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dumb question
    By travis999 in forum C Programming
    Replies: 3
    Last Post: 10-26-2007, 12:57 AM
  2. very dumb question.
    By Blips in forum C++ Programming
    Replies: 14
    Last Post: 11-08-2005, 08:37 AM
  3. Dumb question
    By dragon2309 in forum C Programming
    Replies: 18
    Last Post: 10-29-2005, 03:27 PM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. another dumb question - loop not working?
    By Captain Penguin in forum C++ Programming
    Replies: 8
    Last Post: 10-06-2002, 10:15 PM

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