Ok new Interger query!
-
Ok if I have a number (stored in a regular integer) and say that number is 51.... Is there a function to search the Integer and return how many numbers there are in the Integer? e.g. 51 has a 5 and a 1 in it so it should return 2. I tried -> sprintf(IntLength, "%d", Prime); but I get compilation errors :-( does anyone have any ideas? Thanks of course in advance :-)
-
Ok if I have a number (stored in a regular integer) and say that number is 51.... Is there a function to search the Integer and return how many numbers there are in the Integer? e.g. 51 has a 5 and a 1 in it so it should return 2. I tried -> sprintf(IntLength, "%d", Prime); but I get compilation errors :-( does anyone have any ideas? Thanks of course in advance :-)
Here sprintf() will convert your prime integer to the string representation. so it will not give you your answer that how many numbers in integer. And Regarding to your error , check the type of variable IntLength used in sprintf() function.
-
Ok if I have a number (stored in a regular integer) and say that number is 51.... Is there a function to search the Integer and return how many numbers there are in the Integer? e.g. 51 has a 5 and a 1 in it so it should return 2. I tried -> sprintf(IntLength, "%d", Prime); but I get compilation errors :-( does anyone have any ideas? Thanks of course in advance :-)
Michael101 wrote:
sprintf(IntLength, "%d", Prime);
Whats the error? any way you can use the following function for it
int n = 12345678; int nCount = 1; while( n ) { n = n/10; if( n > 0 ) { nCount++; } }
nave [OpenedFileFinder]
-
Ok if I have a number (stored in a regular integer) and say that number is 51.... Is there a function to search the Integer and return how many numbers there are in the Integer? e.g. 51 has a 5 and a 1 in it so it should return 2. I tried -> sprintf(IntLength, "%d", Prime); but I get compilation errors :-( does anyone have any ideas? Thanks of course in advance :-)
int iCount = 0; int iurNumber= any number; while(iurNumber !=0) { iurNumber /=10; iCount++; } iCount will contain number of element integer have
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
Michael101 wrote:
sprintf(IntLength, "%d", Prime);
Whats the error? any way you can use the following function for it
int n = 12345678; int nCount = 1; while( n ) { n = n/10; if( n > 0 ) { nCount++; } }
nave [OpenedFileFinder]
Naveen.R wrote:
nave
you are superfast dude!:)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
Naveen.R wrote:
nave
you are superfast dude!:)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
:)
nave [OpenedFileFinder]
-
Here sprintf() will convert your prime integer to the string representation. so it will not give you your answer that how many numbers in integer. And Regarding to your error , check the type of variable IntLength used in sprintf() function.
technically you are not 100% correct. the return value of sprintf() is the number of characters stored in buffer so adapting the answer to the previous question Michael asked given by Naveen.R here[^] you could end up with
int IntLength; int n = 123456; char c[33] = {0}; IntLength = sprintf( c, "%d", n );
-
Ok if I have a number (stored in a regular integer) and say that number is 51.... Is there a function to search the Integer and return how many numbers there are in the Integer? e.g. 51 has a 5 and a 1 in it so it should return 2. I tried -> sprintf(IntLength, "%d", Prime); but I get compilation errors :-( does anyone have any ideas? Thanks of course in advance :-)
Michael101 wrote:
...but I get compilation errors...
Are we supposed to guess what those are?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Ok if I have a number (stored in a regular integer) and say that number is 51.... Is there a function to search the Integer and return how many numbers there are in the Integer? e.g. 51 has a 5 and a 1 in it so it should return 2. I tried -> sprintf(IntLength, "%d", Prime); but I get compilation errors :-( does anyone have any ideas? Thanks of course in advance :-)
I've never seen sprintf with this signature. Have you overloaded it yourself? I think the error you are getting is because you are passing an integer instead of a char*. Instead of: sprintf(IntLength, "%d", Prime); use: IntLength = sprintf( someCharBuffer, "%d", Prime ); Or better yet, use one of the other solutions already given. David