-3

I have school homework to write a C++ program that reads an array and then adds +5 to the values via another function.

    /******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <iomanip>

using namespace std;

float subtotal (float[], int);

int main() {
   int arraySize;

   cout << "Enter array size: ";
   cin >> arraySize;

   float array[arraySize];

   cout << "Enter an array size of " << arraySize << " : ";
   for (int i=0; i<arraySize; i++){
       cin >> array[i];
   }

   for (int i=0; i<arraySize; i++){
       array [i] = subtotal (array[i], arraySize);
       cout <<left<<setw(5)<< array [i];
   }

    return 0;
}

float subtotal (float array[], int arraySize){
    float x = array[]+5;
    return x;
}

I get this error:

main.cpp: In function 'int main()':
main.cpp:30:49: error: cannot convert 'float' to 'float*' for argument '1' to 'float subtotal(float*, int)'
        array [i] = subtotal (array[i], arraySize);
                                                 ^

what I'm I doing wrong? what does the error mean?

edit: So I matched my decelerations and now I get this error:

main.cpp: In function 'int main()':
main.cpp:30:49: error: cannot convert 'float' to 'float*' for argument '1' to 'float subtotal(float*, int)'
        array [i] = subtotal (array[i], arraySize);
                                                 ^
main.cpp: In function 'float subtotal(float*, int)':
main.cpp:38:21: error: expected primary-expression before ']' token
     float x = array[]+5;
  • `float array[arraySize];` This is not valid C++ (at least not in all compilers) – DimChtz Apr 21 '18 at 07:56
  • What's the reason for the function `subtotal`? It could be replaced by a simple `+=5`, so I'd guess part of the assignment is to pass the array to the function. So instead of changing the declaration to match the definition, you should instead change the definition, and move the `for` loop from `main` into `subtotal`. But that's only a guess as we don't know the real assignment wording. – Karsten Koop Apr 21 '18 at 08:39

1 Answers1

2

You have three problems: The first is that technically your program is not a valid C++ program, as it doesn't have variable-length arrays (which your array array is). If you want an "array" that gets its size at run-time you should be using std::vector.

Now some compiler allow VLA's as an extension of the language, like your compiler seem to be doing, and that leads to the second problem: You declare that your function subtotal wants an array as the first argument (or rather, a pointer to the first element in the array). You pass a single element of the array.

Then the third problem is that the definition of the subtotal function doesn't match the declaration you have, in that it do take a single element of the array as argument.

Simple solution for the second and third problem? Proper declaration of the function:

float subtotal(float element);

Note that I removed the second argument, since it's not needed.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    @KadiemHaider You didn't edit the right declaration. You should change the *function* declaration. And probably [get a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and ***start over from the beginning***. – Some programmer dude Apr 21 '18 at 08:08