0

I wrote this code it seemed to good but the output is always 0 what could be the problem? I m just writing a code to convert from x number system to decimal.

    #include <string>
    #include <iostream>
    using namespace std;

int main() {

string str;
long szam = 0, Dec, num, Base = 1, x,i=1;
cout << "the number : ";
cin >> str;
cout << "The input number system:";
cin >> x;

   while (str[i] == '\0')
     {
       if (str[i] = 'A') { num = 10;}
       else if (str[i] = 'B') { num = 11; }
       else if (str[i] = 'C') { num = 12; }
       else if (str[i] = 'D') { num = 13; }
       else if (str[i] = 'E') { num = 14; }
       else if (str[i] = 'F') { num = 15; }
       else { num = str[i] - '0'; }

       Dec = Dec + num * Base;
       Base = Base * x;

      }
cout << szam << endl;
return 0;

}

If x smaller than 10 its easy but i cant handle the 10+ system because of the letter use of numbers.

Koreszka10
  • 37
  • 8
  • Please put the code ***in the question itself***. – Mike Nakis Oct 01 '16 at 08:02
  • Also, you should consider that one of the major disadvantages of writing code in some language other than English is that you are unlikely to get any help from the rest of the planet. Worse yet, using cryptic variable names like "x" is unlikely to yield help from *anyone*. – Mike Nakis Oct 01 '16 at 08:05
  • Sry u are right – Koreszka10 Oct 01 '16 at 08:13
  • Your `while` loop is infinite, isn't it? Nothing changes it's condition. – ilotXXI Oct 01 '16 at 08:22
  • 1
    And there are `=` instead of `==` to compare... You know, learn one of the most useful instruments for programmer: debugger. It is easy to use and will help you to solve a lot of problems. – ilotXXI Oct 01 '16 at 08:27

2 Answers2

0
  1. Your i should start from 0, not from 1.

  2. Your i should be incremented within your loop. As it stands right now, i is never incremented, so it always has the value of 1, so you are only ever looking at str[1].

  3. Your loop should continue while str[i] != '\0', not while str[i] == '\0'.

  4. You might want to consider handling the case of not only the letters ABCDEF but also the letters abcdef.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
-1

You should use std::stol which does exactly what you need.

Dmitry Katkevich
  • 883
  • 7
  • 26