Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1043965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:45:06+00:00 2026-05-16T15:45:06+00:00

i have to define a variable that i am using in a program but

  • 0

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

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:45:07+00:00Added an answer on May 16, 2026 at 3:45 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable that im define with another variable and some text. $title
I would like to have a variable (or #define ) in C++ source that
I have the next scenario: I define an int[][] variable in my main class.
My basic situation: I have an include file that has something like #define foo
I have a file that defines constant variables, like this: define_vars.php <? define(something,value); define(something1,value);
I have define variable error in struts.properties as follows: error=this is an error Now
I have a variable define as: set filePath=.\file.txt I'm writing a windows batch file.
I have to extract defined variable and function names from a js code passed
Is there a way to define/undefine debug messages using std::cout whenever inside a program?
I have a windows form that creates multiple console applications of the same program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.