Arrays

Hello, I am running into problems trying out an array exercise. I have the array initially set to NULL. When i try to give it a size, I can't figure out why it won't take that size. I'm also not sure if the printArray function is correct. Any help is appreciated. Below is only part of the code.

inside main routine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

                // get the desired array size and attemt to allocate it
                cout << "Please enter the desired size of the array";
                cout << endl;
                arrSize = getInteger(1, INT_MAX);
                arr = allocArray(arrSize);
                
                if (arr == NULL) {
                    // if the allocaion failed warn the user
                    cout << endl;
                    cout << "*** UNABLE TO ALLOCATE ARRAY OF SIZE ";
                    cout << arrSize << " ***" << endl << endl;
                } else {
                    cout << "Array allocation successful" << endl;
                    cout << endl;
                }




I don't think there is a problem with the following getInteger function. The getInteger function below seems to pass an integer to arrSize. But even if the integer is valid, I still get the ***unable to allocate array ...*** message.

inside getInteger function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
int getInteger(int lowerBound, int upperBound)
{
    // perform a quick correction if lowerBound > upperBound
    if (lowerBound > upperBound) {
        int tmp = lowerBound;
        lowerBound = upperBound;
        upperBound = tmp;
    }
    
    // userEntry holds input as text to allow validation
    // userInt holds the final converted/validated integer
    string userEntry;
    int userInt = 0;
    
    // prompt the user to enter their integer value,
    //     storing it as text
    cout << "Please enter an integer in the range ";
    cout << lowerBound << " to " << upperBound << endl;
    cin >> userEntry;
    
    // use atoi to onvert the value to an integer,
    //     with the knowledge that 'garbage' data
    //     gets auto-converted to 0
    userInt = atoi(userEntry.c_str());
    
    // if the converted value is 0, we need to check and
    //     see if the string entered contained non-integer
    //     data (in which case we prompt them to try again)
    if ((userInt == 0) && (userEntry !="0")) {
        // their data entry was not a valid integer
        cout << "*** " << userEntry << " is not a valid integer, ";
        cout << "please try again ***" << endl;
        userInt = getInteger(lowerBound, upperBound);
    }
    
    // once we have a valid integer data we need to check
    //     that it is within the specified bounds,
    //     prompting them to retry until valid data is given
    if ((userInt < lowerBound) || (userInt > upperBound)) {
        // their data entry was not in the specified range
        cout << "*** " << userEntry << " is not in the range ";
        cout << lowerBound << " - " << upperBound << ", ";
        cout << "please try again ***" << endl;
        userInt = getInteger(lowerBound, upperBound);
    }
    
    // return the final (validated) response
    return userInt;


the function seems to pass along an integer as it should and there are no compile errors in the code.




I am also not sure if the printArray code is correct. I haven't been able to test it because i can't get past the allocate array code.

main routine:
1
2
3
4
            case PrintCmd:
                // a request to print the current array contents
                printArray(arr, arrSize);
                break;               


the printArray function:
1
2
3
4
5
6
// printArray prints the contents of the array
void printArray(int* arr, int arrSize)
{
    cout << arr[arrSize] << endl;
}


thank you :)
Topic archived. No new replies allowed.