Reputation: 1
I'm trying to have several store items display in a dialog box.
I'm trying to get the values from a txt file to line up with each segment (ID Number, Price, etc I'm not even sure if that's right), but when I run it, the dialog box shows completely different values for everything.
Here's the txt file (named Store Sales.txt)
0-8053-7442-6 Problem Solving with 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
And this is the code I've gotten so far:
#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;
}
I'm not sure if it's because of the way the txt values are typed or if it's something with getting the values from the txt file.
Upvotes: 0
Views: 49
Reputation: 3632
Problem is
do
{
Quantity * .1;
} while (Quantity>10);
Above expression is of no use and might be causing problem as the expression result is not used
Another problem is
void Product::setProductCode(char ID[])
{
strcpy_s(idNum, ID);
}
void Product::setName(char PN[])
{
strcpy_s(Pname, PN);
}
change it to
void Product::setProductCode(char ID[])
{
strcpy(idNum, ID);
}
void Product::setName(char PN[])
{
strcpy(Pname, PN);
}
After changing those I can see the output
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
Upvotes: 1
Reputation: 3779
Your default Product
constructor doesn't initialize Quantity
or Price
and it references invalid indexes in both idNum
and Pname
. All of this results in undefined behavior.
Upvotes: 1