-3

I'm working on a project that required me to input 5 numbers from 20-101 into my one-dimensinal array. After that, it shows up output of those 5 numbers. If enter a non-numeric/float number/duplicate/out-range, reject those and ask user to input again. I have to do try and catch into my codes. Also, the program able to shows up the bad input at the end. Here what I got so far and got stuck, I'm totally lost.

Check out-range numbers

int ArrayReader::checkInput(int number)
{
    if (number >= 20 && number <= 101)
    {
        return number;
    }
    else
    {
        throw invalid_argument("Out-range-number entered");
    }

}

Main Code

#include <iostream>
#include <stdexcept>
#include <iomanip>
#include "ArrayReader.h"
using namespace std;

int main()
{
    ArrayReader value;
    int number = 0;
    int dup;
    int Array[5];
    int currentArray = 0;
    cout << "Enter Number from 20 - 101: ";
    for (int i = 0; i < 5; i++)
    {
        cin >> number;
        dup = 0;
        if (number >= 20 && number <= 101)
        {
            for (int value = 0; value < currentArray; value++)
            {
                if (number == Array[value])
                {
                    dup == true;
                    dup = 1;
                    cout << "Number " << number << " already used. Try again";
                    break;
                }
            }
            if (dup == false)
            {
                Array[currentArray++] = number;
                i++;
            }
        }
        else
        {
            try
            {
                int result = value.checkInput(number);
                cout << "Valid Input: " << number << endl;
            }
            catch (invalid_argument &x)
            {
                cout << "Error: "
                    << x.what() << endl;
            }
        }
    }
    for (int i = 0; i < 5; i++)
    {
        cout << "Valid numbers entered" << " " << Array[i] << endl;
    }
}

My problems are:

  1. For now, when I run the program, it doesn't let me enter all 5 numbers (only 3). It checked duplicated numbers and out-range number. The output is chain of weird numbers

  2. I have no ideas how to do reject non-numeric input and float numbers.

I'm beginner of C++, please help me!

Jin Nguyen
  • 45
  • 10
  • Problem 1, you're checking `number` before assigning a value to it (other than the initialization of zero). So, you will jump into the `else` block right away with `number` equal to zero. – DigitalNinja Apr 09 '15 at 18:35
  • Maybe [this Q&A](http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it) is giving you some hints, how to do it. – πάντα ῥεῖ Apr 09 '15 at 18:37
  • Can you guys please explain to me right on the code please? I'm new and it's extremely hard to me for just saying like that. Please! Thank you! – Jin Nguyen Apr 09 '15 at 18:42
  • The trouble is there are more than a couple of problems with the code. It's hard to point everything out and explain why it's wrong on here without writing the whole thing for you. – DigitalNinja Apr 09 '15 at 18:51

3 Answers3

0

change you function like this for debug:

int ArrayReader::checkInput(const int& number)
{
     cout << '\n checkInput number: ' << number << '\n';

    if (number >= 20 && number <= 101)
    {
        return number;
    }
    else
    {
        throw invalid_argument("Out-range-number entered");
    }
  return -1;
}
Michael Popovich
  • 301
  • 1
  • 10
0

You can use cin.fail() to check for valid integers

int input;
cin >> input;

while(!cin.fail())
{
    cin.clear();
    cin.ignore(256,'\n');
    cin.input();
}

Thats just one part of your assignment. You dont assign any values to number after the initial int number = 0. You have variables with the same name ArrayReader value and for(int value = 0;...).

This is incorrect too

dup == true;  // equality

These are just some errors I could see by glancing at the code. Check using a debugger whether the variables are assigned values as you expect.

sam
  • 2,033
  • 2
  • 10
  • 13
0

I changed your code a little. First I should say the following code must be improved.

#include <iostream>
using namespace std;

int main()
{
    int number = 0;
    bool isDuplicate = false;
    int Array[5];
    int arraySize = 0;

    cout << "Enter Number from 20 - 101: ";

    while(arraySize < 5)
    {
        try{
            cin >> number;

            isDuplicate = false;

            if (number >= 20 && number <= 101)
            {
                for (int j = 0; j < arraySize; j++)
                {
                    if (number == Array[j])
                    {
                        isDuplicate = true;
                        throw "Number is already used. Try again";                      
                    }
                }
                if (isDuplicate == false)
                {
                    Array[arraySize] = number;
                    arraySize++;
                }
            }
            else
            {
                throw "Error";
            }
        }
        catch (char* err)
        {
            cout << err << endl;
        }
    }

    for (int i = 0; i < 5; i++)
    {
        cout << "Valid numbers entered" << " " << Array[i] << endl;
    }

    char c;
    cin >> c;
}
  • arraySize holds the information of how many number is exist in the array (valid numbers).
  • while is similar to for loop. for(int i = 0, i < 5; i++) equals the

int i = 0; while( i < 5 ){ ... i++ }

  • Inside the try block you tries the your code. If there is an exception (error) you can throw it to catch block. In this example when there is a duplicate number or a number which is outside the limit it sends an error message to the catch block. Then catch block writes the error message.
ahmet
  • 61
  • 1
  • 8