未初始化的变量,但我正在尝试接受变量的输入?
Unintialized variable but I'm trying to accept an input for a variable?
我正在尝试设置一个输入,该输入可以接收商品的价格和数量,并且它一直给我两个未初始化的变量错误,所以我在主下给他们一个变量 0,但它不会打印出输入,只是显示它们的值为 0?
int main() {
string partNumber;
string partDiscription;
int partQuantity = 0;
int partPrice = 0;
int getInvoiceAmount;
class Invoice;
cout << "Please enter part number: " << partNumber << endl;
getline(cin, partNumber);
cout << "Please enter part discription: " << partDiscription << endl;
getline(cin, partDiscription);
cout << "Please enter quantity of item: " << partQuantity << endl;
cin >> partQuantity;
cout << "Please enter price of item: " << partPrice << endl;
if (partQuantity < 0)
partQuantity = 0;
if (partPrice < 0)
partPrice = 0;
这是因为您尝试在初始化变量数据之前对其进行引用。你应该这样做
cout << "Please enter part number: " << endl;
getline(cin, partNumber);
cout << "Please enter part discription: " << endl;
getline(cin, partDiscription);
cout << "Please enter quantity of item: " << endl;
cin >> partQuantity;
这样,您就不会尝试引用尚未初始化的变量。
相关文章:
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- 堆栈中大小变量输入错误 (C++)
- 如何使用用户输入变量制作二维数组?
- 一个变量的输入值也会保存到另一个变量中
- 如何在循环之外重新输入变量
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较
- 使用单个变量对字符输入进行计数
- 如何在C++中从标准输入中获取变量输入
- 用户无法为第二个变量输入。C++
- C 错误:从命令行输入更改为变量输入
- 基于变量输入的泛型类方法计算
- 为什么我不能继续使用 std::cin 向同一变量输入其他值?
- 如何使用同一变量输入字母和数字?(C )
- C++:为什么为"int"变量输入"char"会导致递归变得疯狂?
- C++ 用户为整数变量输入非整数值
- 循环以丢弃来自stdin、Linux C++的虚假变量输入
- 如何通过cin为2个以上的变量输入流
- 为char类型变量输入多个字符的影响
- 接收从PHP到c++的各种变量输入
- 控制台应用程序-用户可以为c++中的整数变量输入的最高值是多少