Actually, If the user enters a string, your value will be assigned the ascii value of the first character. The next cin you try will grab the next character, and so on.
I did that but the program freak out,
This is the whole code:
If you want to check you will see than all work until you wrote a letter. I can not do something like :
If keyascii = 30 to 39 then
//do the code
else
//do nothing
???
Actually, If the user enters a string, your value will be assigned the ascii value of the first character. The next cin you try will grab the next character, and so on.
No. The value is still 0.
Code:
int input;
cin >> input;
cout<<input<<endl;
//printed that input is 0
I once created a user input library. It's attached; replace lvp\string.h with apstring.h (get it at http://www.collegeboard.com and search for the Computer Science AP test). I think it needs a Borland compiler.
int UserProofInt(const String &Prompt)
// Inputs an integer from the user using Prompt as the prompt. Different
// from cin is that this function uses a slew of debugging techniques to make
// sure that the user really did enter a valid C++ integer.
{
GetNewInput:
cout << Prompt << ' ';
String Input;
cin >> Input;
for (int pos=0; pos<Input.length(); pos++) {
// isdigit(char) function returns true if char is a digit
if (!isdigit(Input[pos]) && Input[pos]!='-') {
cout << '\"' << Input << "\" is not an integer!" << endl;
goto GetNewInput;
}
if (pos!=0 && Input[pos]=='-') {
cout << "Invalid placement of negation sign!" << endl;
goto GetNewInput;
}
}
if (Input[0]!='-') {
if (Input.length()>5) {
cout << "That number exceeds integer bounds!" << endl;
goto GetNewInput;
}
}
else {
if (Input.length()>6) {
cout << "That number exceeds integer bounds!" << endl;
goto GetNewInput;
}
}
long ToReturn=atol(Input.c_str());
if (ToReturn<(-32767) || ToReturn>32767) {
cout << "That number exceeds integer bounds!" << endl;
goto GetNewInput;
}
// If execution reaches here, the user entered an integer
// Type-cast it into an int before returning!
return int(ToReturn);
}
I know it uses the evil goto, but I wrote it two years ago.
Last edited by filburt1; Sep 29th, 2001 at 11:52 AM.
[code]
int UserProofInt(const String &Prompt) // WHY the : &
{
GetNewInput:
cout << Prompt << ' '; // WHY the ' '
String Input;
cin >> Input;
for (int pos=0; pos<Input.length(); pos++) { /*You are checking if the string input have any charater on it, if not he add one ? */
// isdigit(char) function returns true if char is a digit
if (!isdigit(Input[pos]) && Input[pos]!='-') //I undestand nothing here
{
cout << '\"' << Input << "\" is not an integer!" << endl;
goto GetNewInput; //go to in the function who you are already?
}
if (pos!=0 && Input[pos]=='-') { //Why ' ' and why [pos]
cout << "Invalid placement of negation sign!" << endl;
goto GetNewInput;
}
}
if (Input[0]!='-') {
if (Input.length()>5) {
cout << "That number exceeds integer bounds!" << endl;
goto GetNewInput;
}
}
else {
if (Input.length()>6) {
cout << "That number exceeds integer bounds!" << endl;
goto GetNewInput;
}
}
long ToReturn=atol(Input.c_str()); //WHOA what is that
if (ToReturn<(-32767) || ToReturn>32767) {
cout << "That number exceeds integer bounds!" << endl;
goto GetNewInput;
}
// If execution reaches here, the user entered an integer
// Type-cast it into an int before returning!
return int(ToReturn);
}
[/code¸]
The first comment (why the &): const type variable can be used to pass a reference pointer to an object if the object doesn't need to be modified. This just saves memory and doesn't make a local copy of the object.
The red block parses through the string and makes sure that the user only entered digits or a - sign. The goto statement re-asks the user for an integer if they violated these rules.
The "why '' and []" comment: stringname[x] is like Mid(stringname, x, 1) in Visual Basic
The atol thing is a standard C function that converts a C-string to a long.