简体   繁体   English

C ++结构变量初始化遇到问题

[英]Trouble with C++ Structure Variable Initializing

I'm a little new to C++. 我是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. 出于某种原因,每次我在类或函数中传递数字时,数字始终会返回0。无论我将数字放在代码中的什么位置,最终输出中它们总是会踢回0。 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. 它显示两个变量Budget1和Budget2尚未初始化(错误代码C4700),但我在网上发现的所有初始化变量都是带有数字的变量。 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 Output_For_Code

When you have a function prototype like this void function(MonthlyBudget); 当您有一个像这样的函数原型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. 对对象的引用在语法上将与副本相同,但是在编辑对象时,您是在对真实对象进行编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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