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

    reading input between 1 and 100

    Hello,
    Am trying to get the user to input a series of numbers that will be store into an array. My range is between 1 and 100, anything over that or under can't be accepted and the user must know. Occurs any number that are not in range can't be included in the array. Now my problem is that I want to end the input by entering a blank line which is conflicting with my range.

    here is my code

    Code:
    int choiceRead(int num1[], int size)
    {
        int count=0;
        char input[100];
        
        //this will be the keyboard input of the numbers you want to use to create the chart and the statistics
        cout << "Please enter a data between 1 and 100 when your done please press enter: \n";
        while ( cin.getline(input, 100) )
        {
            if (atoi(input) > 100 || atoi(input) <= 0)
            {
                    cout << "you enter a number out-of-range.Please try again: ";
                    continue;
            }
            else
            {
            num1[count++] = atoi(input);
            if ( count >= size ) 
                break;
            if (count > 100 || count < 0) 
                break;
            if ( strlen(input) == 0 )  // This end the loop that ask for numbers
                break;
            }
        }
    
    
        return count - 1; // this will remove the last count because it will be read as "Zero"
    }

  2. #2
    Join Date
    Dec 2008
    Posts
    56

    Re: reading input between 1 and 100

    You are writing code in C++. You should avoid using a C-style array for storing values; use an std::vector instead. You do not need to keep track of its size, and thus is safer to use (there is no need to worry about overwriting the buffer as with a regular array). The vector also provides the API to query its size.

    Here's a simple program that uses a vector:
    Code:
    #include <vector>
    #include <string>
    #include <algorithm>
    #include <iostream>
    #include <cstdlib>    // for atoi()
    
    
    void choiceRead(std::vector<int>& values, const size_t size)
    {
      while (values.size() < size)
      {
        std::string input;
    
        std::cout << "Enter a number between 1 and 100, or a blank line when done: ";
        getline(std::cin, input);
    
        // check if a blank line was entered...
        if (input.size() == 0) break;
    
        // convert input to a numeric value
        const int val = atoi(input.c_str());
    
        // is the inputted value acceptable?
        if (val < 1 || val > 100)
        {
          std::cout << "Sorry, that value is not valid; try again..." << std::endl;
        }
        else
        {
          // the inputted value is valid; let's store it...
          values.push_back(val);
        }
      }
    }
    
    void display(const int& value)
    {
      std::cout << value << std::endl;
    }
    
    int main()
    {
      std::vector<int> values;
    
      // read 5 values (this number was arbitrarily chosen)
      choiceRead(values, 5);
    
      // print results
      std::for_each(values.begin(), values.end(), display);
    }
    Another way to convert a string containing a numeric value is to use std::stringstream. In the example above, it seemed like overkill to use this approach, thus I used the atoi() that you are familiar with.
    Last edited by dwhitney67; April 3rd, 2009 at 10:15 PM.

  3. #3
    Join Date
    Mar 2009
    Posts
    26

    Re: reading input between 1 and 100

    I would love to use vector which I did learn about in my class, but for this project our teacher wants us to try it this way first without vectors or 2d array or any other type of array that we have not learn yet.
    Last edited by vladic256; April 4th, 2009 at 09:53 AM.

  4. #4
    Join Date
    Dec 2008
    Posts
    56

    Re: reading input between 1 and 100

    Quote Originally Posted by vladic256 View Post
    I would love to use vector which I did learn about in my class, but for this project our teacher wants us to try it this way first without vectors or 2d array or any other type of array that we have not learn yet.
    I figured this was for a class, and thus I provided a semi-advanced example. Take the code I wrote, an in lieu of the vector, pass the array as you had done. Within the choiceRead() function, declare a 'count' variable similar to what you did in the code presented in the OP, and initialize it to zero. Each time a value is added to the array, increment 'count' by 1 (once again, this is similar to what you did previously). The conditional for the while-loop should check to ensure 'count' is less than 'size'.

    When the while-loop exits, either due to 'count' being equal to 'size' or the user entering a blank line, return 'count'; the value should be used in the main() function so that you know how many numbers were added to the array.

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