C ++-<错误阅读=“” character =“” of =“” string =“”>和“访问冲突写入位置0x86A1ECD8”

时间:2018-08-31 13:44:30

标签: c++

我正在尝试为我的学校项目制作百货商店程序。 我为产品创建了一个类数组,以包括它们的名称,价格,库存和所购商品的数量。 但是由于某种原因,我在弹出窗口时出现错误
    “在Test project.exe中的0x68665139(vcruntime140d.dll)处引发了异常:      0xC0000005:访问冲突写入位置0x86A1ECD8。 I get this error just after the string input function inside the class

另外,在执行后,我收到一个称为读取字符串的错误的错误。

error reading characters of string

这是我的代码:

#include<iostream>
#include<string>


using namespace  std;

class product {
 private:
    int stock;       //items in stock
    int add_stock;   //added stock
    int purchased;   //purchased items(reduces stock)
    string name;     //name of item
    float price;    //price of item

    public:

//fuctions to input all private variables
void setstock(int x) {
    stock = x;
}
void setaddstock(int x) {
    add_stock = x;
}
void setpurchased(int x) {
    purchased = x;
}
void setname(string x) {
    name = x;
}
void setprice(float x) {
    price = x;
}

//functions to output all private variables
int getstock() {
    return stock;
}
int getaddstock() {
    return add_stock;
}
int getpurchased() {
    return purchased;
}
string getname() {
    return name;
}
float getprice() {
    return price;
}

//function that restocks the items
void restock() {
    stock += purchased;
}

//function that deducts purchased items from stock
void destock() {
    if (stock >= purchased) {
        stock -= purchased;
    }
    else {                                                              //in case the purchase demand exceeds items in stock
        cout << "\nSorry we only have " << stock << "amount of left\n";
    }
}
};




int main() {
int purchased;
product stuff[10];              //class array
int choice, total_qty = 0;      //choice-> to choose between products, 
total_qty-> total number of products purchased
char yesno;                     //to choose if user wants to buy anything 
else (for do loop)
float total_price = 0;          //total amount of money to be paid 


// declaring product name and price 

 stuff[1].setname  ("Coconut biscuits");                         
stuff[1].setprice  (12.0);
stuff[2].setname  ("Wai Wai noodles");                      stuff[2].setprice  (20.0);
stuff[3].setname  ("Cadbury Dairy Milk");                   stuff[3].setprice  (45.5);
stuff[4].setname  ("Lays");                                 stuff[4].setprice  (50.0);
stuff[5].setname  ("Rara Noodles");                         stuff[5].setprice  (18.5);
stuff[6].setname  ("Khajurko Puff");                        stuff[6].setprice  (50.0);
stuff[7].setname  ( "Nanglo Doughnut");                     stuff[7].setprice  (15.0);
stuff[8].setname  ( "Nanglo whole-wheat bread");            stuff[8].setprice  (65.0);
stuff[9].setname  ("Dabur Real fruit juice");               stuff[9].setprice  (30.0);
stuff[10].setname ("Coca-Cola");                            stuff[10].setprice (35.5);


// declairing the number of items in stock and setting purchased = 0 for easy calculation  
for (int i = 1; i <= 10; i++) {
    stuff[i].setstock     (100);
    stuff[i].setpurchased (0);
}


// displays the menu, make purchase, repeat
cout << "What would you like to buy?\n\n\n";

do {

    for (int i = 1; i <= 10; i++) {
        cout << i << ". " << stuff[i].getname() << "\n\n";      //displays menu in format: 1. Biscuit
    }

    cout << "Enter your choice: ";
    cin >> choice;

    cout << "\n\nHow many of it would you like to buy?";
    cin >> purchased;
    stuff[choice].setpurchased(purchased);

    stuff[choice].destock();                            //function for destocking


    cout << "\n\nWould you like to buy other items?";
    cin >> yesno;

} while (yesno == 'y' || yesno == 'Y');

cout << "\n\n\n";               //line spacing, nothing cool here


//this for loop calculates total quantity and price as well as  displays the receipt
for (int i = 1; i <= 10; i++) {
    total_qty += stuff[i].getpurchased();                                   //total quantity
    total_price += stuff[i].getpurchased() * stuff[i].getprice();                   //total price


    //only displays if stuff is purchased
    if (stuff[i].getpurchased() > 0) {

        //format: 1. Biscuit 4 10 40 
        cout << i << " " << stuff[i].getname() << " " << stuff[i].getpurchased() << " " << stuff[i].getprice() << " " << stuff[i].getpurchased()*stuff[i].getprice() << "\n";
    }
}

// displays total price and quantity
cout << "\ntotal quantity: " << total_qty;
cout << "\ntotal price: " << total_price;



return 0;
}

1 个答案:

答案 0 :(得分:0)

for (int i = 1; i <= 10; i++)

应该是

for (int i = 0; i < 10; i++)

您超出了产品范围。