views:

72

answers:

4

i have to define a variable that i am using in a program but i want the variable to change from int to char.

example

#include <iostream>
#include <cstdlib>

int main()
{
    int l=rand();
    char x;
    std::cout<<"this program makes a random char or number enter n for a number or c if you want a letter\n";
    std::cin>>l>>"\n";
    if (x="c")
    {
        char l=rand();
        std::cout<<"here is your letter :"<<l<<"\n";
    }
    else if (x="n")
    { 
        int l=rand();
        std::cout<<"here is your number :"<<l<<"\n";
    }
}

so i already know there is other problems this was just an example i wrote real quick to show what i meant. i want to make it so that depending on what the user enters it will change l from a char to an int. I don't know if there is something to do this but if there is i would like to know.

Thank You

+2  A: 

You can use a union

union char_and_int {
    char char_l;
    int int_l;
}l;

//...

l.int_l=rand();
if (x == 'c')
    {
        std::cout<<"here is your letter :"<<l.char_l<<"\n";
    }
    else if (x == 'n')
    { 
        std::cout<<"here is your number :"<<l.int_l<<"\n";
    }

Also, be aware that this can result in unprintable chars

klez
please, read your code again before posting - typical C error - `if (x="c")` results in .. interesting behavior.
Yossarian
@Yossarian, sorry copy and pasted the OP's code XD
klez
A: 

Because C++ is a statically-typed language, a variable can't actually change from one type to another. A work around solution would be to create a struct or class that contains both an int and a char variable, and assign them accordingly.

A dynamically-typed language, like Python or Ruby, could do this though.

Jesse J
A: 

i am not a pro at c++ but i guess you can declare 2 variables, one char and one int. First you can assign the users input to char, than test it if it can be assigned as integer, and if it can be, than your program will start using that. And for deciding which variable to use, you can also declare another boolean value to keep that data. I believe there must be better ways, but i guess this works too.

gkaykck
If it can be assigned to a char, it can be assigned to an integer.
klez
i didn't meant it, i meant "if its ascii values are in between the integer's ascii values"
gkaykck
+1  A: 

The generic name for this kind of thing is a union.

You can use the union keyword to define a C-style union: a variable which may be treated as having one of several types. However, you need to do all the work to keep track of which type it is.

union {
    char c;
    int i;
} l;

bool l_is_int;

if ( x == 'c' ) {
    l_is_int = false;
    cin >> l.i;
} else if ( x == 'n' ) {
    l_is_int = true;
    cin >> l.c;
}

There is a class boost::variant which does this more elegantly and safely:

boost::variant< char, int > l;
if ( x == 'c' ) {
    char c;
    cin >> c;
    l = c; // set content type of l to char
    cout << "your char is " << boost::get< char >( l ) << endl;
} else {
    int i;
    cin >> i;
    l = i; // set content type of l to int
    cout << "your int is " << boost::get< int >( l ) << endl;
}
cout << "your int or char is " << l << endl;

For your immediate problem, using any kind of union is overkill, since an int can hold any char value:

int l;
bool l_is_int;

if ( x == 'c' ) {
    char c;
    cin >> c;
    l = c;
    l_is_int = false;
} else if ( x == 'n' ) {
    cin >> l;
    l_is_int = true;
}
Potatoswatter
k thank you for your time and help
Justin Yoder
@Justin: If you like this answer best, click the checkmark at top left. And welcome to Stack Overflow!
Potatoswatter