开发者

i want a variable that will change from a char to a int and vice versa depending on the user entry

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()
{
    开发者_StackOverflow社区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


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


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;
}


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜