hello guys,

i have a simple question which is how can i declare a variable to accept character and number at one time

for example, i ask the user to input 'a1' to do something

waiting for you

It depends on how you want to use the input. Do you want A1 to do one thing and B1 to do an entirely different thing, or do you want A to do one thing, and B to do another, or 1 to do something in addition to A and B, and 2 to do something different in addition to A and B?

Psuedo:

get input

if(input_character == "a1")
     do something
if(input_character == "b1")
     do something else

or

get input

if(input_character == "a")
     do something
if(input_character == "b")
     do something else

if(input_number == "1")
     do something in addition
if(input_number == "2")
     do something else in addition

Also, can the user input more than 1 character or more than 1 number in the same input? That can change how the program needs to work.

hi,

i mean this one "Do you want A1 to do one thing and B1 to do an entirely different thing"

thanks for the code but i mean what is the type of the input varibles?

is it int or char or string or what

because i tried to use all of them but no one wroks

thanks again *_^

Well, string would be the variable type you want.

You should be able to use it like this:

std::string x;
std::cin >> x;

If that doesn't work, you can try using:

std::string x;
std::getline(std::cin, x);

The first method should work fine, as it's a single "word" of input. Getline is more for multiple words w/ spaces, at least as far as I've used it.

hi,

i tried both but no one works :(

this is all the code

#include<iostream>
#include<string>
using namespace std;
void board()
{
	cout << "A: 1   1   1" << endl;
                cout << "B: 1   0   1" << endl;
	cout << "C: 1   1   1" << endl;
	cout << "+------------"<< endl;
	cout << "   1   2   3" << endl;
	}
int main()
{
std::string a1;
std::cin >> a1;
if (a1 == 'a1') 
	     {
		board();
         }
	return 0;
}

Edited 6 Years Ago by empror9: n/a

Change this and let me know if it works as intended:

if (a1 == 'a1')

to this:

if (a1 == "a1")

If you have more than one character, you need double quotes. Double quotes for strings, single quotes for single characters.

This question has already been answered. Start a new discussion instead.