-1

I have a very basic question how can I assign a string value to a variable that already has integer value. For example, int a = 10 then I need to assign a ='hey'. Is it possible in c++?

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
HElooo
  • 23
  • 4
  • It is possible in C++, you can use `std::variant`. – Eljay Nov 26 '20 at 21:00
  • `'hey'` is a multicharacter literal, not a string. You can assign it to an `int`. That means `int a = 10; a = 'hey';` is valid in C and C++ but the result is implementation defined. Read: https://en.cppreference.com/w/cpp/language/character_literal – Thomas Sablik Nov 26 '20 at 21:02
  • You could use some kind of container struct, which has a pointer to some buffer and a variable holding information about what type is. This way you could change the type. This is how python does it. I don't recommend it though as it's not very performant. –  Nov 26 '20 at 21:04
  • 3
    you can use `std::any` or `std::variant` but only where they're actually useful, use separate variables for separate things, its rare that the same thing needs to be 2 different types. What are you actually trying to implement? Please show a [mre] – Alan Birtles Nov 26 '20 at 21:04
  • 3
    What are you really trying to do? What is the actual problem you need to solve?`Why do you think you must assign a character or a string to an `int` variable? – Some programmer dude Nov 26 '20 at 21:08
  • I'm assigning a multicharacter to an integer – HElooo Nov 26 '20 at 21:11
  • Yes we can see that, but *why?* What problem is it supposed to solve? Right now this is too much of an [XY problem](https://en.wikipedia.org/wiki/XY_problem). – Some programmer dude Nov 26 '20 at 21:12
  • Basically I'm comparing numbers and if one number is less than default Im substituting that number with 'NaN' – HElooo Nov 26 '20 at 21:13
  • 1
    `std::optional` sounds like a better option – Alan Birtles Nov 26 '20 at 21:14
  • Its just an exercise, but the point is that I tried to say a = 'NaN' but Im getting a random number(ASCII value of character) – HElooo Nov 26 '20 at 21:14
  • It's not a random number. It's implementation defined but on most systems it's `'N' * 256 * 256 + 'a' * 256 + 'N'` – Thomas Sablik Nov 26 '20 at 21:19
  • any idea what I can do to get actual value? – HElooo Nov 26 '20 at 21:21
  • *Basically I'm comparing numbers and if one number is less than default Im substituting that number with 'NaN'* -- Even that explanation is XY Problem territory. What is the *high-level* problem you are trying to solve? – PaulMcKenzie Nov 26 '20 at 21:22
  • There is no problem, its just execrcise to practice and get better in cpp – HElooo Nov 26 '20 at 21:23
  • The problem with multi-character constants is that they need to have an actual value, no matter how implementation-defined such constants are. So how do you differ from an actual valid `int` value and the value created by e.g. `'NaN'`? If you want an `int` value to be optionally "empty" or unset, then `std::optional` (as mentioned by @AlanBirtles) is really a better way. – Some programmer dude Nov 26 '20 at 21:24
  • Here you can see how the value is calculated with GCC https://wandbox.org/permlink/xoUSHHNbL9ANPijX – Thomas Sablik Nov 26 '20 at 21:25
  • as has been already pointed out, this is a XY problem. The solution to your problem "How to store a special value in a `int`, so I can distinguish 'no result' from 'result' ?" is not to store a string in an `int`. Instead of asking for that, better ask for the actual problem you are trying to solve – 463035818_is_not_an_ai Nov 26 '20 at 21:31
  • @ThomasSablik Multicharacter literals are conditionally-supported, so it may not be valid depending on the compiler. – NotAProgrammer Nov 26 '20 at 21:32

1 Answers1

1

I think you are new to C++? In C++ you assign the variable a fixed type (int in your case). It is not possibile to change that type inside the scope. You have to convert a to a string and save them in a new variable. How to convert a string (char* or std::string) you can look here:

Easiest way to convert int to string in C++