2

在我插入数学部分之前,我完成的程序运行良好。完成数学部分后,我构建了代码,没有出现错误。但是,当我尝试调试我的程序时,我得到了这个提示:“Reciept.exe 中 0x4f7ccb1a (msvcr100d.dll) 处的未处理异常:0xC0000005:访问冲突写入位置 0x4e65ab48。”

我一开始以为是数学问题,但后来我删除了它并再次运行程序,而应该像以前那样出现的结果却没有。同样的提示出现了。

你能告诉我发生了什么事吗?

这是我的代码:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct menuItemType
{
    string menuItem;
    double menuPrice;
    double sum;
    double amountTotal;
    double tax;
};

void getData(menuItemType menuList[8]);
void printCheck(menuItemType menuList[8]);
void showMenu(menuItemType menuList[8]);

int main()
{
    menuItemType menuList[8];
    getData(menuList);
    showMenu(menuList);
    printCheck(menuList);

    system ("pause");
    return 0;
}

void getData(menuItemType menuList[8])
{
    menuList[1].menuItem = "Plain Egg"; 
    menuList[1].menuPrice = 1.45;
    menuList[2].menuItem = "Bacon and Egg";
    menuList[2].menuPrice = 2.45;
    menuList[3].menuItem = "Muffin";
    menuList[3].menuPrice = 0.99;
    menuList[4].menuItem = "French Toast";
    menuList[4].menuPrice = 1.99;
    menuList[5].menuItem = "Fruit Basket";
    menuList[5].menuPrice = 2.49;
    menuList[6].menuItem = "Cereal";
    menuList[6].menuPrice = 0.69;
    menuList[7].menuItem = "Coffee";
    menuList[7].menuPrice = 0.50;
    menuList[8].menuItem = "Tea";
    menuList[8].menuPrice = 0.75;
}

 void showMenu(menuItemType menuList[8])
 {
    cout << "Please enter the numbers  beside the product that you 
        would like to  have today.
        When you are finished press 0.\n" << endl;
    cout << "1 - Plain Egg" << setw(14) << "$1.45" << endl;
    cout << "2 - Bacon and Egg" << setw(10) << "$2.45" << endl;
    cout << "3 - Muffin" << setw(17) << "$0.99" << endl;
    cout << "4 - French Toast" << setw(11) << "$1.99" << endl;
    cout << "5 - Fruit Basket" << setw(11) << "$2.49" << endl;
    cout << "6 - Cereal" << setw(17) << "$0.69" << endl;
    cout << "7 - Coffee" << setw(17) << "$0.50" << endl;
    cout << "8 - Tea" << setw(21) << "$0.75\n" << endl;
}

void printCheck(menuItemType menuList[8])
{
    int selections = 1;

    while(selections != 0)
    {
        cout << "\n Please enter one of the choice from our menu: ";
        selections += selections;
        cin >> selections;

        switch(selections)
        {
            case 0:
            break;

            case 1:
            cout << menuList[1].menuItem << setw(14) << "$1.45";
            break;
            case 2:
            cout << menuList[2].menuItem << setw(10) << "$2.45";
            break;
            case 3:
            cout << menuList[3].menuItem << setw(17) << "$0.99";
            break;
            case 4:
            cout << menuList[4].menuItem << setw(11) << "$1.99";
            break;
            case 5:
            cout << menuList[5].menuItem << setw(11) << "$2.49";
            break;
            case 6:
            cout << menuList[6].menuItem << setw(17) << "$0.69";
            break;
            case 7:
            cout << menuList[7].menuItem << setw(17) << "$0.50";
            break;
            case 8:
            cout << menuList[8].menuItem << setw(20) << "$0.75";
            break;
            default:
            cout << "The number you just enter is not between 1 and 8. Please try again.\n";
            break;


            //  const double tax = 0.05; 
            int x;
            double amountTotal = 0;
            double tax = 0; 
            cout << endl;
            cout << endl;
            cout << endl;
            cout << " Welcome to Bry's Restaurant! " << endl; 
            cout << endl;

            for (x=0; x<2; x++) 
            { 
                cout.precision(2);
                cout << showpoint;
                tax = tax + ((menuList[x].menuPrice) * 0.10); 
                    cout << menuList[x].menuItem << setw(16) << "$ " << menuList[x].menuPrice <<  endl; 
            }
            cout.precision(2);
            cout << showpoint;
            cout<<"     Tax:                         $ " << tax << endl; 
            cout.precision(2);
            cout << showpoint;
            //amount
            for (x=0; x<2; x++) 
            { 
                cout.precision(2);
                cout << showpoint;
                amountTotal = amountTotal +(menuList[x].menuPrice);
            }
            amountTotal = amountTotal + tax;
            cout.precision(3);
            cout << showpoint;
            cout<<"     Amount Total:                  $ " << amountTotal <<endl;
        }
    }
    cout << "\n Thank you for coming to Bry's Restaurant! Have a blessed day!" << endl;
    system ("pause");
}
4

2 回答 2

7

C 和 C++ 中的数组是从 0 开始的。您已声明:

menuItemType menuList[8];

这意味着您可以索引元素 0 到 7。但是您正在索引 1 到 8。当您读取或写入索引 8 时,您将遇到访问冲突。

于 2013-04-24T02:14:40.407 回答
0

C++ 中的数组索引从 0 开始,而不是 1。

于 2013-04-24T02:15:11.390 回答