Archived

This topic is now archived and is closed to further replies.

Limiting input in console(using cin)

This topic is 5127 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. Im trying to limit input for the user. The user can only input 1 character if its over 1 character the function does something else. Heres the function
void Input(char c[1]) {

    if (c[1] > 1)  cout <<"to big"<<endl; 
     else cout <<"fine"<<endl;
	
}
this function works fine if i pass a character into it. Well actually a string I guess. I pass say "a" into it.(i cant pass 'a' into it though). Anyway It works and says fine. I pass "aa" into and it says to big. Anyway I want to get input from the user like this: cin >>choice[1]; but now i want to pass that array into the function. I can do it if I say something like char *a = &choice[1]; by passing a pointer the a into it. But I think that just compares memory addresses and it doesnt work. So anyway im just wandering is there an easier way about limiting the input with cin or away to make this function work. Thanks for any help. [edited by - Arch_DOA on November 28, 2003 7:14:20 PM]

Share this post


Link to post
Share on other sites
I have no idea what you are trying to do, but if you want to vary control depending on the input length, just have a temporary buffer large enough to fit any string the user would enter, say char buffer[100], or something, read the input into here, check the length up to /0 with strlen(), and go from there. If another function takes this array, pass it as a pointer, i.e. char* buffer, you can reference elements as buffer[0] and so on.

Wizza Wuzza?

Share this post


Link to post
Share on other sites
you can use cin.width(n) to limit the number of chars accepted from the istream:


#include <iostream>

using namespace std;

int main(int, char *[])
{
char n[4];

cout << "Enter something for testing: ";

//don''t forget about null-terminator

cin.width(4);
cin >> n; //user enters some long num here eg. 23755

cout << "n is " << n << endl;

return 0;
}//main



output:


Enter something for testing: 23755
n is 237






--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites
Hey thanks man. That worked. Sortof I guess anyway

I had to set it to a high number though. I think im confused on how it worked. It limits input up to that number but after that number it keeps storing it?..I know that sounds sortof confusing but I had my array char choice[4]; i put cin.width(3) and cin>>choice; . It accepted the 1st letter like i wanted but then accepted the 3''rd letter to. Its wierd. But I put it to 100 so It only accepted bascially the first unless you entered 100 characters. Im not sure why this worked. But anyway heres the code i''ve been working on. If anybody explain''s why the width works like that. Thanks for any help


#include <iostream>

using namespace std;

void GetLetter();
void DrawBoard();
void DoGame();
void AiMove();

char gameboard[3][3] = {
{ ''a'',''b'',''c'' },
{ ''d'',''e'',''f'' },
{ ''g'',''h'',''i'' } };

char player;
char cpu;
char choice[4];

int main() {

cout <<"Tic Tac Toe..Press enter to start ''z'' quits the game"<<endl;
cin.get();
GetLetter();
system("CLS");
cin.get();
DrawBoard();
DoGame();

cin.get();

return 0;
}

void GetLetter() {

bool done = false;

while (!done) {

cout <<"X''s or O''s : ";
cin >>player;

if (player == ''o'' || player == ''x'' || player == ''O'' || player == ''X'') done = true;
else done = false;
}

if (player == ''o'' || player == ''O'') { cpu = ''X''; player = ''O''; }
if (player == ''x'' || player == ''X'') { cpu = ''O''; player = ''X''; }
}

void DrawBoard() {

cout <<endl;

for (int a = 0; a < 3; a++) {

for (int b = 0; b < 3; b++) {

cout <<" "<<gameboard[a][b];

if (b < 2) cout <<"|";
}

if (a < 2 ) cout <<endl<<" --------"<<endl;

}

}

void DoGame() {

while(choice[0] != ''z'' || choice[0] != ''Z'') {

cout <<endl<<endl;

if (player == ''O'')
cout <<"Enter a letter to put an "<<player<<" on the game board: ";
if (player == ''X'')
cout <<"Enter a letter to put a "<<player<<" on the game board: ";

cin.width(100);
cin >>choice;

switch (choice[0]) {

case ''a'':
gameboard[0][0] = player;
break;
case ''b'':
gameboard[0][1] = player;
break;
case ''c'':
gameboard[0][2] = player;
break;
case ''d'':
gameboard[1][0] = player;
break;
case ''e'':
gameboard[1][1] = player;
break;
case ''f'':
gameboard[1][2] = player;
break;
case ''g'':
gameboard[2][0] = player;
break;
case ''h'':
gameboard[2][1] = player;
break;
case ''i'':
gameboard[2][2] = player;
break;
//case ''z'':

// exit(0);

default :
break;

}
system("CLS");
AiMove();
DrawBoard();

}

}

void AiMove() {


}






Share this post


Link to post
Share on other sites