C ++ - 和“访问冲突写入位置0x86A1ECD8”

问题描述 投票:0回答:1

我正在尝试为我的学校项目制作百货商店计划。我为产品创建了一个类数组,以包括它们的名称,价格,库存和购买项目的数量。但由于某种原因,我得到一个错误作为弹出窗口 “测试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++
1个回答
0
投票
for (int i = 1; i <= 10; i++)

应该

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

你超越了你的产品阵列。

© www.soinside.com 2019 - 2024. All rights reserved.