2013-07-09 84 views
1

嗨初學者程序員在這裏:]甚至不知道如何短語這個問題。我正在做家庭作業,我似乎無法改變我創建的這個結構的價值(預算2)。C++初級程序員。幫助將結構傳遞給函數...?

該計劃的目標是在用戶輸入budget2的值後,顯示第一個結構budget1,並查找它與budget2之間的差異。

根據調試器,當我使用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

使用指針或引用來更改您的inputData中的值 void inputData(struct MonthBudget&data) – Alexis

回答

2

您正通過價值傳遞您的結構。
這意味着無論何時調用其中一個函數,您的結構都會被複制並傳遞給函數。所以在你的情況下,budget2被複制並傳遞給inputData。但是在完成inputData之後budget2仍然與副本上工作的功能相同。
您需要通過指針或參考來傳遞您的結構。我會在你的案例中使用引用。 所以你的函數應該是這樣的:
void inputData(MonthlyBudget& data);

BTW:
你真的應該拿一本好書關於C++和值閱讀通行證或按引用傳遞。

+0

好的,非常感謝。該作業的描述表明通過引用傳遞,但我認爲我跳過書中的那部分或其他內容。我正在使用C++開始:早期對象來加速夏季課程。盡力做到最好,但有時候會讓我頭疼:/。非常感謝你的幫助。 – 389helix