-3

So I need to display a table with all of the categories such as housing, transportation, etc along with the other information in each of the arrays that I've initialized. I can't seem tot figure out how to display individual array elements using the "displayTable" function. When I say "cout << housing[0] << " " << utilities[0]" and so on I keep getting a build error. I've tried different syntax as well but I just can't figure it out. It's for an assignment in one of my classes. Any help is much appreciated!

#include <iostream>
#include <iomanip>

using namespace std;

struct Category
{
    string category;
    double maxAmount;
    double amountSpent;
};

void displayTable(Category housing[3], Category utilities[3],
              Category transportation[3], Category food[3],
              Category entertainment[3], Category miscellaneous[3])
{ 
  cout << setprecision(2)
       << housing[0];
}

int main()
{
    int menuChoice;

    Category housing = {"Housing", 500.00, 0.00};
    Category utilities = {"Utilities", 150.00, 0.00};
    Category transportation = {"Transportation", 50.00, 0.00};
    Category food = {"Food", 250.00, 0.00};
    Category entertainment = {"Entertainment", 150.00, 0.00};
    Category miscellaneous = {"Miscellaneous", 50.00, 0.00};

    do
    {

    } while (menuChoice != 3);

    return 0;
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 3
    What's that build error, then? Usually they tell you what's wrong, but if you need help understanding the error, don't hide it from us :) – Max Vollmer Aug 30 '18 at 20:02
  • If want to print a custom class, you almost certainly need a custom print routine. Check out the section on overloading `operator<< ` in [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 Aug 30 '18 at 20:04
  • Also, answer that by editing your question to add the missing info, don't just add comments below it. – Dave S Aug 30 '18 at 20:04
  • 2
    You are trying to do too many new things at once. When you write code, start with something small and simple that works perfectly, then build up, and *never add to code that doesn't work.* (Funny how they never teach that in school.) Create one variable of type `Category`, and try to display that. – Beta Aug 30 '18 at 20:07

3 Answers3

1

I think you don't study well the array etc. With the string housing[0] you access the first element of an array named housing however in this case there is not array in the code. Maybe you want to create a function that take an element of the type Category and print the value of its field. Something like this:

displayCategory(Category aCategory){
    cout<<"Category = "+aCategory.category+"\n"+"maxAmount ="+aCategory.maxAmount+"\n"+"amountSpent = "+aCategory.amountSpent+"\n";
}

Then you maybe want to create an array of Category so instead of declare a variable for every category you could do this:

Category arrayOfCategory[6];
arrayOfCategory[0] = {"Housing", 500.00, 0.00};
arrayOfCategory[1] = {"Utilities", 150.00, 0.00};
arrayOfCategory[2] = {"Transportation", 50.00, 0.00};
arrayOfCategory[3] = {"Food", 250.00, 0.00};
arrayOfCategory[4] = {"Entertainment", 150.00, 0.00};
arrayOfCategory[5] = {"Miscellaneous", 50.00, 0.00};

now if you want a function that print all the category in arrayOfCategory simply using the first function displayCategory we can create this function:

displayTable(Category *anArrayOfCategory, int dimOfTheArray){
    for(int i = 0; i < dimOfTheArray; i++)
        displayCategory(anArrayOfCategory[i]);
}
Andrea Bellizzi
  • 497
  • 5
  • 14
0

You should treat Category as a struct, not as an array

The simplest way is to access members in the following way

void displayTable(Category housing, Category utilities,
              Category transportation, Category food,
              Category entertainment, Category miscellaneous)
{ 
  cout << setprecision(2)
       << housing.maxAmount;
}
alsora
  • 551
  • 5
  • 17
0
void displayTable(Category housing[3], Category utilities[3],
          Category transportation[3], Category food[3],
          Category entertainment[3], Category miscellaneous[3])
{ 
    std::cout << setprecision(2) << housing[0];
}

housing[0] is of type Category, which you made. How should std::cout know what to print if you give it your own custom type?

You can print the members of your struct one by one like so:

std::cout << setprecision(2) << housing[0].maxAmount;

or implement a custom << operator inside your type:

struct Category
{
    std::string category;
    double maxAmount;
    double amountSpent;

    friend std::ostream& operator<< (std::ostream& stream, const Category& category) {
        stream << category.maxAmount;
    }
};

and then your original code should work fine.

(Obviously you need to add the other members to the output as well, and maybe add some headers/titles, commas etc. for human readability, but the principle should be clear.)

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43