CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2008
    Posts
    9

    problems with input validation

    Hi,

    I've written the following function and am having trouble with input validation so that my program displays:

    Score 1:
    Score 2:
    Score 3:
    Score 4:
    Score 5:

    for the user to enter a score between 1 and 10. If a number other than 1-10 are entered, it should repeat the previous statement until an appropriate number is entered.

    Here is my code. This is for an assignment, so I'm just looking for any comments as to where I might have gone wrong so that I can get back on the right track.

    Thank you.

    Code:
    float getScores(float scores[], int JUDGES)
    {
        int count = 0;
    	
    	do
    	{
    		for(count = 0; count < JUDGES; count++)
    		{
    			cout << "Score " << (count + 1) << ": ";
    			cin >> scores[count];
    		}
    	}while (scores[count] > 0.0 && scores[count] <= 10.0);
    	return scores[];
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: problems with input validation

    What is this line supposed to do?
    Code:
    	return scores[];
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Nov 2008
    Posts
    9

    Re: problems with input validation

    Hi,

    That line was supposed to return the array....I did originally have return scores[count]; so that the scores would be returned and later displayed on the screen through another function.

    Cordelia

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: problems with input validation

    Quote Originally Posted by cordelia View Post
    Hi,

    That line was supposed to return the array...
    Which it doesn't do. It shouldn't even compile. In C++, you cannot return an "array" like that. You can't return arrays at all, so your problem goes much further than validating input.

    Also, you don't pass arrays, so this:
    Code:
    float getScores(float scores[], int JUDGES)
    is the same as this:
    Code:
    float getScores(float* scores, int JUDGES)
    What is really being passed is a pointer to the first argument of the array.

    First, you declared the return value as "float". This only returns a single value.

    Second, if you want to simulate returning an array, there are at least 4 ways to do it.

    1) Declare a pointer to float, dynamically allocate memory, and return that.
    2) Use std::vector<float> and return that.
    3) Create a struct that has an array as a member and return that.
    4) Have the user pass the buffer, and your function fills it in, and return that.

    Number 1) leads to memory leaks and hard to maintain. Number 3) is not used to often, but can work. Number 2) should be the first choice if you really want to return an array or array-like sequence of values. Number 4) is the one you're trying to do, but not doing it correctly:
    Code:
    float* getScores(float* scores, int JUDGES)
    {
    //....
        return scores;
    }
    Regards,

    Paul McKenzie

Tags for this Thread

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

Featured