简体   繁体   中英

Beginner programmer in C++ . Help passing structure to function…?

Hi beginner programmer here :] not even sure of how to phrase this question. I'm working on a homework assignment and I can't seem to change the values of this structure I've created (budget2).

The goal of the program is to display the first structure, budget1, and find the difference between it and budget2 after the user inputs the values for budget2.

According to the debugger I'm not changing any values in budget2 when I input them using the inputData function and so when the showDiff function runs it just subtracts every value in budget2 from budget1 making all totals show up as 0.

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;


struct MonthlyBudget
{
    MonthlyBudget();
    double housing, utilities, householdExpenses, transportation, food, medical, insurance, entertainment, clothing, misc;

};


int main()
{
    HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position;

    void inputData(struct MonthlyBudget data);
    void displayData(struct MonthlyBudget);
    void showDiff(struct MonthlyBudget, struct MonthlyBudget); 
    struct MonthlyBudget budget1, budget2;


    displayData(budget1);
    inputData(budget2);
    showDiff(budget1, budget2);




    system ("PAUSE");
    return 0;
}

MonthlyBudget::MonthlyBudget()
{
    housing = 500.00;
    utilities = 150.00;
    householdExpenses = 65.00;
    transportation = 50.00;
    food = 250.00;
    medical = 30.00;
    insurance = 100.00;
    entertainment = 150.00;
    clothing = 75.00;
    misc = 50.00;
}

void displayData(struct MonthlyBudget data)
{
    cout << "\n\tBudget\n\n\t";
    cout << "$" << data.housing << "\n\n\t";
    cout << "$" << data.utilities << "\n\n\t";
    cout << "$" << data.householdExpenses << "\n\n\t";
    cout << "$" << data.transportation << "\n\n\t";
    cout << "$" << data.food << "\n\n\t";
    cout << "$" << data.medical << "\n\n\t";
    cout << "$" << data.insurance << "\n\n\t";
    cout << "$" << data.entertainment << "\n\n\t";
    cout << "$" << data.clothing << "\n\n\t";
    cout << "$" << data.misc << "\n\n\t";

}

void inputData(struct MonthlyBudget data)
{

    HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position;

    position.X = 15;
    position.Y = 3;
    SetConsoleCursorPosition(screen, position);

    cin >> data.housing;
    position.Y = 5;
    SetConsoleCursorPosition(screen, position);

    cin >> data.utilities;
    position.Y = 7;
    SetConsoleCursorPosition(screen, position);

    cin >> data.householdExpenses;
    position.Y = 9;
    SetConsoleCursorPosition(screen, position);

    cin >> data.transportation;
    position.Y = 11;
    SetConsoleCursorPosition(screen, position);

    cin >> data.food;
    position.Y = 13;
    SetConsoleCursorPosition(screen, position);

    cin >> data.medical;
    position.Y = 15;
    SetConsoleCursorPosition(screen, position);

    cin >> data.insurance;
    position.Y = 17;
    SetConsoleCursorPosition(screen, position);

    cin >> data.entertainment;
    position.Y = 19;
    SetConsoleCursorPosition(screen, position);

    cin >> data.clothing;
    position.Y = 21;
    SetConsoleCursorPosition(screen, position);

    cin >> data.misc;
    position.Y = 23;
    SetConsoleCursorPosition(screen, position);

}


void showDiff(struct MonthlyBudget data, struct MonthlyBudget data2)
{   
    HANDLE screen = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD position;

    position.X = 20;
    position.Y = 3;
    SetConsoleCursorPosition(screen, position);

    cout << (data.housing - data2.housing);

    position.Y = 5;
    SetConsoleCursorPosition(screen, position);
    cout << (data.utilities - data2.utilities);


    position.Y = 7;
    SetConsoleCursorPosition(screen, position);
    cout << (data.householdExpenses - data2.householdExpenses);

    position.Y = 9;
    SetConsoleCursorPosition(screen, position);
    cout << (data.transportation - data2.transportation);

    position.Y = 11;
    SetConsoleCursorPosition(screen, position);
    cout << (data.food - data2.food);

    position.Y = 13;
    SetConsoleCursorPosition(screen, position);
    cout << (data.medical - data2.medical);

    position.Y = 15;
    SetConsoleCursorPosition(screen, position);
    cout << (data.insurance - data2.insurance);

    position.Y = 17;
    SetConsoleCursorPosition(screen, position);
    cout << (data.entertainment - data2.entertainment);

    position.Y = 19;
    SetConsoleCursorPosition(screen, position);
    cout << (data.clothing - data2.clothing);

    position.Y = 21;
    SetConsoleCursorPosition(screen, position);
    cout << (data.misc - data2.misc);

    position.X = 5;
    position.Y = 23;
    SetConsoleCursorPosition(screen, position);

}

Not really sure what I'm doing wrong and any help would be appreciated. Thank you very much.

PS. I know my code must look ridiculous.

You are passing you structures by value.
This means whenever you call one of your functions your struct is copied and then passed to your function. So in your case budget2 is copied and passed to inputData . But after you inputData is done budget2 is still the same as the function worked on a copy.
You need to pass your structure either by a pointer or a ref. I would usggest using a reference in your case. So your function should look like this:
void inputData(MonthlyBudget& data);

BTW:
You should really take a good book about c++ and read about pass by value or pass by reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM