string detect

closed account (4w7X92yv)
Hello

I have a string code inputted and when the input is a 'A'
the program has to output 1111 as an double.

What do i wrong to make this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <string>
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    std::string hex;
    getline(std::cin,hex);
    
    if (hex = 'A')
    {
        hex = 1111;
    }

double HEX = atoi(hex.c_str());

cout << HEX << endl;


system ("pause");
return 0;
}
Last edited on
if (hex = 'A') = is assignment but you want comparison so you should use ==. 'A' is not a string, so you can't compare it with a string. You need to make it a string (or make hex a char but I don't think that is what you want).
In the end it becomes something like this: if (hex == "A")

hex = 1111; This compiles but that is just because std::string has an operator= that takes a char as argument so it is equivalent to hex = 'W'; (well, at least for me it is). What I think you mean is hex = "1111";
why use std:: if you define using namespace std;?
Topic archived. No new replies allowed.