-1

This is what I have so far:

#include <iostream>
#include <cmath>
#include <string.h>
using namespace std;


int main() {
    string hexa = "1A";
    
//  cout<<"HEXADECIMAL TO DECIMAL\n";
//  cout<<"ENTER HEXADECIMAL: ";
//  cin>>hexa;  
    
//  int inc1 = 0, hex1, ans, total = 0;
                            
    int a=0, b=1, ans, count=0, total, size=hexa.length();
    //count also increment
    
    for (int i=0; i<hexa.length(); i++){
        if(hexa[i] == 'A'){
            ans = 10 * pow(16, i);
        } else {
            ans = hexa[i] * pow(16, i);
        }
        total = total + ans;
    }
    
    cout<<"ANSWER IS: " <<total;    
/**
    do{
    hex1 = hexa % 10;
    ans = hex1 * pow(16, inc1);
    inc1++;
    total = total + ans;
    hexa = hexa / 10;
    } while (hexa!=0);
    
    
**/
}

I could just steal some codes online but I don't want to cheat this one (my teachers are getting suspicious of me). The code inside the comment works, the one with the do-while loop but it only works with numbers-only input. Im struggling to create a code that accepts input with letters. Also, Im not permitted to use those fancy functions and whatnot.

Batang Z
  • 1
  • 1
  • 3
    Does this answer your question? [C++: Converting Hexadecimal to Decimal](https://stackoverflow.com/questions/11031159/c-converting-hexadecimal-to-decimal) – voiarn Jan 25 '22 at 08:04
  • 2
    `int total = std::stoi(hexa, nullptr, 16);` – Some programmer dude Jan 25 '22 at 08:05
  • 1
    But if you want to do it yourself, you might want to learn about [`std::isalpha`](https://en.cppreference.com/w/cpp/string/byte/isalpha). And I recommend using bit-shifting instead of the floating-point `pow` function. – Some programmer dude Jan 25 '22 at 08:08
  • And a very big problem is that there is no encoding where the character `'1'` is equal to the integer `1`. However, C++ requires that all digit characters are encoded consecutively, which means `'1' - '0'` will be equal to `1`. While there's no such guarantee for letters (and there exists encodings where letters aren't consecutively encoded), in the most common ASCII encoding all upper-case letters are consecutively encoded, meaning that e.g. `'B' - 'A'` will also be equal to `1` (here e.g. [`std::toupper`](https://en.cppreference.com/w/cpp/string/byte/toupper) might be useful). – Some programmer dude Jan 25 '22 at 08:10
  • 1
    And `std::isxdigit` – jkb Jan 25 '22 at 08:10
  • 1
    If you don't want to steal codes online and are not allowed to use "those fancy functions and whatnot", what exactly is your question? – Lukas-T Jan 25 '22 at 08:13
  • And I'm sure the code in comments doesn't even compile unless for some reason you can apply modulo and division operators on a `std::string`. – Lukas-T Jan 25 '22 at 08:20
  • This code is broken, and the reason why it's broken is the usage of `pow`, a floating point function. Do not use floating point functions in an integer-based program -- floating point may not yield the results you are expecting due to floating point being an approximation. If you want a power of 16, use a simple lookup table. – PaulMcKenzie Jan 25 '22 at 08:26
  • The commented code "converts" one number into a different number - for instance, it "converts" twelve into eighteen, and one hundred into two hundred and fifty-six. In other words: it doesn't work. – molbdnilo Jan 25 '22 at 08:30

1 Answers1

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

int main(){
  string s;
  cin>>s;//input string
  int ans=0;//for storing ans
  int p=1;
  int n=s.size();//length of the string
  //travarsing from right to left
  for(int i=n-1;i>=0;i--){
    //if the character is between 0 to 9
    if(s[i]>='0'&&s[i]<='9'){
        ans=ans+p*(s[i]-'0');           
    }
    //if the character is between A to F
    else{
            ans=ans+p*(s[i]-'A'+10);
    }
  p=p*16;                
  }
  cout<<ans;
  return 0;
}