CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    [RESOLVED] Array Subscript Errors

    Ok, im trying to make a program that takes up to 10 letters entered by the user and if they're lowercase it will make them uppercase, but I have a problem; when I try to run the code below, I get 2 errors (highlighted in red), what's wrong with the way I'm doing this?

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int getCase(int);
    
    int main()
    {
        const int count = 11;
        int array[count], upper;
    
        cout << "Enter 10 uppercase or lowercase letters and I will make them all uppercase.\n";
        for (int num = 0; num < count; num++)
            {
                cin >> array[num];
            }
        for (int num = 0; num < count; num++)
            {
                upper[num] = getCase(array[num]);
            }
        system("CLS");
        cout << "Before: ";
        for (int num = 0; num < count; num++)
            {
                cout << static_cast<char>(array[num]);
            }
        cout << "\nAfter: ";
        for (int num = 0; num < count; num++)
            {
                cout << static_cast<char>(upper[num]);
            }
        cout << "\n\n";
        system("PAUSE");
        return 0;
    }
    
    
    int getCase(int low)
    {
        int test = low, upper;
    
        if (test > 97 && test < 122)
            {
                upper = test - 32;
            }
        else
            {
                upper = low;
            }
        return upper;
    }
    In function 'int main()':
    19. error: invalid types 'int[int]' for array subscript
    30. error: invalid types 'int[int]' for array subscript

    ||=== Build finished: 2 errors, 0 warnings ===|
    [/CODE]
    Last edited by iiSoMeGuY 7x; May 14th, 2011 at 03:58 PM. Reason: Update Code

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Array Subscript Errors

    Code:
    int array, upper;
    array isn't an array and even if it was this
    Code:
    for (int num = 0; num <= count; num++)
    would probably also be wrong (index out of bounds).
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: Array Subscript Errors

    Quote Originally Posted by S_M_A View Post
    Code:
    int array, upper;
    array isn't an array and even if it was this
    Code:
    for (int num = 0; num <= count; num++)
    would probably also be wrong (index out of bounds).
    how is the index out of bounds?

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

    Re: Array Subscript Errors

    Quote Originally Posted by iiSoMeGuY 7x View Post
    how is the index out of bounds?
    Declare an array first, and then use it in your program. Once you do that, we'll let you know if and why the index is out of bounds.

    Regards,

    Paul McKenzie

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Array Subscript Errors

    Quote Originally Posted by iiSoMeGuY 7x View Post
    how is the index out of bounds?
    There is no array in the code you posted.

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Array Subscript Errors

    Well, it looks like you half corrected the code. You only declare one array, but are accessing the variables as if they are both arrays.

    Viggy

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