我试图在对话框中显示多个商店商品。
我试图将txt文件中的值与每个段(ID号,价格等等)对齐,我甚至不确定它是否正确,但是当我运行它,对话框显示完全不同的值。
这是txt文件(名为 Store Sales.txt )
0-8053-7442-6使用C ++解决问题1 45.50
7751158146 Looney Tunes Diskettes 12 8.50
88869809780 HP Laser Printer Paper 3 10.99
2429412454 No. 2 Pencil 24 0.10
4895469286 Note Pad 5 0.89
这是我到目前为止所获得的代码:
#include<iostream>
#include<iomanip>
#include<string.h>
#include<fstream>
#include <cstdlib>
using namespace std;
ifstream inFile;
class Product
{
private:
char idNum[70];
char Pname[100];
double Price;
int Quantity;
public:
Product();
Product(char[], char[], double, int);
double getPrice();
int getQuantity();
void setProductCode(char[]);
void setName(char[]);
void setPrice(double);
void setQuantity(int);
int fulfillOrder(int);
void print();
};
Product::Product()
{
idNum[70] = '\0';
Pname[100] = '\0';
}
Product::Product(char ID[], char PN[], double Pr, int Qu)
{
setProductCode(ID);
setName(PN);
setPrice(Pr);
setQuantity(Qu);
}
double Product::getPrice()
{
cout << "Price: " << Price << endl;
return Price;
}
int Product::getQuantity()
{
cout << "Quantitiy: " << Quantity << endl;
return Quantity;
}
void Product::setProductCode(char ID[])
{
strcpy_s(idNum, ID);
}
void Product::setName(char PN[])
{
strcpy_s(Pname, PN);
}
void Product::setPrice(double newPrice)
{
if (newPrice >= 0)
{
Price = newPrice;
}
else
{
Price = 0;
}
}
void Product::setQuantity(int newQuantity)
{
if (newQuantity >= 0)
{
Quantity = newQuantity;
}
else
{
Quantity = 0;
}
}
int Product::fulfillOrder(int orderq)
{
if (orderq<0)
{
cout << "Error" << endl;
orderq = 0;
cout << "Shipped: " << orderq << endl;
}
else if (orderq <= Quantity)
{
orderq = Quantity;
Quantity -= orderq;
cout << "Shipped: " << orderq << endl;
}
else
{
orderq = Quantity;
orderq = 0;
cout << "Shipped: " << orderq << endl;
}
return orderq;
}
void Product::print()
{
do
{
Quantity * .1;
} while (Quantity>10);
cout << "Product ID: " << idNum << " Product Name: " << Pname << " Price: " << Price << " Quantity: " << Quantity << endl;
}
int main()
{
inFile.open("C:\\Users\\Spectre\\Desktop\\C++ Work\\Store Sales.txt");
Product product1 = Product();
cout << "Product 1" << endl;
product1.getPrice();
product1.getQuantity();
product1.print();
Product product2 = Product();
cout << "Product 2" << endl;
product2.getPrice();
product2.getQuantity();
product2.print();
Product product3 = Product();
cout << "Product 3" << endl;
product3.getPrice();
product3.getQuantity();
product3.print();
Product product4 = Product();
cout << "Product 4" << endl;
product4.getPrice();
product4.getQuantity();
product4.print();
Product product5 = Product();
cout << "Product 5" << endl;
product5.getPrice();
product5.getQuantity();
product5.print();
{
cout << "Unable to open the selected file. Please try again or choose another file.";
//exit(0);
}
system("pause");
return 0;
}
我不确定是否因为输入了txt值的方式,或者它是否从txt文件中获取值。
答案 0 :(得分:1)
您的默认Product
构造函数未初始化Quantity
或Price
,并且它引用了idNum
和Pname
中的无效索引。所有这些都会导致未定义的行为。
答案 1 :(得分:1)
问题是
do
{
Quantity * .1;
} while (Quantity>10);
上面的表达没有用,可能会导致问题,因为没有使用表达式结果
另一个问题是
void Product::setProductCode(char ID[])
{
strcpy_s(idNum, ID);
}
void Product::setName(char PN[])
{
strcpy_s(Pname, PN);
}
将其更改为
void Product::setProductCode(char ID[])
{
strcpy(idNum, ID);
}
void Product::setName(char PN[])
{
strcpy(Pname, PN);
}
更改后我可以看到输出
Product 1
Price: 0
Quantitiy: 1606416072
Product ID: Product Name: Price: 0 Quantity: 1
Product 2
Price: 0
Quantitiy: 0
Product ID: Product Name: Price: 0 Quantity: 0
Product 3
Price: 0
Quantitiy: 0
Product ID: Product Name: Price: 0 Quantity: 0
Product 4
Price: 0
Quantitiy: 0
Product ID: Product Name: Price: 0 Quantity: 0
Product 5
Price: 0
Quantitiy: 0
Product ID: Product Name: Price: 0 Quantity: 0
Unable to open the selected file. Please try again or choose another file.sh: pause: command not found
Program ended with exit code: 0