我对C ++有点新意。我出于学校的原因一直在服用它。在介绍函数和类之前,我一直做得非常好。出于某种原因,每次我通过类或函数传递数字时,它总是返回0.无论我在代码中放置数字,它们总是在最终输出中被踢回0。我联系了我的老师,她没有提供任何建议,我或多或少地复制了完整的书,没有任何结果。 我将包括完整的代码和我的输出,我没有看到代码本身有任何问题,也许我只是遗漏了一些东西。
显示两个变量Budget1和Budget2未初始化(错误代码C4700),但我在线初始化的所有内容都是带有数字的变量。我找到了这个链接, Structure Initialization 但是当我尝试这个时:
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;
}
答案 0 :(得分:0)
如果您有一个像void function(MonthlyBudget);
这样的函数原型,那么您的变量将按值传递。这意味着您创建了类型的副本,并且在您正在编辑副本的函数范围内。它将在您的功能范围结束时销毁。
您需要做的是通过引用传递,这将涉及使用&
符号,如下所示:
void function(MonthlyBudget &monthlyBudget)
对象的引用将在语法上与副本相同,但在编辑时,您正在编辑真实对象。