变量正在使用,但未初始化
The variable is being used and not being initialized
这是一个基本的C++编程类的程序。 可变食物价格和bisPrice给了我一个错误,即在没有初始化的情况下被使用,并且它们不能通过引用传递。我不确定如何初始化它们。请帮忙。谢谢
using namespace std;
void getBiscuits( string &, double & );
void getDryFood( string & , double & );
void processData( double, double , double & , double & , double & , double , double );
void displayOrder( string , string , string , double , double , double , double , double , double , double , double , double , double );
const double foodCost = 30;
const double bisCost = 10;
const double salesTax = .07;
int main()
{
//declare variables
string name;
double numBis;
string bis;
string dryFood;
double foodNum;
double bisPrice;
double foodPrice;
double tax = 0;
double total = 0;
double subTotal = 0;
//get data from user
cout << "Welcome to The Big Dog's Food Panty" << endl;
cout << "Please tell us your first and last name?" << endl;
getline(cin, name);
cout << "Thanks " << name << endl;
system ("cls");
getBiscuits( bis , numBis ) ;
getDryFood( dryFood , foodNum ) ;
processData( numBis , foodNum , subTotal , tax , total , bisPrice , foodPrice ) ;
displayOrder( name , bis , dryFood , bisPrice , numBis , bisCost , foodPrice , foodNum , foodCost , subTotal , tax , salesTax , total ) ;
// stop to let the user read the far
system ("pause");
}
void getBiscuits( string &bis , double &numBis )
{
string dummyVariable;
cout << "Would you like the Spicy Chicken or BBQ Beef biscuits?" << endl;
getline(cin, bis);
system ("cls");
cout << "How many five lb bags of " << bis << " would you like?" << endl;
cin >> numBis;
getline(cin, dummyVariable);
system ("cls");
}
void getDryFood( string &dryFood , double &foodNum )
{
cout << "Would you like Salmon and Peas or Chicken and Rice dry food?" << endl;
getline(cin, dryFood);
system ("cls");
cout << "How many 28 lbs bags of " << dryFood << " would you like?" << endl;
cin >> foodNum;
system ("cls");
}
void processData( double numBis , double foodNum , double &subTotal ,double &tax , double &total , double bisPrice , double foodPrice )
{
//calculate
bisPrice = numBis * bisCost;
foodPrice = foodNum * foodCost;
subTotal = bisPrice + foodPrice;
tax = subTotal * salesTax;
total = subTotal + tax;
}
void displayOrder( string name , string bis , string dryFood , double bisPrice , double numBis , double bisCost , double foodPrice , double foodNum , double foodCost , double subTotal ,double tax , double salesTax , double total )
{
// display cost
cout << "Thanks " << name << " for your order!" << endl;
cout << numBis << setprecision(2) << fixed << " five lbs bags of " << bis << " at $10.00 each is $" << bisPrice << endl;
cout << setprecision(0) << fixed <<foodNum << setprecision(2) << fixed << " 28 lbs bags of " << dryFood << " at $30.00 each is $" << foodPrice << endl;
cout << setprecision(2) << fixed << "Your subtotal is $" << subTotal << endl;
cout << setprecision(2) << fixed << "Your sales tax is $" << tax << endl;
cout << setprecision(2) << fixed << "Your total is $" << total << endl;
}
您没有定义它们的初始值。
double bisPrice;
double foodPrice;
更改为:
double bisPrice = 0;
double foodPrice = 0;
若要修复编译器警告,请初始化变量:
double bisPrice = 0;
double foodPrice = 0;
在 C 和 C++ 中,基本值类型没有初始值。
声明常规局部变量时,默认情况下其值未确定。但是您可能希望变量在声明该变量的同时存储该值。为此,您可以初始化变量。
对象将使用其默认构造函数进行初始化,这就是为什么string
变量没有相同的错误的原因。
只需这样做,错误就会消失:
double bisPrice = 0;
double foodPrice = 0;
我认为你应该很明显,你没有在声明:)上提供任何价值
是的,不要忘记这些:
string name = "";
double numBis = 0;
string bis = "";
string dryFood = "";
double foodNum = 0;
函数中的变量具有自动存储功能,因此默认情况下它们不会初始化为值。
相关文章:
- 初始值设定项列表是否只接受使用相同类型的值初始化变量?
- 在 C++ 中访问 lambda 捕获初始化变量
- 为什么C++不支持对未初始化变量进行智能分析?
- 使用 clang++ 和 g++ 在C++中初始化变量
- C++使用 lambda 初始化变量
- 如何在初始化列表中的构造函数之后初始化变量/对象?
- C++ - 输出与初始化变量不同?
- C++/Win32 构造函数不使用从对话框获取的字符串初始化变量
- 在C++中,为什么int可以使用new运算符初始化变量,而double不能
- 具有静态存储持续时间的常量初始化变量的初始化顺序
- C++ 中的初始化变量
- C++中未初始化变量的值
- 如何在需要提及需要循环声明的其他类的类中初始化变量?
- 我想知道在构造函数中初始化变量时的生命周期
- 读取恰好具有良好值的未初始化变量
- 如何在 getter 的父类中初始化变量的情况下访问子类中的变量
- 为什么在 c++ 中有多种初始化变量的方法
- 没有参数的默认构造函数是否总是初始化变量?
- 是否可以使用 lambda 初始化变量(删除复制 ctor 时)
- 使用构造函数跳闸UB的新放置后使用初始化变量