0

I'm a little new to C++. I've been taking it for school reasons. I have been doing pretty good, up until Functions and classes were introduced. For some reason, every time I pass numbers through a class or function it always comes back as 0. No matter where I put numbers in the code, they always get kicked back to 0 in the end output. I have contacted my teacher she didn't offer any suggestions, I've more or less copied exactly form the book with no results. I will include the full code and my output, I don't see anything wrong with the code myself, maybe I'm just missing something.

It is showing that the two variables Budget1 and Budget2 are not initialized(Error Code C4700) but everything I am finding online for initialization is for variables that carry numbers. I did find this link, Structure Initialization but when I tried this:

MonthlyBudget Budget1 =
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
MonthlyBudget Budget2 =
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

It stilled showed the error that the variables were uninitialized. Any advice would be great, thanks!

/*
  Display a proposed budget and actual money spent through the month
  Compute totals in each area of the budget and display 
  if amounts spent were over or under budget.
*/

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <windows.h>

using namespace std;

struct  MonthlyBudget
{
     double House;      // Housing Expenses
     double Utils;      // Utilities Expenses
     double HouseExp;   // Houshold Expenses
     double Trans;      // Transportation Expenses
     double Food;       // Food Expenses
     double Med;        // Medical Expenses
     double Ins;        // Insurance Expenses
     double Ent;        // Entertainment
     double Cloth;      // Clothing Expenses
     double Misc;       // Miscellanious Expenses
};

void placeCursor(HANDLE, int, int);         //function prototypes
void displayPrompts(HANDLE);
void getOriginalBudget(HANDLE, MonthlyBudget);
void getActualBudget(HANDLE, MonthlyBudget);
void displayTotals(HANDLE, MonthlyBudget, MonthlyBudget);


int main()
{
    MonthlyBudget Budget1, Budget2;
    cout << fixed << showpoint << setprecision(2);

    // Get the handle to standard output device (console)
    HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);

    displayPrompts(screen);
    getOriginalBudget(screen, Budget1);
    getActualBudget(screen, Budget2);
    displayTotals(screen, Budget1, Budget2);

    return 0;
}

// Place Cursor
void placeCursor(HANDLE screen, int row, int col)
{
    COORD position;  // holds a pair of x and y coords
    position.Y = row;
    position.X = col;
    SetConsoleCursorPosition(screen, position);
}

void displayPrompts(HANDLE screen)
{
    placeCursor(screen, 3, 25);
    cout << "******* Data Entry Form *******" << endl;
    placeCursor(screen, 5, 25);
    cout << "Housing: " << endl;
    placeCursor(screen, 7, 25);
    cout << "Utilities: " << endl;
    placeCursor(screen, 9, 25);
    cout << "Household Expesnse: " << endl;
    placeCursor(screen, 11, 25);
    cout << "Transportation: " << endl;
    placeCursor(screen, 13, 25);
    cout << "Food: " << endl;
    placeCursor(screen, 15, 25);
    cout << "Medical: " << endl;
    placeCursor(screen, 17, 25);
    cout << "Insurance: " << endl;
    placeCursor(screen, 19, 25);
    cout << "Entertainment: " << endl;
    placeCursor(screen, 21, 25);
    cout << "Clothing: " << endl;
    placeCursor(screen, 23, 25);
    cout << "Miscellaneous: " << endl;
}
// Get user input for original budget
void getOriginalBudget(HANDLE screen, MonthlyBudget Budget1)
{
    placeCursor(screen, 5, 45);
    cin >> Budget1.House;
    placeCursor(screen, 7, 45);
    cin >> Budget1.Utils;
    placeCursor(screen, 9, 45);
    cin >> Budget1.HouseExp;
    placeCursor(screen, 11, 45);
    cin >> Budget1.Trans;
    placeCursor(screen, 13, 45);
    cin >> Budget1.Food;
    placeCursor(screen, 15, 45);
    cin >> Budget1.Med;
    placeCursor(screen, 17, 45);
    cin >> Budget1.Ins;
    placeCursor(screen, 19, 45);
    cin >> Budget1.Ent;
    placeCursor(screen, 21, 45);
    cin >> Budget1.Cloth;
    placeCursor(screen, 23, 45);
    cin >> Budget1.Misc;
}
// Get final ammounts spent throughout the month
void getActualBudget(HANDLE screen, MonthlyBudget Budget2)
{
    placeCursor(screen, 5, 55);
    cin >> Budget2.House;
    placeCursor(screen, 7, 55);
    cin >> Budget2.Utils;
    placeCursor(screen, 9, 55);
    cin >> Budget2.HouseExp;
    placeCursor(screen, 11, 55);
    cin >> Budget2.Trans;
    placeCursor(screen, 13, 55);
    cin >> Budget2.Food;
    placeCursor(screen, 15, 55);
    cin >> Budget2.Med;
    placeCursor(screen, 17, 55);
    cin >> Budget2.Ins;
    placeCursor(screen, 19, 55);
    cin >> Budget2.Ent;
    placeCursor(screen, 21, 55);
    cin >> Budget2.Cloth;
    placeCursor(screen, 23, 55);
    cin >> Budget2.Misc;
}
// Display the difference in original budget and actual budget
void displayTotals(HANDLE screen, MonthlyBudget Budget1, MonthlyBudget Budget2)
{
    placeCursor(screen, 28, 0);
    cout << "Here is the differences between what you planned to spend and what you spent: \n";
    cout << " Housing: $" << Budget1.House - Budget2.House << endl;
    cout << "Utilities: $" << Budget1.Utils - Budget2.Utils << endl;
    cout << "Household Expenses : $" << Budget1.HouseExp - Budget2.HouseExp << endl;
    cout << "Transportation: $" << Budget1.Trans - Budget2.Trans << endl;
    cout << "Food: $" << Budget1.Food - Budget2.Food << endl;
    cout << "Medical: $" << Budget1.Med - Budget2.Med << endl;
    cout << "Insurance: $" << Budget1.Ins - Budget2.Ins << endl;
    cout << "Entertainment: $" << Budget1.Ent - Budget2.Ent << endl;
    cout << "Clothing: $" << Budget1.Cloth - Budget2.Cloth << endl;
    cout << "Miscellaneous: $" << Budget1.Misc - Budget2.Misc << endl;
}

Output_For_Code

Memo
  • 1
  • 2
  • 3
    You need to pass your objects by reference: https://stackoverflow.com/questions/19827119/c-argument-passing-passed-by-reference – NathanOliver Jan 17 '18 at 22:03

1 Answers1

0

When you have a function prototype like this void function(MonthlyBudget); your variable is being passed by value. What this means is that a copy of your type is created and in the scope of the function you are editing a copy. It will be destroyed at the end of your function scope.

What you need to do is pass by reference which will involve using the & symbol like so:

void function(MonthlyBudget &monthlyBudget)

A reference to the object will be used syntactically the same as the copy, but when you edit it you are editing the real object.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • Thank you so much Mr Fox!! I put all the & in the program and it wasn't doing anything. So that one function call was all I needed then. – Memo Jan 19 '18 at 00:12