1
#include <iostream>
#include <string> 
#include <cmath> 

using namespace std;

int main(int argc, char **argv)
{
    string c; 
    int k = 0, decval, i; 
    cout << "Please input your number starting from lowest value number to highest" << endl; 
    cin >> c;
    //the for loop takes a backwards integer and makes it forwards.
    for(i = 0; i < c.length(); i++){
        decval += (c[i] - '0') * pow(10, k);
        ++k;
    } 
    cout << decval;  
    return 0;
}

so my problem is when I input something like 564(wanting to get 465 in return) I get 462. I haven't been able to spot the logic error in the code. Note that I am both new to coding and stack overflow so please don't be too harsh. Any help would be greatly appreciated.

Michael Hall
  • 31
  • 1
  • 1
  • 5
  • 8
    the biggest help I can give you (even better than an answer resolving your problem) is to tell you how **paramount** a **debugger** is. Learn to debug and you will make your life so much easier. You will save countless hours of frustration and bashing your head on the desk because *It just should work!!... Why don't you work damn it!!??* – bolov Jan 27 '15 at 22:43

4 Answers4

6

You forgot to initialize decval to 0. It probably contains an arbitrary value which messes up your result.

nicebyte
  • 1,498
  • 11
  • 21
  • Going back and initializing it to zero nearly got it there, oddly when I initialized it to 1 then every test output was on point. Thanks for the help nicebyte. – Michael Hall Jan 27 '15 at 22:49
  • @MichaelHall Your compiler should have warned you about this mistake. – drescherjm Jan 27 '15 at 22:59
4

This code:

(c[i] - '0') * pow(10, k);

Converts an integral type to floating point, performs floating-point math, then converts back to an integral type. (See this question)

You absolutely risk rounding errors along the lines of 59.99999 getting rounded down to 59.

Adjusting your logic to only use integer math will fix it.

int multiplier = 1;
for(i = 0; i < c.length(); i++, multiplier *= 10){
    decval += (c[i] - '0') * multiplier;
    ++k;
} 
Community
  • 1
  • 1
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
1

This is also a solution and is pretty simple I think:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main(int argc, char **argv)
{
    string str;

    cout << "Please input your number starting from lowest value number to highest" << endl;
    cin >> str;

    reverse(str.begin(), str.end());

    int number = stoi(str);

    cout << number << endl;

    return 0;
}
Silex
  • 2,583
  • 3
  • 35
  • 59
0

Both Drew and nicebyte are correct in what they have pointed out. Just wanted to add that you can do this without k, an extra multiplier variable, calling pow(), or rounding issues:

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main(int argc, char **argv)
{
    string c;
    int decval = 0, i;
    cout << "Please input your number starting from lowest value number to highest" << endl;
    cin >> c;
    //the for loop takes a backwards integer and makes it forwards.
    for( i=c.size()-1 ; 0<=i ; --i ) decval = c[i] - '0' + 10*decval;
    cout << decval << endl;
    return 0;
}

Explanation of for loop:

Take the number 4321 for example. Start at the end of the string, and work backwards. I.e.

  1. After first loop, decval = 1.
  2. After second loop decval = 12.
  3. After third loop, decval = 123.
  4. After fourth loop, decval = 1234.

Each time you are multiplying decval by 10, and adding the new digit. By doing it this way, you don't have to multiply by 10 the first time, 100 the second time, 1,000 the third time, etc.

Matt
  • 20,108
  • 1
  • 57
  • 70