0

I'm new to C++ and have an assignment that requires four calculations that all build off of each other. The first is simple, with the requirement to provide the average for 4 defined numbers. I need the output from this file to print in the main cpp file, but I'm not able to get the value out to pass to the main file. I apprecate any input and feedback!

Below is the code for the header file, "average.h":

#ifndef AVERAGE_H_INCLUDED
#define AVERAGE_H_INCLUDED

using namespace std;

double getAverage(int Avg);

#endif

Below is the code for the "average.cpp" file:

#include <iostream>
#include <cmath>

using namespace std;

double getAverage(int avg)

{
    int arr[] = { 24, 30, 40, 50 };
    int size, i;
    int Average, sum = 0;
    size = sizeof(arr) / sizeof(arr[0]);
    for (i = 0; i < size; i++)
        sum = sum + arr[i];
    Average = sum / size;
    return Average;
}

void getAverage()
{
    std::cout << Average << std::endl;
}

Below is the "main.cpp" file:

#include <iostream>
#include "average.h"

using namespace std;

int main()
{
    cout << "The average of the four scores is: " << getAverage << endl;

}

I have tried numerous things and now I'm getting multiple errors and feel like I'm missing a few simple things. Any guidance and a little explanation of what I'm doing wrong would be very helpful.

  • It seems you are at the very start of your learning journey. I recommend you first spend some time onto simple examples such as [this tutorial](https://www.w3schools.com/cpp/). I did not go through it myself but at your level, I think it is better starting there than on SO forums. A more advanced and complete reference is cppreference.com ([link to the functions page](https://en.cppreference.com/w/cpp/language/functions) that you can read to understand how to call functions, although that is only part of what you did wrong). Long term, it will help you greatly. – Atmo Mar 24 '23 at 04:35
  • 1
    *"I'm getting multiple errors"* -- these are what you should be asking about. Well, not all of them; focus on the first one reported by your compiler. [Enabling compiler warnings](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) and addressing those is also a good idea. – JaMiT Mar 24 '23 at 04:45

2 Answers2

3

Multiple things wrong here, and because you do not actually show any error messages its hard to know if you are compiling correctly or not. Assuming that you are actually compiling correctly then lets fix a few things

First, to call a function you have to use ()

Ie this

int main()
{
    cout << "The average of the four scores is: " << getAverage() << endl;
                                                               ^^   
}

Secondly, the first getAverage function has a meaningless argument, take it out. Also it should be returning a double.

double getAverage()
{
    int arr[] = { 24, 30, 40, 50 };
    int size, i;
    int sum = 0;
    size = sizeof(arr) / sizeof(arr[0]);
    for (i = 0; i < size; i++)
        sum = sum + arr[i];
    double Average = sum / size;
    return Average;
}

Fix the header file too

 double getAverage();

Finally get rid of the second getAverage function

pm100
  • 48,078
  • 23
  • 82
  • 145
  • Thank you very much for helping explain and clarify this for me! It's very helpful seeing where I was going wrong. I have this step working now. – user21478345 Mar 25 '23 at 13:04
  • This will still perform integer division before assigning the result to the `double` and returning it. – Nathan Pierson Mar 27 '23 at 01:08
  • @NathanPierson - yup , but thats the least of OPs problems – pm100 Mar 27 '23 at 02:12
  • Sure, just noting that your answer called attention to the return type mismatch but made a change that doesn't actually affect the program's behavior. – Nathan Pierson Mar 27 '23 at 02:14
-1

A function that returns a type has to be defined with the same type. That's wrong:

double getAverage(int avg){ ...; int Average; ... ; return Average}

The getAverage() function cannot use the Average variable, because Average is defined in another scope. It is defined in the getAverage(int avg) body.

//average.h

#ifndef AVERAGE_H_INCLUDED
#define AVERAGE_H_INCLUDED

int getAverage();

#endif

//average.cpp

#include <iostream>
#include <cmath>
#include "average.h"

using namespace std;

int getAverage()

{
    int arr[] = { 24, 30, 40, 50 };
    unsigned long long size;
    int Average;
    int sum = 0;
    size = sizeof(arr)/sizeof(arr[0]);
    for (int i = 0; i < size; i++) {
        sum = sum + arr[i];
    }
    Average = sum / size;
    return Average;
}

//main.cpp

#include <iostream>
#include "average.h"

using namespace std;

int main()
{
    cout << "The average of the four scores is: "<<getAverage();
}
Gabriel
  • 32
  • 5
  • Thank you for your reply! In looking at your post, I realized I was missing a very important bracket. – user21478345 Mar 25 '23 at 13:05
  • There's no reason to make `Average` a global variable. Beyond that, a code only answer with no explanation is not very useful imo. – Nathan Pierson Mar 27 '23 at 01:09
  • @Nathan Pierson . You're right. I made a mistake when I was correcting a previous problem. I already corrected it in the post. – Gabriel Mar 27 '23 at 01:46
  • There's nothing preventing a function with a return type of `double` from returning a local variable whose type is `int`, because an `int` is implicitly convertible to a `double`. Your edit adds incorrect information and didn't get rid of the now-useless global variable. – Nathan Pierson Mar 27 '23 at 01:52
  • @Nathan Pierson. Ready. Deleted variable. – Gabriel Mar 27 '23 at 01:58