C++结构变量初始化时出现问题
Trouble with C++ Structure Variable Initializing
我对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)
对对象的引用在语法上与副本相同,但是当您编辑它时,您正在编辑实际对象。
相关文章:
- C++转换参数初始化问题
- 运行时C++数组初始化问题
- wxWidgets 拖放文件事件处理程序初始化问题(无效static_cast)
- C++初始化具有 int 0 问题的unique_ptr
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 它解决了什么问题,对于非真空初始化,生命周期在初始化之前就开始了
- 在构造函数 c++ 中初始化属性时出现问题
- 我是否需要在虚拟继承类的构造函数中初始化基类以解决菱形继承问题?
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- shared_ptr类的向量的向量初始化问题
- public:静态常量字符串声明/初始化问题
- 初始化与类类型相同的静态成员(静态初始化顺序问题)
- 使用 true 初始化布尔数组时出现问题
- 为什么C++中的大括号初始化可以解决STL容器的初始化问题
- 程序在没有输入的情况下退出,它有基本的,驱动的类属性初始化问题
- C++中构造函数的对象初始化出现问题
- 初始化com对象的问题
- 初始化多个纹理 OpenGL、SDL C++ 时出现问题
- 共享库中 __attribute__((构造函数)) 的全局/静态变量初始化问题
- 理解向量初始化时出现问题