Thread: Experts please help a student! Basic C++ question.

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    Experts please help a student! Basic C++ question.

    this is a part of my programme

    Code:
    class Impedence
    {
    protected:
        int R1, R2, X1, X2;   
    public:
    	void enterNum(void);
    };
    void Impedence::enterNum(void)
    {
    
            cout<<"Enter real part of impendence A: ";
    	cin>>R1;
            cout<<"Enter imaginary part of impedence A: ";
    	cin>>X1;
    	cout<<"Enter real part of impedence B: ";
    	cin>>R2;
    	cout<<"Enter imaginary part of impedence B: ";
    	cin>>X2;
    }
    how do i modify it such that the user can only enter positive integer?
    invalid input will stop the next statement and return to the first.
    for eg. if the user entered alphabet "a" or any other alphabet it will show "Please enter an integer value!" the programme will then loop back to the first cout.
    Last edited by goodgene; 08-13-2011 at 10:17 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Read your book on do...while loops perhaps?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you are worried about people typing letters instead of numbers, then you need to look at methods for getting rid of input (>> will absorb -1 and allow you to get rid of it, but a will stay forever). You can either read into a string and then parse the string, or you can check whether the input stream is in a fail state using fail() and then clear the input stream.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It may also be better, in this case, to read a whole line of input into a string (using istream's getline() method), interpret the input to see if it is valid, and only then extract the values needed

    Also, if you want to input a positive value, why not store it in an unsigned type?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Darcs experts: question about conflicts
    By Kempelen in forum Tech Board
    Replies: 1
    Last Post: 05-05-2009, 08:36 AM
  2. Question for Prolog Experts
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-03-2009, 03:59 AM
  3. Student Question
    By ladysniper in forum C++ Programming
    Replies: 5
    Last Post: 02-26-2006, 10:22 PM
  4. a question for the experts
    By ahmed_alzahrani in forum C Programming
    Replies: 8
    Last Post: 05-20-2005, 08:42 PM
  5. Question For C Experts
    By Troll_King in forum C Programming
    Replies: 11
    Last Post: 07-29-2002, 05:30 AM