C ++初级程序员。帮助传递结构功能......?

时间:2013-07-09 07:44:25

标签: c++ visual-studio-2010 struct

嗨,初学者程序员在这里:]甚至不确定如何表达这个问题。我正在做家庭作业,我似乎无法改变我创建的这个结构的值(budget2)。

该计划的目标是显示第一个结构budget1,并在用户输入预算2的值后查找它与预算2之间的差异。

根据调试器,当我使用inputData函数输入时,我没有更改budget2中的任何值,因此当showDiff函数运行时,它只需从budget1中减去budget2中的每个值,使所有总计显示为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);

}

不确定我做错了什么,任何帮助都会受到赞赏。非常感谢你。

PS。我知道我的代码看起来很荒谬。

1 个答案:

答案 0 :(得分:2)

你是按价值传递你的结构 这意味着无论何时调用其中一个函数,您的结构都会被复制,然后传递给您的函数。因此,在您的情况下,budget2会被复制并传递给inputData。但是inputData完成后budget2仍然与副本上的函数相同 您需要通过指针或引用传递您的结构。我会在你的案例中使用引用来获取。 所以你的功能应该是这样的:
void inputData(MonthlyBudget& data);

顺便说一句:
你应该写一本关于c ++的好书并阅读有关按值传递或通过引用传递的内容。