我的C ++程序中的奇怪错误

时间:2013-04-24 02:12:08

标签: c++ windows

在我插入数学部分之前,我完成的程序工作得很好。完成数学部分后,我构建了代码,没有出现任何错误。但是,当我尝试调试我的程序时,我得到了这个提示:“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");
}

2 个答案:

答案 0 :(得分:7)

C和C ++中的数组是基于0的。你宣布了​​这个:

menuItemType menuList[8];

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

答案 1 :(得分:0)

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