0

I want to write like this:

int x;
cin >> x;
if (x == 10) {
x = "A";
}

But I get this error message "error: invalid conversion from 'const char*' to 'int'"

AlexD4K
  • 21
  • 5
  • 3
    Why are you trying to give `x`, which is an `int`, the value of `"A"`? – NathanOliver Mar 16 '21 at 20:30
  • 1
    Once you've declared a variable, you don't get to change its type in C++. – sweenish Mar 16 '21 at 20:30
  • 1
    It's not clear what you want but an assignment like that should be `x = 'A'`; – anastaciu Mar 16 '21 at 20:31
  • 3
    Having all the above comments in mind: `if (x = 10);` is functionally equivalent to `x = 10;`. – Algirdas Preidžius Mar 16 '21 at 20:32
  • Can you explain what you want to achieve? What should the program do? – rustyx Mar 16 '21 at 20:34
  • 1
    You can't assign a char to an int. If you actually want to convert an int to a string, [this](https://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c) already answers your question. – Brent Mar 16 '21 at 20:35
  • 1
    @Brent actually you can, what you can't do is to assign a `char*` to an `int`. – anastaciu Mar 16 '21 at 20:37
  • @Brent "_You can't assign a char to an int._" Char you could assign (a la `x = 'A';` is valid syntax). What you can't assign, however, is `char*`, as is attempted here, – Algirdas Preidžius Mar 16 '21 at 20:37
  • 1
    BTW, you have a `;` following the `if` statement. Usually, coding guidelines say to always use braces, `{` and `}` to avoid this pitfall. – Thomas Matthews Mar 16 '21 at 20:39
  • 1
    Make sure your compiler warnings are turned on/up because compilers _will_ warn about that extra semicolon in the if statement and save you the headache of debugging it. – chris Mar 16 '21 at 20:45
  • @rustyx I want to convert decimal numbers into hexadecimal. I take an input and divide it by 16, if the remainder is 10 the hexadecimal would be "A". 11 = "B" etc until 15 = "F". – AlexD4K Mar 16 '21 at 20:46
  • @ThomasMatthews Right thank you, I'm allways mixing python with c++ – AlexD4K Mar 16 '21 at 20:48
  • 1
    The easiest method is to read the number as an `int`, then output using `std::hex`. You can also output to `std::ostringstream` if you want to convert to a hex string. – Thomas Matthews Mar 16 '21 at 20:50
  • Use the [edit] button and add that info into the question itself so people can understand the intent. – rustyx Mar 16 '21 at 20:51

2 Answers2

3

C++ has something called type checking, which basically means that you can't change the type of a variable (so if you define x as an int, you can't redefine it to a string). If you want to save the value of x as a string, you could do the following:

int x = 5;
std::string xAsString = std::to_string(x);

As a side note, you should use == in your if-statement to check whether two things are equal.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Right thank you, I'm very new to programming and I'm trying to do a program converting decimal numbers to hexadecimal. So I take an input and module 16. If the remainder is 10 then the hexadecimal would be "A", if its 11 it would be "B" etc until 15 = "F". – AlexD4K Mar 16 '21 at 20:42
0

From your comment below the first answer, if you are converting to a hexadecimal string, you can do it directly with std::sprintf() and a plain old C-string. See std::printf, std::fprintf, std::sprintf, std::snprintf. You can do:

#include <iostream>

#define MAXHEXSTR 36

int main (void) {
    
    uint16_t u = 65535;
    char hexstr[MAXHEXSTR] = "";
    
    std::sprintf (hexstr, "0x%04x", u);
    std::cout << hexstr << '\n';
}

You can adjust the format string to meet your needs. Above it just formats the 16-bit value with a "0x" prefix and the conversion yields a 4-byte hex representation of the value zero padded as needed.

Example Use/Output

$ ./bin/hexstrprintf
0xffff

or

A value of ten would yield:

$ ./bin/hexstrprintf
0x000a

Let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85