C++结构变量初始化时出现问题

Trouble with C++ Structure Variable Initializing

本文关键字:问题 初始化 结构 变量 C++      更新时间:2023-10-16

我对C++有点陌生。我一直在学习学校的原因。我一直做得很好,直到引入函数和类。出于某种原因,每次我通过类或函数传递数字时,它总是返回为 0。无论我在代码中将数字放在何处,它们总是在最终输出中被踢回 0。我已经联系了我的老师,她没有提供任何建议,我或多或少地完全复制了这本书,没有结果。 我将包括完整的代码和我的输出,我自己没有看到代码有什么问题,也许我只是错过了一些东西。

它显示两个变量 Budget1 和 Budget2 没有初始化(错误代码 C4700),但我在网上找到的用于初始化的所有内容都是针对带有数字的变量。我确实找到了这个链接, 结构初始化 但是当我尝试这个时:

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

它静止显示变量未初始化的错误。任何建议都会很棒,谢谢!

/*
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

当你有这样的函数原型时void function(MonthlyBudget);你的变量是按值传递的。这意味着将创建您类型的副本,并在函数范围内编辑副本。它将在函数作用域结束时被销毁。

您需要做的是通过引用传递,这将涉及使用&符号,如下所示:

void function(MonthlyBudget &monthlyBudget)

对对象的引用在语法上与副本相同,但是当您编辑它时,您正在编辑实际对象。