Hello:

Is it possible to declare a variable with multiple data types.

for example in my program I would like to declare my variable as int or char depending on the situation the variable is in as an example.

declare variable C
int K =cin();

if( K ==4)
C = 5;
else
C = '*'

thanks

try this:

#include <iostream> 
using namespace std;

int main() {
	char var;
	char choice;
	clog << "do you want the variable to be int or char? type i for int and c for char.\n";
	cin >> choice;
	clog << "what number or char do you want displayed?\n";
	cin >> var;
	if(choice == 'i')
		clog << "\n" << (int) var - 48 << "\n"; 
	else
		clog << "\n" << var << "\n";
	return 0;
}

You can't really change a variable unless you cast it into something else. In the example I cast the char to int and subtract 48 because if you look at the ASCII table (http://www.asciitable.com/) , thats how much the character 0 and the integer 0 are offset by.

I can't think of any really simple way to do this. I suppose you could use something like a struct or a union. This way you could store more than one type of variable depending on what you need. I was thinking something like:

#include <iostream>

using namespace std;

struct Data
{
	int a;
	char b;
	Data();
};

Data::Data()
{
	a = 0;
	b = '\0';
}

int main()
{
	Data data;
	string temp;
	
	cout << "Enter some data -> ";
	cin >> temp;
	
	if (temp == "4")
	{
		data.a = 4;
	}
	else
	{
		data.b = '*';
	}
	
	cout << data.a << endl << data.b << endl;
	
	return 0;
}

Not an ideal solution, but it works.

Thank you all for your reply....

What about using a template <typedef> T

T somenumer

Unfortunately typedef templates aren't supported by the C++98 standard. Here's a way you could convert a 3 digit string into the corresponding 3 digit integer:

#include <iostream>
#include <string>
using namespace std;

int main() {
	string numstr;
	clog << "enter 3 digit number\n";
	cin >> numstr;
	int numint = ((numstr[0] - 48) * 100) + ((numstr[1] - 48) * 10) + (numstr[2] - 48);
	clog << "number in integer form: " << numint << "\n";
	clog << "number in string form: " << numstr << "\n";
	return 0;
}

but yeah.. sorry I guess that really doesn't answer your question. Do you HAVE to set that same variable to '*'. There might be an easier work around depending on your application.

C language doesn't have a real character data type. char is just a one-byte integer, and all characters such as '=' are treated exactly like integers. You can safely assign '=' to either a char or int data type (also long, long long, __int64, float or double).

You can also perform mathematical operations with characters, such as int numint = ((numstr[0] - '0') * 100) + ((numstr[1] - '0') * 10) + (numstr[2] - '0');

Edited 7 Years Ago by Ancient Dragon: n/a

This sounded interesting so I made a class that changes its type,
simulates it at least.

I know there are some bugs, but its getting late ( 3 am). Anyways,
it could do the basic things a primitive datatype can to for now,
that is add,subtract,multiply,divide. It can also be used with
cin/cout, or ostream/istream;

Anyways here you are. You can disregard its implementation, and just use it.

I have an example below :

#include <iostream>
#include <sstream>
#include <string>

using namespace std;


namespace Type
{		
	using namespace std;

	class Variable
	{
		string  var;
		ostream& strm;
	public:		
		Variable() : strm(cout), var(""){ }
		template<typename T >
		Variable(T a, std::ostream& os = cout) : strm(os),var("") {
			var = this->changeType( a ); 
		}
		template<typename U>		
		string changeType(const U val ){
			stringstream converter;			
			converter << val;	
			return converter.str();
		}	
		//operator overloading	
		template<typename T>
		Variable operator+(const T a)
		{			
			stringstream ss;			
			T d;
			ss << var;
			ss >> d;
			ss.clear();
			ss.str("");
			T r = d + a;
			ss << r;			 
			Variable tmp(ss.str());
			return tmp;

		}

		Variable operator+(const Variable& v){
			stringstream ss;
			double d;
			ss << v.var;
			ss >> d;
			return operator+(d);
		}
		template<typename T>
		void operator+=(const T a){				
			string tmp = "";
			tmp = this->operator +(a); 
			var = tmp;
		}
		template<typename T>
		Variable operator-(const T a){			
			return this->operator +(-a);
		}	
		Variable operator-(const Variable v){
			stringstream ss;
			double d = 0.0;
			ss << v;
			ss >> d;
			return this->operator +(-d);
		}
		Variable operator-(const string s) {
			Variable tmp(s);  return this->operator-(tmp);
		}
		Variable operator-(){
			Variable tmp(var);
			stringstream ss;
			double d;
			ss << var;
			ss >> d;
			tmp = -d;
			return tmp;
		}
		template<typename T>
		void operator -=(const T a) {
			string tmp = "";
			tmp = this->operator -(a); 
			var = tmp;
		}
		template<typename T>
		Variable operator /(const T a)
		{
			stringstream ss;
			T d;
			ss << var;
			ss >> d;
			ss.clear();
			ss.str("");
			T r = d / a;
			ss << r;
			Variable t( ss.str());
			return t;

		}
		template<typename T>
		void operator /=(const T a){
			string tmp = "";
			tmp = this->operator /(a);
			var = tmp;
		}
		template<typename T>
		Variable operator *(const T a){
			if(!a || !var[0]) 
				return "0";

			string tmp = var;
			string orig = var;
			stringstream ss(var);
			T add;
			ss >> add;

			for(int i = 1; i < a; i++)	{
				tmp = this->operator +(add);
				var = tmp;	
			}
			//reset val;
			var = orig;

			Variable t( tmp );
			return t;
		}
		template<typename T>
		void operator *=(const T a){
			string tmp = "";
			tmp = this->operator *(a);
			var = tmp;
		}
		template<typename T>
		void operator = (const Variable& a)
		{			
			if(var != a.var)
				var = a.var;			
		}			
		template<typename T>
		void operator = (T a){
			var = "";
			var = this->changeType(a);			
		}							
		operator string() { return var;	}	
	
		//Friend
		friend ostream& operator<<(ostream& os,const Variable& v ){
			os << v.var;
			return os;
		}
		friend istream& operator>>(istream &stream, Variable &v)
		{
			stream >> v.var;
			return stream;
		}		
		template<typename T>
		friend Variable operator*( T a, Variable& v) {
			return v * a; 
		}
		template<typename T>
		friend Variable operator*=(T a, Variable& v) { return v *= a; }
		template<typename T>
		friend Variable operator/(T a, Variable& v) { return v / a; }
		template<typename T>
		friend Variable operator/=(T a, Variable& v) { return v /= a; }
		template<typename T>
		friend Variable operator+(T a, Variable& v) { return v + a; }
		template<typename T>
		friend Variable operator+=(T a, Variable& v) { return v += a; }
		template<typename T>
		friend Variable operator-(T a, Variable& v) { return v - a; }
		template<typename T>
		friend Variable operator-=(T a, Variable& v) { return v -= a; }

	};		
	
}


int main() 
{
	using namespace Type;
				
	 Variable X("hello");

	 cout << X << endl;

	 X.changeType(3.1415);

	 cout << X << endl;

	 X  = 100/200.0;

	 cout<< X << endl;

	 cout<<"Enter a number : ";
	 cin >> X;
	 cout<< X <<" * 7 is = " << X*7;

	return 0;
}

Edited 7 Years Ago by firstPerson: n/a

No it does not have to be '*' but something to represent a null character so it does not have to be a char but I was curious if you could change a data type on the fly.

This article has been dead for over six months. Start a new discussion instead.