Search code examples
c++visual-studio-2012structurebubble-sort

C++ Structure with Bubble Sort


I'm using Microsoft Visual Studios Express 2012, and I am trying to write a program that gets the rainfall per month and displays it. I have to use structures, and I have to use bubble sort to display it from largest to smallest. I seem to have become lost in my code, and I'm confused with where I went wrong. It is currently telling me I have two unresolved externals.

//This program shows the months of the year with their associated rainfall amount.
#include <iostream> 
#include <string>
using namespace std;

const int SIZE = 12;

struct Rainfall
{
double rain[SIZE];

double getValue()
{
    double value = rain[SIZE];
    return value;
}

};

struct Months
{
const string MONTHS[SIZE];

Months()
{
    string MONTHS[SIZE] = {"January", "Feburary", "March",
                         "April", "May", "June", "July", 
                         "August","September","October",
                         "November","December"};
}

string getNames()
{
    string names = MONTHS[SIZE];
    return names;
}
};

//Function Prototypes
double getInput(const Months &, Rainfall &);
void sortData(Rainfall[],int);
void displayData(const Months, const Rainfall &);

int main()
{
Months timePeriod;
Rainfall amount;
Rainfall rain[SIZE];

getInput(timePeriod,amount);
sortData(rain,SIZE);
displayData(timePeriod,amount);

cin.ignore();
cin.get();
return 0;
}

/*****getInput*****/
double getInput(Months &timePeriod, Rainfall &amount)
{
cout << "\nPlease enter the amount of rainfall per month for the following  months:\n";

for(int counter = 0; counter <= 11; counter++)
{
    cout << timePeriod.MONTHS[counter] << ": ";
    cin >> amount.rain[counter];
    cout << endl;
    return amount.rain[counter];
}

}

/*****sortData*****/
void sortData(Rainfall array[], int SIZE)
{
Rainfall temp;
bool swap;

do
{
    swap = false;
    for(int count = 0; count < (SIZE-1); count++)
    {
        if(array[count].getValue() > array[count +1].getValue())
        {
            temp = array[count];
            array[count] = array[count +1];
            array[count + 1] = temp;
            swap = true;
        }
    }
} while (swap);
}

/*****displayData*****/
void displayData(Rainfall number[], Months names[], int SIZE)
{
for(int index = 0; index < SIZE; index++)
{
    cout << names[index].getNames() << endl;
    cout << number[index].getValue() << endl;
}
}

Solution

  • Make sure your definitions match your declarations.

    double getInput(const Months &, Rainfall &);
    void sortData(Rainfall[],int);
    void displayData(const Months, const Rainfall &);
    

    Without looking at all of them I can see a discrepancy here with displayData

    void displayData(const Months, const Rainfall &); // Declaration.
    void displayData(Rainfall number[], Months names[], int SIZE) // Definition.
    

    An unresolved external symbol in this case means that you have declared a function, but during the linking stage there was no definition found for it.

    You have declared the displayData function to take a const Months& and const Rainfall& argument. Your definition takes a Rainfall[], Months[] and int argument. These therefore, are not matching, and to the compiler they are different functions.

    Thanks to function overloading, we can have functions with the same name but take different arguments.