#include <iostream>
using namespace std;

void get_temps(int choice)
{
    if(choice == 1)
    {
        double temps[12];
        for(int i = 0; i < 12; i++)
        {
            int month_num= i+1;
            cout << &"Please enter the average temperature for month " [month_num];
            cin >> temps[i];
        }
    }
    if(choice == 2)
    {
        double temps[10];
        for(int i = 0; i < 10; i++)
        {
            int year_num = i+1;
            cout <<"Please enter the average yearly temperature for year " [ year_num];
            cin >> temps[i];
        }
    }
}

double get_vector_average(double temps[], int size)
{
    double total = 0;
    for(int i = 0; i < size; i++)
    {
        total = total + temps[i];
    }
    double average = total/size;
    return average;
}

int main ()
{
    int choice;
    do{
        cout << "Please enter:\n\t1 to calculate the average yearly temperature\n\t2 to calculate the average temperature for a decade\n\t3 to quit\nEnter number now: ";
        cin >> choice;
        if (choice < 1 || choice > 3)
        {
            cout <<"Input Error!\n\n";
        }
    }while(choice < 1 || choice > 3);

    if(choice == 1)
    {
        get_temps(1);
        cout << "The average temperature for the year is " + get_vector_average(double temps[12], 12);
    }
    if(choice == 2)
    {
        get_temps(2);
        cout << "The average temperature for the decade is " + get_vector_average(double temps[10], 10);
    }
    if(choice == 3)
    {
        cout << "Goodbye...";
    }

    return 0;
}

This is my code and I cannot get it to run. My code consists of two user defined functions: The first is used to get the temperatures and store them in a vector. If the user picks yearly temp average, it stores 12 temps. If the user picks decade average temp, the vector will store 10 temps. The second is used to find the average of the temperatures. Both will be called in the main function corresponding to what the user chooses. My code will not compile. It says the array needs an initializer. I am not sure how to do this. Any help would be appreciated.

Firstly you are not using a vector but an array. You need to have the array in main and pass it to the function that takes in input and then pass it to the average function.

Could you show me where the error is and how to fix it. I am very new at this and do not understand arrays or vectors at all.

Maybe this startup hint would help you

#include <iostream>
using namespace std;

void getMonthsData(int data[], const int SIZE){
  //print message
  //for i = 0 to size 
    //ask for data[i]

}
void getYearData(int data[], const int SIZE){
 //print message
 //for i = 0 to size
   //ask for data[i]
}

void getAverate(int data[],const int size){
 //your get_average logic here
}

int main(){
 int data[12] = {0}; //zero initialize everything
 int MAX_SIZE = 12;

 int choice = -1;
 cin >> choice; //get user input

 switch(choice){
  case 1: MAX_SIZE = 12; getMonthsData(choice,MAX_SIZE); break;
  case 2: MAX_SIZE = 10; getYearData(choice,MAX_SIZE); break;
 }
 cout << "Average= " << getAverage(choice,MAX_SIZE) << endl;
 return 0;
}

Edited 3 Years Ago by firstPerson

This article has been dead for over six months. Start a new discussion instead.