• 0

c++ input screening


Question

im trying to write a fairly simple program where the computer randomly chooses a number between 1-100, and the user trys to guess it in 7 go's. I can do it alright, but if i accidently entered a letter instead of a number for my guess, it went completly spastic. I need help trying to make sure thta the user does enter a number, and if they dont, i want to tell them so and ask them to try again.

I have tried this using arrays of characters, and checking each character for its decimal value... but it doesnt seem to work, and my guesses are always somehow made to equal some werid number, normally about 4950, even if i only input 3.

My code for this is here:

int g_input_guess()

{

char guess[4];

int a=0,b=0,c=0,z=0;

cin >> guess;

for(int i = 0;i<4;i++)

{

cout << "guess i is: " << guess; // this outputs some weird stuff... like nothing!! so im not sure, it appears

//that the guess doesnt actually get put in the array or something.

if(guess==113)

{ return -1; }

if(guess>58&&guess<48)

{ cout << "Please enter a real number!\n"; return -2; }

}//end for

a = guess[0]*100;

b = guess[1]*10;

c = guess[2]*1;

z = a+b+c;

return z;

}

any help would be *very* appreciated. Thanks

Link to comment
https://www.neowin.net/forum/topic/475694-c-input-screening/
Share on other sites

8 answers to this question

Recommended Posts

  • 0
  Quote
cin >> guess;

for(int i = 0;i<4;i++)

{

cout << "guess i is: " << guess; // this outputs some weird stuff... like nothing!! so im not sure, it appears

//that the guess doesnt actually get put in the array or something.

nothing is going into the array cause you need to do cin>>guess[some number in here or just 'i' if you're gonna do a for loop]

  • 0

i tried that, both inside and outside the for() loop.

The problem is, i want to be able to check each key pressed by the user, to check if it is actually a number. There is a function, called get() i belive, that works for borland turbo c++... but not the standard c++ code im writing in. My biggest problem is that the cin >> array[] thing only inputs until the return key is pressed, and so i cant check each key *as* they are pressed. If i could find a way to do that, my problem would be solved...

  • 0
  Ianbobisveck said:

int g_input_guess()

{

char guess[4];

int a=0,b=0,c=0,z=0;

cin >> guess;

for(int i = 0;i<4;i++)

{

cout << "guess i is: " << guess; // this outputs some weird stuff... like nothing!! so im not sure, it appears

//that the guess doesnt actually get put in the array or something.

if(guess==113)

{ return -1; }

if(guess>58&&guess<48)

{ cout << "Please enter a real number!\n"; return -2; }

}//end for

a = guess[0]*100;

b = guess[1]*10;

c = guess[2]*1;

z = a+b+c;

return z;

}

any help would be *very* appreciated. Thanks

Okay, I don't know C++, but, i can tell you what is wrong with your code.

first of all, you're cycling through each element of the array, even if the user only enterd a single digit. after using the debugger (you need to know how to use this if you don't) i found that after the input is placed in the array, it is followed by NULL, or 0. so if the user entered just the number 3, your char array would look like this:

guess[0] = '3'

guess[1] = 0

junk in the rest.

so what you need to do is check for a value of 0 and break out of the loop when you find it.

also, this statement in incorrect:

f(guess[i]&gt;58&amp;&amp;guess[i]&lt;48)
{ cout &lt;&lt; "Please enter a real number!\n"; return -2; }
}//end for

it should be >58 or < 48. how can a number be >58 and <48 at the same time?

lastly, your conversion to an integer will only work if they entered 3 characters, and even if they do that it still won't work properly. you are multiplying the ascii value by 100, 10 and 1 instead of the integer value of the input. so what you're doing is '3' * 100 (=5100) instead of 3 * 100 (=300). To convert a character number to an integer subract '0' from it.

If they entered 3 characters your array woudl look like this

guess[0] = '1'

guess[1] = '2'

guess[2] = '3'

guess[3] = 0

So you would be right multiplying guess[0] by 100 and guess[1] by 10, but if they entered in 2 characters:

guess[0] = '1'

guess[1] = '2'

guess[3] = 0

Now it would be incorrect to multiply guess[0] by 100 since they only entered a 2 digit number, so you need to keep track of how many digits were entered.

so heres the code that works, but this doesn't seem like a very good way of doing this, but like I said I don't know C++ so I can't tell you how you should be doing it.

int g_input_guess()
{
	char guess[4];
	int a=0,b=0,c=0,z=0;
	cin &gt;&gt; guess;
	int i; //keeps track of how many chars were entered
	for(i = 0;i&lt;3;i++)
	{
		if (guess[i] == 0) //check for end of user input
		{
			break;
		}

		if(guess[i]==113)//check for 'q'
		{ return -1; 
		}
		else if(guess[i]&gt;58 || guess[i]&lt;48) //check for non-numeric value
		{ 
			cout &lt;&lt; "Please enter a real number!\n"; return -2; 
		}

	}//end for
		//make the conversion depending on how many characters were entered.
		//eg: if only 1 digit was entered, your original method would mutliply it by 100. instead of 1.
	if (i==1) 
	{
		return guess[0] - '0';
	}
	else if (i == 2)
	{
		return (guess[0] - '0') * 10 + (guess[1] - '0');
	}
	else if (i== 3){
		a = (guess[0]-'0')*100;
		b = (guess[1]-'0')*10;
		c = (guess[2]-'0')*1;
		z = a+b+c;
	}



	return z;

You'd also have to worry about what happens if the user decides to type too many characters and overflow the buffer. Hope that helps. Someone with C++ knowledge can tell you if there is a better way to go about it, but thats why it's not working.

  • 0

Also, if you need a you want to be an extremely strict ANSI coder and you need a function to convert integers to ASCII characters, the easiest way is using sprintf():

int n = 2;
char* x;
sprintf(x, "%d", n); /* 'd' specifies a signed integer */

If you are using a std::string instead, here is what it would look like:

std::string x;
int n = 122;
sprintf((char*)x.c_str(), "%d", n);
printf("%s", x.c_str());

Just a tip in case your compiler doesn't have an itoa() or _itoa() function (or you want to be as ANSI standards-compliant as possible). ;)

  • 0

Thanks heaps!!

just a query tho, about this debugger... what would you recomend using/learning? cuz i dont use one, but i have seen one being used (once), and i think it might help me... There is a link to a debugger in your post, but i wasnt sure if that was you or Neowin, and i was wondering what people use, whats good and stuff. Im using linux to compile and run this program, so it'd be good if it worked with that.

Also, what documentation do people use to learn about functions etc? I dont know how to use atoi(), and i dont know where to find out, without just googling it, and there must be a better way then randomly googleing every function i come accross.

Thanks again

  • 0
  Andareed said:

You should not modify the string returned by c_str().

You are right. That's why it is a "dirty" example. I used the language improperly to my advantage. :( Best way when using std::string :

std::string x;
char* buffer = new char();
int n = 122;
sprintf(buffer, "%d", n);
x = buffer;
delete buffer;
printf("%s", x.c_str());

  Ianbobisveck said:

Thanks heaps!!

just a query tho, about this debugger... what would you recomend using/learning? cuz i dont use one, but i have seen one being used (once), and i think it might help me... There is a link to a debugger in your post, but i wasnt sure if that was you or Neowin, and i was wondering what people use, whats good and stuff. Im using linux to compile and run this program, so it'd be good if it worked with that.

Also, what documentation do people use to learn about functions etc? I dont know how to use atoi(), and i dont know where to find out, without just googling it, and there must be a better way then randomly googleing every function i come accross.

Thanks again

Standard C libraries, along with some iostream (well-documented IMHO) - http://www.cplusplus.com/ref/

Pretty much everything including STL - http://www.cppreference.com/

Edited by gohankid77
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.