Click here to Skip to main content
15,793,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am trying to take a number as string input and convert it to the number but its showing a little error cann't understand what to do..suppose the input was 123456789
and my code showing output as 123456785 change in the last digit..help please..

What I have tried:

C++
#include <iostream>
#include <cmath>
using namespace std;

int main()
{

    string s;
    while(cin>>s)
    {
        long long number=0;
        int len=s.size();
        for(int i=len-1,j=0;i>=0;i--)
        {
            int digit=s[i]-'0';
            number+=digit*pow(10,j);
        j++;
        }
        cout<<number<<endl;
      
    }

    //cout << "Hello world!" << endl;
    return 0;
}
Posted
Updated 19-Apr-16 22:34pm
v4
Comments
Richard MacCutchan 20-Apr-16 3:07am    
That should not even compile since pow does not accept or return ints.
Jochen Arndt 20-Apr-16 3:13am    
The VS math.h header contains a template:

_Ty _Pow_int(_Ty _X, int _Y)

which is used by some inline functions including:

inline double __cdecl pow(int _X, int _Y) {return (_Pow_int(_X, _Y)); }
Richard MacCutchan 20-Apr-16 3:34am    
The sample above does not include that header, so std::pow is implied.
Jochen Arndt 20-Apr-16 3:40am    
There may have been cmath or math.h behind the empty #include (it has been added now by ppolymorphe).

Even with std::pow there are versions with exp as integer (since C++11) and a promotion for other types which may have been already included with the used development environment.
Richard MacCutchan 20-Apr-16 5:17am    
Not according to https://msdn.microsoft.com/en-us/library/dt5dakze.aspx.

But this is all academic anyway, as none of us actually knows what the OP did in his/her real test.

You code works fine on my Lubuntu box (g++ 4.7.3).
You may also write it this way
C++
 #include <iostream>
 #include <cmath>
using namespace std;

int main()
{
  string s;
  while ( cin >> s)
  {
    long long number = 0;
    for (const auto & c : s)
    {
      number *= 10;
      number += c -'0';
    }
    cout << number << endl;
  }
}
 
Share this answer
 
v2
Comments
Member 12270086 20-Apr-16 3:02am    
what does for loop codition meaning
CPallini 20-Apr-16 3:09am    
It is the range-based for (similar to other languages foreach construct), see
http://en.cppreference.com/w/cpp/language/range-for
Sergey Alexandrovich Kryukov 20-Apr-16 3:39am    
5ed.
—SA
CPallini 20-Apr-16 3:48am    
Thank you.
I just tested it with GCC (g++) on Ubuntu and it is showing the correct result.

Your wrong output indicates that there is probably a rounding error which may be introduced by using the pow() function. Depending on your compiler and the math.h / cmath header file it may use floating point or a template.

[EDIT]
When using VS there is a _Pow_int template used with inline functions in the math.h header file:
inline double __cdecl pow(int _X, int _Y)
        {return (_Pow_int(_X, _Y)); }

So the power is caluclated from integers and the returned result is converted to double. This result is then converted to long long. These two conversions may lead to rounding errors.
[/EDIT]

But you can avoid using pow() by rewriting your function:
long long number=0;
int len = s.size();
for (int i = 0; i < len; i++)
{
    int digit=s[i] - '0';
    number *= 10;
    number += digit;
}
cout<<number<<endl;
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 20-Apr-16 3:39am    
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900