Thread: Had somee trouble with the reject integer cod......

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    41

    Had somee trouble with the reject integer cod......

    I used the function in the FAQ to reject a non integer input when
    an integer is required:

    Code:
    while(iINum<1||iINum>4)
    {
    InvalidInputChar("How many? (1-4)\n",iINum);
    if(iINum<1||iINum>4)
    {
    cout<<"\nInvalid input"<<endl;
    }
    
    
    
    //Reject NON-INTEGER input
    void InvalidInputChar(string arg1,int arg2)
    {
    while (cout<<arg1 && !(cin>>arg2))
    {
    cout<<"\nInvalid input"<<endl;
    cin.clear();
    cin.ignore(std::numeric_limits<int>::max(),'\n');
    }
    iINum=arg2;
    }
    As you can see, the top snippet calls the function at the bottom. The
    point of this code is to force the user to enter an integer between
    1 and 4, inclusive. I have to go through the function to weed out
    a non integer input. The trouble is, it also reset arg2 to zero, no matter what the user enters. Since I am a novice, I admit that I do not know how the
    two cin calls work. I removed both of them individually and had the
    same problem. Since iINum is the argument, I thought it made sense
    that whatever was inserted in the function call would stay there, but
    that is not so. When the resulting iINum is fed through the
    [if iINum<1||iINum>4] snippet, the result was always "invalid input".
    After about an hour of work I figured out that the only thing I
    needed to do was assign iINum to arg2, essentially telling it to equal
    itself, since iINum IS arg2. I tried using a third argument, having
    iINum equal it, and still the result was zero. Only when I had
    something equal arg2 within the function did that something maintain
    a nonzero value. However, when I had iINum equal arg3 (as a dummy
    variable) externally, it was zero. I do not understand why having
    iINum equal itself solved this problem.

    Perhaps somebody else has had this or a similar problem and knows why
    it happened? Is there a better way to accomplish this function? I
    would appreciate any help.

  2. #2
    Registered User
    Join Date
    Sep 2005
    Posts
    41
    I forgot to mention that iINum was initalized to zero before main(), as a global variable.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your second parameter, arg2, is a copy of whatever is passed to it. That means that any changes made to it do not affect the variable that you used to pass the value to the function. So when you pass iINum to InvalidInputChar, it makes a copy of the value and stores it in arg2. It then fills arg2 with the input from the user, but iINum doesn't change. That is why it worked only when you set iINum equal to arg2 at the end of the function.

    There are several possible fixes. The first is to return the number you found. So return int instead of void, and you don't even need the second parameter. Just assign iINum to the return value of the function when you call it.

    The second possible fix is to make the arg2 parameter a reference. If you make it a reference, then instead of a copy of the value being made, arg2 refers to the same variable that is passed to the function, in this case iINum. If you make it a reference, then iINum will automatically be updated like you expected and you won't need the last line in the function.

    The third possible method is how you did it. This method uses the fact the iINum is global and directly assigns the value to it. This is not a good way of doing things. One of the first two methods is preferred. I would choose the first method. Take the string as a parameter and return the value you get. Get rid of the second parameter and make arg2 a local variable to the InvalidInputChar function, then return it when the input is valid.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM