0

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

2
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by empror9
Featured Replies
  • Change this and let me know if it works as intended: [CODE] if (a1 == 'a1') [/CODE] to this: [CODE] if (a1 == "a1") [/CODE] If you have more than one character, you need double quotes. Double quotes for strings, single quotes for single characters. Read More

0

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.

0

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 *_^

0

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.

0

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 by empror9: n/a

1

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.