Solving Error in C++ Digit Algorithm Program

  • C/C++
  • Thread starter chaoseverlasting
  • Start date
  • Tags
    Algorithm C++
In summary, the log() function calculates the natural logarithm. It always gives a weird result on my computer, but I don't know why. I am using a dev c++ compiler. Is this homework? No, this is not homework.
  • #1
chaoseverlasting
1,050
3
I wrote a program to find the number of digits of an integer, but I always get the wrong result. Could someone point out the error? Here's the source code:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
int i=53443;
cout<<log(i);
return 0;
}


The log function gives me a weird result each time. I don't know why though. I am using dev c++ compiler...
 
Technology news on Phys.org
  • #2
The log function computes the natural logarithm.
 
  • #3
man log10
 
  • #4
Is this homework? I don't want to go much further with help if it is.
 
  • #5
This isn't homework. I am trying to write a program which needs this bit. I can't seem to figure this out. So is it log(e) or log (10) ?
 
  • #6
If that's it (the natural log- base 10 difference), I can figure the rest out. Is there perhaps some property of integers and the way this function works (the taylor series of log(1+x) maybe?) that's causing this?
 
  • #7
RTFM, or as Borek said, man log10 (and man log won't hurt). If you are not on a UNIX machine, there is always the internet. What does log() compute?

BTW, neither log() nor log10() is really what you want. Will this approach work on negative integers or on 64 bit (long long) integers?
 
  • #8
Personally I'd use sprintf & count the characters in the buffer...
 
  • #9
Since this is not homework, I will give my solution. Keep dividing by ten (the integer, not the double) until the result becomes zero. This works for all sizes of integers, negative integers, is much faster and more accurate than using log10(), and is much, much faster than sprintf.
Code:
int ndigits = 0;
for (same_int_type_as_i rem = i; rem != 0; rem /= 10) {
   ++ndigits;
}
 
  • #10
When I went to bed yesterday I thought of strlen(itoa(abs(i))) but I am not addicted to the web enough to post such things immediately :wink: This is similar to sprintf, just much easier to code.
 
  • #11
Borek said:
When I went to bed yesterday I thought of strlen(itoa(abs(i))) but I am not addicted to the web enough to post such things immediately :wink: This is similar to sprintf, just much easier to code.

D H said:
Since this is not homework, I will give my solution. Keep dividing by ten (the integer, not the double) until the result becomes zero. This works for all sizes of integers, negative integers, is much faster and more accurate than using log10(), and is much, much faster than sprintf.
Code:
int ndigits = 0;
for (same_int_type_as_i rem = i; rem != 0; rem /= 10) {
   ++ndigits;
}

Thank you. :cool: That helps :cool:.
 

1. Why am I getting an error when running my C++ program?

There could be several reasons why you are getting an error in your C++ program. Some common causes include syntax errors, logical errors, and runtime errors. Syntax errors occur when the code violates the rules of the programming language and can be easily fixed by correcting the code. Logical errors occur when the program runs but does not produce the expected output, and finding and fixing these errors may require debugging techniques. Runtime errors occur when the program is running and can be caused by issues such as out-of-bounds array access or division by zero.

2. How can I debug my C++ program to find and fix errors?

To debug your C++ program, you can use a debugger tool that allows you to step through your code line by line, inspect variables, and track the execution flow. You can also use print statements to output the values of variables at different stages of the program to identify where the error occurs. Another helpful technique is to use a rubber duck or explain the code to someone else, as this can help identify any logical errors.

3. I have fixed all the errors in my C++ program, but it still doesn't work. What could be the issue?

Even after fixing all the errors, your C++ program may not produce the expected output. This could be due to an underlying algorithmic error or a logical error that may be difficult to spot. In such cases, it is helpful to break down the code into smaller parts and test each part separately to identify the root cause of the issue.

4. How can I avoid errors in my C++ program?

To avoid errors in your C++ program, it is essential to have a good understanding of the programming language and its syntax. You should also follow good coding practices, such as writing modular and reusable code, using meaningful variable names, and adding comments to explain complex sections of code. Additionally, testing your code regularly and using debugging techniques can help catch errors early on.

5. Can I prevent runtime errors in my C++ program?

While it is not always possible to prevent runtime errors in your C++ program, there are some steps you can take to minimize the chances of them occurring. These include checking for invalid user input, avoiding out-of-bounds array access, and handling exceptions using try-catch blocks. It is also a good practice to test your program with different inputs to ensure it can handle a variety of scenarios without crashing.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
3
Views
728
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top