在我的C ++程序非常奇怪的错误

我已经完成的程序在插入math部分之前工作得很好。 完成math部分后,我构build了代码,并没有出现错误。 但是,当我试图debugging我的程序,我得到这个提示:“在Reciept.exe中的0x4f7ccb1a(msvcr100d.dll)未处理的exception:0xC0000005:访问冲突写入位置0x4e65ab48”。

我首先想到这是math,但是我把它删除了,再次运行这个程序,以前想要的结果是没有的。 同样的提示出现了。

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

这是我的代码:

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

C和C ++中的数组是基于0的。 你已经声明:

 menuItemType menuList[8]; 

这意味着你可以索引元素0到7.但是你索引1到8.当你读或写索引8时,你会得到一个访问冲突。

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