Reputation: 85
My problem is simple but I have no idea on solving it.
I have an integer variable which gets random numbers.
But on special occasions this integer represents chars.
For instance, if rand()
function gives 100
, the variable becomes A
.
I used switch-case
to manage these exceptions.
Then I executed the program and rand()
gave 100
for a variable.
However, instead of printing out A
char, program gave its ASCII value, 65
.
I thought about opening up another print function and implementing type casting for these exceptions. But there's a lot of them, and also, I'm getting a lot of random numbers during the program, so it's almost impossible to make this happen.
// Program gets random values for a lot of variables (`rand_val1`, `rand_val2`, `rand_val3`...)
// For some integers, it converts them to pre-defined chars
char A = 'A', char B = 'B' ...
...
switch (rand_val1)
case 100:
rand_val1 = A;
break;
case 200:
rand_val2 = B;
break;
...
switch (rand_val2)
...
// It prints out each one of them.
cout << rand_val1 << " " << rand_val2 << ... << endl;
/* As output, it doesn't give chars and instead it gives their ASCII values
>>> 65 66 300 400 500 70 71 ...
What can I do in this case? Any ideas?
Upvotes: 0
Views: 91
Reputation: 791
Try This one. This is a much simple If Else condition but based on your requirements, it does not need to be dynamic
#include<iostream>
using namespace std;
char Convert(int input)
{ char ToBeReturn = ' ';
if (input == 100)
{
ToBeReturn = 'A';
}
if (input == 200)
{
ToBeReturn = 'B';
}
if (input == 300)
{
ToBeReturn = 'C';
}
if (input == 400)
{
ToBeReturn = 'D';
}
return ToBeReturn;
}
void main()
{ int rand_val1, rand_val2, rand_val3;
cout << "Enter Value 1: ";
cin >> rand_val1;
cout << "Enter Value 2: ";
cin >> rand_val2;
cout << "Enter Value 3: ";
cin >> rand_val3;
cout << Convert(rand_val1) << " " << Convert(rand_val2) << " " << Convert(rand_val3) << endl; }
Upvotes: 0
Reputation:
Because you told there are a lot of such conversion, so I dont think any function / std::cout type casting suit you. You can use a wrapper class here. Just declare all the rand_valXXX as Integer class.
If every rand_val have different conversion logic, then I think you have no way, but cast it every time when you print.
#include <iostream>
class Integer
{
public:
Integer( int a ) : _a(a){}
operator int() const { return _a; }
private:
int _a;
};
std::ostream& operator<< ( std::ostream& out, const Integer& val )
{
switch( val )
{
case 100:
out << 'A';
break;
case 200:
out << 'B';
break;
default:
out << (int)val;
}
return out;
}
int main()
{
Integer a = 100;
std::cout << a;
a = 200;
std::cout << a;
a = a + 100;
std::cout << a;
a = a / 200;
std::cout << a;
}
Upvotes: 0
Reputation: 3832
How about defining an "exception list" ? Here's an example
#include <bits/stdc++.h>
using namespace std;
int main ()
{
const unordered_map<int, char> exceptionList = {
{ 100, 'A'},
{ 200, 'B'},
{ 300, 'C'},
{ 400, 'D'},
{ 500, 'E'}
};
int rand_val1 = 100;
if (exceptionList.find(rand_val1) == exceptionList.end())
{
cout << rand_val1 << endl;
}
else
{
cout << exceptionList.at(rand_val1) << endl;
}
return 0;
}
Or with a lambda:
#include <bits/stdc++.h>
using namespace std;
int main ()
{
const std::unordered_map<int, char> exceptionList = {
{ 100, 'A'},
{ 200, 'B'},
{ 300, 'C'},
{ 400, 'D'},
{ 500, 'E'}
};
int rand_val1 = 100;
int rand_val2 = 101;
const auto charOrInt = [&exceptionList] (const int val) -> string {
if (exceptionList.find(val) == exceptionList.end())
{
return to_string(val);
}
else
{
return string{exceptionList.at(val)};
}
};
cout << charOrInt(rand_val1) << " " << charOrInt(rand_val2) << endl;
return 0;
}
Upvotes: 1