-2

I am trying to make this code work on Visual Studio 2022, but it tells me that after the second void printArray(int theArray\[\], int sizeOfArray) it expected a ;. I am doing this code based on https://youtu.be/VnZbghMhfOY. How can I fix this?

Here is the code I have:

#include <iostream>
using namespace std;

void printArray(int theArray[], int sizeOfArray);

int main()
{
    int bucky[3] = {20, 54, 675};
    int jessica[6] = {54, 24, 7, 8, 9, 99};

    printArray(bucky, 3);

    void printArray(int theArray[], int sizeOfArray)
    {
        for (int x = 0; x < sizeOfArray; x++){
            cout << theArray[x] << endl;
        }
    }
}

I tried to change the code order but that only made it worse, the error saying ; is apparently useless and the whole thing breaks apart if I put it there.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
dogydoge1
  • 1
  • 1
  • 2
    better dont try to learn C++ from youtube videos. Try here: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – 463035818_is_not_an_ai Oct 31 '22 at 19:00
  • You simply cannot define functions like this inline in c++ code blocks. If that's a requirement for your solution, use a lambda expression and `std::function` instead. Voted to close as a typo. – πάντα ῥεῖ Oct 31 '22 at 19:51

2 Answers2

2

You should take the implementation of the function "printArray" out of the main.

#include <iostream>
using namespace std;

void printArray(int theArray[], int sizeOfArray);

int main()
{

    int bucky[3] = {20, 54, 675};
    int jessica[6] = {54, 24, 7, 8, 9, 99};

    printArray(bucky, 3);
    return 0;
}
void printArray(int theArray[], int sizeOfArray)
{
    for (int x = 0; x < sizeOfArray; x++){
        cout << theArray[x] << endl;
    }
}
Danny Cohen
  • 505
  • 1
  • 5
  • 11
  • Just for the scalability: `3` +> `sizeof bucky` – Sandburg Oct 31 '22 at 19:30
  • 2
    @Sandburg [`sizeof bucky`](https://en.cppreference.com/w/cpp/language/sizeof) != 3. It's the size of `bucky` in **bytes**, probably 12. [`std::size(bucky)`](https://en.cppreference.com/w/cpp/iterator/size) == 3. – user4581301 Oct 31 '22 at 19:38
1

C++ doesn't have nested functions. You need:

#include <iostream>
using namespace std;

void printArray(int theArray[], int sizeOfArray);

int main()
{

    int bucky[3] = {20, 54, 675};
    int jessica[6] = {54, 24, 7, 8, 9, 99};

    printArray(bucky, 3);

}

void printArray(int theArray[], int sizeOfArray)
{

    for (int x = 0; x < sizeOfArray; x++)
    {
         cout << theArray[x] << endl;
    }
}
Jeffrey
  • 11,063
  • 1
  • 21
  • 42