和“访问冲突写入位置 0x86A1ECD8”,c++"/>

首页 > 解决方案 > C++ -和“访问冲突写入位置 0x86A1ECD8”

问题描述

我正在尝试为我的学校项目制作一个百货商店项目。我为产品创建了一个类数组,以包括它们的名称、价格、库存和所购商品的数量。但由于某种原因,我收到一个错误弹出窗口
“Test project.exe 中的 0x68665139 (vcruntime140d.dll) 抛出异常:0xC0000005:访问冲突写入位置 0x86A1ECD8。” 我在类中的字符串输入函数之后收到此错误

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

读取字符串字符时出错

这是我的代码:

#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;
}

标签: c++

解决方案


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

应该

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

您正在超出您的产品系列。


推荐阅读