Hi,
I'm new to C++. I'm having trouble detecting space bar as an input. I'm unfamiliar with using the char data type. I've tried using ASCII decimal numbers but only detects the '.' . And I'm unsure how to go about this without using string or anything advanced.

char canvas;
		cin >> canvas;

	while(canvas < 32 || canvas > 46 || (canvas > 32 && canvas < 46)){	//input check
		cout << "Error: Canvas choice can only be 'space' or .\n";
		cout << "Choose either . or space\n";
		cin >> canvas;
	}

	if (canvas == 32){
	}
	else if (canvas == 46){
	}

space bar = 32, . = 46

>> will read to the next whitespace. ie. space character so it wont pick up your space. Do you have to use space? You can explicitly read the next char from the stream though. Check out the other stream functions available to you. Is there anything else you could use.

Yes it has to be space. Preferably input >> space bar then enter, which is what I'm trying to do.

Yes it has to be space. Preferably input >> space bar then enter, which is what I'm trying to do.

Ok then, so what other stream input functions are available to you?

Or you should investigate whether or not you can change the delimiter used by >>...

Ok then, so what other stream input functions are available to you?

I can use cin.getline / get, if that is what you are referring to?

Also, I was thinking about assigning another char variable to = space and if "space" typed in then canvas = spacebar value = 32, not sure if this would work, any thoughts?

Yes you could use those functions. But if you must use the >> operator and the space character then try to investigate if you can change the delimiter used. Or another approach might be to see whether you can change the way >> works to ensure it picks up the initial whitespace char.

here is the solution

char c;
std::cin.get(&c, 2, '\n');

here is the solution

char c;
std::cin.get(&c, 2, '\n');

I wonder how people are supposed to learn anything if we just give em answers verbatim? Why not gently point people in the right direction and let them find the solution by themselves?

But if you must use the >> operator and the space character then try to investigate if you can change the delimiter used.

I'm familiar with the >> but it doesn't have to be my only choice. And I have never heard or learned of a delimiter before so I won't be able to use it either.

here is the solution

char c;
std::cin.get(&c, 2, '\n');

Thanks, but I won't be able to use it since I was never taught the std:: or &.

I wonder how people are supposed to learn anything if we just give em answers verbatim? Why not gently point people in the right direction and let them find the solution by themselves?

this is the forum in which we try to solve problem of those people who shows effort. I did that nothing else. dont wonder.

I'm familiar with the >> but it doesn't have to be my only choice. And I have never heard or learned of a delimiter before so I won't be able to use it either.


Thanks, but I won't be able to use it since I was never taught the std:: or &.

you must have been taught how to use a

using

statement.

eg. using namespace std;

the usage std::cin just explicitly says that you are going to use the cin in the std namespace rather than making available all the members of the std namespace in one go, as per the

using namespace std;

this is the forum in which we try to solve problem of those people who shows effort. I did that nothing else. dont wonder.

By giving a direct answer? OP was almost there himself anyway. Probably would have been more productive from his point of view to let him get the final bit himself.

Thanks, but I won't be able to use it since I was never taught the std:: or &.

std is namespace in which cin is defied. If you specify

using namespace std

you dont need to write std:: .
See the signature of get function. For & do some googling.

you must have been taught how to use a using statement.
eg. using namespace std;

Yes only taught this using statement though

Or another approach might be to see whether you can change the way >> works to ensure it picks up the initial whitespace char.

Thanks for this, just used noskipws to detect it.

std is namespace in which cin is defied. If you specify

using namespace std

you dont need to write std:: .
See the signature of get function. For & do some googling.

Thanks for your help as well, I'll try this way as well. I'll read into get function and &, should also I consider getline function as well, or is get more suitable

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.