Results 1 to 21 of 21

Thread: Number only

  1. #1
    DaoK
    Guest

    Number only

    I want the use be able to use only number in : cin>>input; How can I do that?

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Just use it like CornedBee said. If the user enters a string the var will recieve the value 0.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Zaei
    Guest
    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.

  4. #4
    DaoK
    Guest
    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
    ???

    Code:
    #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;
    }
    Thx in advance

  5. #5
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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 am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  6. #6
    DaoK
    Guest
    Vlatko you tryed the code ? if yes you saw then when I enter a letter then you press enter the code freak?

  7. #7
    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.
    Attached Files Attached Files

  8. #8
    DaoK
    Guest
    Thx turtle

  9. #9
    Zaei
    Guest
    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.

  10. #10
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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).
    Well, that is right, but if you want a string then you will define a char array, not a single char.

    Nevermind, we understand eachother now.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    this is because cin>>char asks for a single character, not a number.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Replace String with apstring after you download and copy the AP classes from http://www.collegeboard.org/ap/compu...l/classes.html :
    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 know it uses the evil goto, but I wrote it two years ago.
    Last edited by filburt1; Sep 29th, 2001 at 11:52 AM.

  13. #13
    DaoK
    Guest
    I do not understand a piece of that code.

  14. #14
    One piece or the entire thing?

  15. #15
    DaoK
    Guest
    !isdigit?
    goto GetNewInput? That restart the function?
    && Input[pos]!='-' ? whoa....i understand nothing there

  16. #16
    I colored the code; do you have trouble with one specific color or should I walk you though each color?

  17. #17
    DaoK
    Guest
    [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¸]

  18. #18
    DaoK
    Guest
    all in bold is what I do not understand.

  19. #19
    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?

  20. #20
    DaoK
    Guest
    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

  21. #21

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width