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

    Question New to c++ and getting an error for searching an Array

    I am getting an error and I can't figure out what I am doing wrong. I have compared to similar code in the text book and I am still over looking something any help with what I am missing would be appreciated!

    Error 1 error C2109: subscript requires array or pointer type 45 1


    PHP Code:
    #include "stdafx.h"
    #include <conio.h>
    #include <iostream>
    using namespace std;

    //Function prototype
    int searchList (const int [ ],  int);

    //constant
    const int NUM_ELEMS 10;

    int main()
    {
        
    // varibles
        
    int luckyNumbers [NUM_ELEMS] = {13579267912679233445555556248377777742285647,    
        
    93121};
        
        
    // call the LuckyNumbers check function
        
    searchList (luckyNumbersNUM_ELEMS);

        
    _getche ();
        return 
    0;
    }

    // ***************************************************
    // The searchList function performs a linear search on the int array  
    // luckyNumbers. If the number is with in luckyNumbers it will return  
    // message confirming the numbers are winners otherwise it  it will    
    // notify user that the numbers are not winning numbers                 
    // ***************************************************

    void searchList (int luckyNumbers[], int NUM_ELEMS)
    {
        
    int winningNumbers 0;

        
    bool found false;

        
    cout << "Enter this weeks Winning Numbers: " << endl;
        
    cin >> winningNumbers;
        for (
    int i 0NUM_ELEMSi++)
        {
            if (
    luckyNumbers[i] == winningNumbers)
            {
                
    found true;
                break;
            }
        }
        if (
    found)
        {
            
    cout << "You are a winner! " << endl
        }
        else
        { 
            
    cout << "Sorry better luck next week. /n ";
        }


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

    Re: New to c++ and getting an error for searching an Array

    The problem is using your const int NUM_ELEMS as the name of your function parameter. Call it something else, or better yet don't pass it at all as it's global.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: New to c++ and getting an error for searching an Array

    To correctly use code tags with c/c++ code rather than use PHP tags, Go Advanced, select the code and click '#'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: New to c++ and getting an error for searching an Array

    Your searchList() declaration has the const int [] as the first argument - but for the definiton it is int [].

    As the contents of the array luckyNumbers are not intended to be changed, it would be better to define the array as const.

    To output a new line within a string literal you use \n and not /n.

    Also, it would be considered a better design to split the operations of obtaining a number and searching for it into separate functions rather than having both these operations within the same function. The search function could then take as an argument the number to find and return a bool true or false which the calling function (or a message function) could then use to determine which message to display. Your search function is then more general which could be reused.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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