-6
error: expected primary-expression before ']' token
     calcNumbers(myArr[], type);
                       ^

No idea what that means. Main issue seems to be at 11 but i think i have more than that. Also how can improve code? Do i put pointers somewhere or something, should i structure it more somehow or would this be applicable in a job?

#include <iostream>

void calcNumbers(int arr[] ,int type);
int askNumbers();
int askType();

int main()
{
    int type = askType();
    int myArr = askNumbers();
    calcNumbers(myArr[], type);
}

int askType()
{
    int type_ = 0;

    std::cout << "1 for addition. 2 For subtraction. 3 for multiplication. 4 for division." << std::endl;
    std::cin >> type_;

    return type_;
}

int askNumbers()
{
    int arr[] = {};

    std::cout << "Enter as many numbers you'd like. Type 0 to stop." << std::endl;
    int i = 0;

    do
    {
        std::cin >> arr[i];
        i++;
    }

    while(arr[i] != 0);
    return arr[];
}

void calcNumbers(int arr[] , int _type)
{
    int arrSize = (int)( sizeof(arr));
    int totalNumber = 0;

    for(int i = 0; i < arrSize; i++)
    {
        if (_type == 1)
            totalNumber += arr[i];
        else if (_type == 2)
            totalNumber -= arr[i];
        else if (_type == 3)
            totalNumber *= arr[i];
        else if (_type == 4)
            totalNumber /= arr[i];
    }
    std::cout << totalNumber << std::endl;
}

thanks in advance, i really cannot wrap my head around this wierd issue...

user4581301
  • 33,082
  • 7
  • 33
  • 54
Aspect
  • 7
  • 4
  • 4
    What do you expect `calcNumbers(myArr[], type);` to do? – NathanOliver Jun 18 '18 at 20:30
  • 1
    Please only tag the language used. – Christian Gibbons Jun 18 '18 at 20:31
  • Will you put what the error message here? – Sailanarmo Jun 18 '18 at 20:33
  • 1
    Error message is in the title – Aspect Jun 18 '18 at 20:41
  • @Aspect no they are not. C++ originated from C but they have diverged since then – 463035818_is_not_an_ai Jun 18 '18 at 20:42
  • Part of the error message is in the title. What was left out is quite significant. – user4581301 Jun 18 '18 at 20:43
  • C:\Users\Adamx\C++ Utbildning\Uppgift 2\memeerisno\main.cpp|11|error: expected primary-expression before ']' token| - There you go! – Aspect Jun 18 '18 at 20:46
  • Unrelated: You have a logic error in `askNumbers`. `arr` is a fixed size array (of size zero elements, so there may be a compiler message at `int arr[] = {};`) It will not grow as you `std::cin >> arr[i];`. If you are allowed to, use `std::vector` instead of an array. It does grow as you `push_back` items. – user4581301 Jun 18 '18 at 20:47
  • Returning arrays is difficult due to [Array Decay.](https://stackoverflow.com/questions/1461432/what-is-array-decaying). You wind up returning a pointer rather than a copy of the array, and that pointer winds up pointing at an invalid object because the array goes out of scope at the end of the function and can no longer be counted on to be intact when you use the pointer later. – user4581301 Jun 18 '18 at 21:02
  • Another bit of nastiness: `sizeof(arr)` returns the size in bytes of ab object, not the number of items in the object. This is made worse because of array decay. `int arr[]` is a pointer, and the `sizeof` a pointer is the size of an address. The address has no idea how big what it points at is, so the actual size of the array is lost and cannot be recovered. Another reason to use `std::vector`: `vector` knows how big it is. – user4581301 Jun 18 '18 at 21:07

1 Answers1

3

The error message tells you that an expression is expected at a certain point in your code:

calcNumbers(myArr[missing expression here ], type);

Why is that? Because operator[] takes an argument (traditionally an index), as in myArr[1]. No argument, no compile.

Note that this error occurs when you are using myArr. You have other places where [] is used in a declaration, and in those locations it is accepted as it is part of the type. If you have an array and you want to pass the entire array as a parameter, use just the variable name, as in calcNumbers(myArr, type);. (This would assume that myArr is an array, even though it is not in your code. You declare myArr as type int.)

Then again, your code has many problems with array usage (such as the lack of a bound on the index in askNumbers). You may want to find a good tutorial on array usage in C++. Or see if you can use std::vector after reading vector - C++ Reference or std::vector - cppreference.com.

JaMiT
  • 14,422
  • 4
  • 15
  • 31