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!
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!
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.
Not getting it. here's a piece of the code.
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.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; }
the equation that should be used with the score is:
[seconds]/2*[number of digits]
thanks
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
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...
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.Code:int numDigits(int number) { int guessmax; return log10(guessmax) + 1; }
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.*
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 ) ); } }