I want the use be able to use only number in : cin>>input; How can I do that?
Printable View
I want the use be able to use only number in : cin>>input; How can I do that?
Just use it like CornedBee said. If the user enters a string the var will recieve the value 0.
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.
Z.
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
???
Thx in advanceCode:#include <iostream.h>
//'Declaration
double addit(double x,double y);
double soust(double x,double y);
double multi(double x,double y);
double divide(double x,double y);
double x=0;
double y=0;
double msgexit=1;
//'Code
int main()
{
do{
int input;
cout<<"\n:::Menu:::"<<endl<<endl;
cout<<"1. Add function"<<endl;
cout<<"2. Soustract function"<<endl;
cout<<"3. Multi function"<<endl;
cout<<"4. Divide function"<<endl;
cout<<"5. Exit"<<endl;
cout<<"Your choice is : ";
cin>>input;
if (input==5)
return 0;
cout<<"First number: ";
cin>>x;
cout<<"Second number: ";
cin>>y;
switch (input)
{
case 1:
cout<<"The answer is ---> ";
cout<<+addit(x,y);
cout<<" <---"<<endl;
break;
case 2:
cout<<"The answer is ---> ";
cout<<soust(x,y);
cout<<" <---"<<endl;
break;
case 3:
cout<<"The answer is ---> ";
cout<<multi(x,y);
cout<<" <---"<<endl;
break;
case 4:
cout<<"The answer is ---> ";
cout<<divide(x,y);
cout<<" <---"<<endl;
break;
case 5:
msgexit = 0;
//return 0;
break;
default:
cout<<"Error, bad input, quitting"<<endl;
return 0;
}
}while (msgexit == 1);
}
//'Function
double addit(double x,double y)
{
return x+y;
}
double soust(double x,double y)
{
return x-y;
}
double multi(double x,double y)
{
return x*y;
}
double divide(double x,double y)
{
return x/y;
}
Quote:
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
Vlatko you tryed the code ? if yes you saw then when I enter a letter then you press enter the code freak?
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.
Thx turtle
Ok, Vlatko, you were right, but if you define input as char, you will get the behavior i described (and oh what a pain it is, sometimes).
Z.
Well, that is right, but if you want a string then you will define a char array, not a single char.Quote:
Ok, Vlatko, you were right, but if you define input as char, you will get the behavior i described (and oh what a pain it is, sometimes).
Nevermind, we understand eachother now. ;) ;)
this is because cin>>char asks for a single character, not a number.
Replace String with apstring after you download and copy the AP classes from http://www.collegeboard.org/ap/compu...l/classes.html :
I know it uses the evil goto, but I wrote it two years ago. :)Code: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 do not understand a piece of that code. :confused:
One piece or the entire thing?
!isdigit?
goto GetNewInput? That restart the function?
&& Input[pos]!='-' ? whoa....i understand nothing there
I colored the code; do you have trouble with one specific color or should I walk you though each color?
[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¸]
all in bold is what I do not understand.
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.
Any other questions? :)
Turtle i want to tell you than you are the man! and you take all the time the time I need to understand . Thank you! I appreciate it a lots.
I bough Sams Teach Yourself C++ today and I have read 40 pages for the moment. I hope helping person, a day, like you are doing so well.
Daok
Me good. :D :D