Очень странная ошибка в моей программе на C++ [закрыто]



Моя программа, которую я завершил, работала просто отлично, прежде чем я вставил в нее математическую часть. После того, как я закончил математическую часть, я построил код, и никаких ошибок не возникло. Однако, когда я попытался отладить свою программу, и я получил это приглашение: "необработанное исключение в 0x4f7ccb1a (msvcr100d.dll) в Reciept.exe: 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.75n" << 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");
}
407   2  

2 ответов:

Массивы в C и C++ основаны на 0. Вы провозгласили это:

menuItemType menuList[8];

Это означает, что вы можете индексировать элементы от 0 до 7. Но вы индексируете от 1 до 8. При чтении или записи в индекс 8 вы получите нарушение доступа.

Индекс массива в C++ начинается с 0, а не с 1.

Comments

    Ничего не найдено.