7 Replies - 2493 Views - Last Post: 31 July 2009 - 01:46 PM Rate Topic: -----

#1 codefreak.  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 79
  • Joined: 11-July 09

[C++] How Can I Set The Input Buffer Size?

Posted 31 July 2009 - 11:44 AM

How can I set the input buffer size?

I want to set the input buffer to allow only one character to be entered.

#include <iostream>
using namespace std;

int main()
{
	char ch;

	cout << "Do you want to do this? ";
	cin >> ch;

	if (toupper(ch) == 'Y')
		cout << "Yes" << endl;
	else if (toupper(ch) == 'N')
		cout << "No" << endl;

	return 0;
}


This post has been edited by codefreak.: 31 July 2009 - 11:49 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [C++] How Can I Set The Input Buffer Size?

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: [C++] How Can I Set The Input Buffer Size?

Posted 31 July 2009 - 12:02 PM

Use cin.get(ch);

It gets the first key pressed. I doesn't require the user to click enter.
Was This Post Helpful? 0
  • +
  • -

#3 codefreak.  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 79
  • Joined: 11-July 09

Re: [C++] How Can I Set The Input Buffer Size?

Posted 31 July 2009 - 12:15 PM

No, I want to make it so that only one character can be entered.

For example, a yes no question, you press y, then press n it will change y into n.

Like those y/n questions you see...

This post has been edited by codefreak.: 31 July 2009 - 12:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: [C++] How Can I Set The Input Buffer Size?

Posted 31 July 2009 - 12:24 PM

That would have to be done by using some sort of gotoxy function and creating an optical illusion.

Here is a snippet by GabeHabe for a gotoxy function:
http://www.dreaminco...snippet1980.htm

Then you would have to use getch from conio.h to capture the letter but not display it until you want it to.
Was This Post Helpful? 0
  • +
  • -

#5 codefreak.  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 79
  • Joined: 11-July 09

Re: [C++] How Can I Set The Input Buffer Size?

Posted 31 July 2009 - 12:29 PM

I didn't realise it was that complicated. Nevermind, and by the way cin.get(ch); is the same as cin >> ch;.

This post has been edited by codefreak.: 31 July 2009 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: [C++] How Can I Set The Input Buffer Size?

Posted 31 July 2009 - 01:21 PM

View Postcodefreak., on 31 Jul, 2009 - 06:29 PM, said:

I didn't realise it was that complicated. Nevermind, and by the way cin.get(ch); is the same as cin >> ch;.

not quite the same, cin.get(ch) will read white space characters but cin >> ch will skip them so long as the ios::skipws flag is set (the default)
Was This Post Helpful? 0
  • +
  • -

#7 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: [C++] How Can I Set The Input Buffer Size?

Posted 31 July 2009 - 01:33 PM

View Posteker676, on 31 Jul, 2009 - 06:24 PM, said:

That would have to be done by using some sort of gotoxy function and creating an optical illusion.

Here is a snippet by GabeHabe for a gotoxy function:
http://www.dreaminco...snippet1980.htm

Then you would have to use getch from conio.h to capture the letter but not display it until you want it to.

if you are using conio.h and simply want to read y or n to set some internal flag you could use the backspace character '\x8' to overwrite the previous character, e.g. this reads a character which is overwritten when you type the next one etc
while(1)
{
	char ch=getche();
	cout.put('\x8');
}


you could always add some check to make sure the character read is a y or n

This post has been edited by horace: 31 July 2009 - 01:33 PM

Was This Post Helpful? 0
  • +
  • -

#8 codefreak.  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 79
  • Joined: 11-July 09

Re: [C++] How Can I Set The Input Buffer Size?

Posted 31 July 2009 - 01:46 PM

View Posthorace, on 31 Jul, 2009 - 07:21 PM, said:

View Postcodefreak., on 31 Jul, 2009 - 06:29 PM, said:

I didn't realise it was that complicated. Nevermind, and by the way cin.get(ch); is the same as cin >> ch;.

not quite the same, cin.get(ch) will read white space characters but cin >> ch will skip them so long as the ios::skipws flag is set (the default)


Thank you, I didn't know that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1