2017-08-04 175 views
0

我試圖在對話框中顯示幾個商店項目。對話框中的奇怪符號

我想從一個txt文件中的值與每個段(ID號碼,價格等我排隊,我甚至不知道如果這是正確的)排隊,但是當我運行它時,對話框顯示一切完全不同的價值觀。

這裏的txt文件(名爲商店Sales.txt

0-8053-7442-6問題與C++ 1 45.50

7751158146個Looney Tunes的軟盤解決12 8.50

88869809780惠普激光打印機紙3 10.99

2429412454第2號鉛筆24 0.10

4895469286注座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文件獲取值的東西。

回答

1

您的默認構造函數Product不初始化QuantityPrice,並在這兩個idNumPname引用無效索引。所有這些都會導致未定義的行爲。

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 
+0

當我做了strcpy的變化,它表示的strcpy是不安全的。我想我有那個「_s」來阻止這些錯誤。是「_s」壞編碼還是什麼? –

+0

'strcpy_s'是C函數,不保證在C++中存在。看到[這個問題](https://stackoverflow.com/questions/36723946/why-does-strcpy-s-not-exist-anywhere-on-my-system)。如果它存在於你的機器上,那麼這是一個編譯器擴展,但它是不可移植的。 (實際上,我試圖運行你的代碼,而那些'strcpy_s'調用不能在g ++ 5.4.0上編譯) –

+0

我正在使用Visual Studio 2017.是否有另一種方法來實現該段並獲得相同的結果結果如何? –