0

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.

AleXelton
  • 767
  • 5
  • 27

2 Answers2

1

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.

Michael Albers
  • 3,721
  • 3
  • 21
  • 32
1

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
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
  • When I did the strcpy change, it said that the strcpy was unsafe. I think I had the "_s" there to stop these errors. Is "_s" bad coding or something? – Rez The Ripper Aug 04 '17 at 03:19
  • `strcpy_s` is a C function that is not guaranteed to exist in C++. See [this question](https://stackoverflow.com/questions/36723946/why-does-strcpy-s-not-exist-anywhere-on-my-system). If it exists on your machine, then that's a compiler extension, but it's non-portable. (In fact, I tried to run your code, and those `strcpy_s` calls don't compile on g++ 5.4.0 for me) – Silvio Mayolo Aug 04 '17 at 03:22
  • I'm using Visual Studio 2017. Is there another way to do that segment and get the same results? – Rez The Ripper Aug 04 '17 at 03:31