I need immediate help here. Please help me! :)
I just can't figure out why my program is crashing while executing it.
The compiling of the program is fine. (no syntax errors)


I used Dev C++ 4.9 for this version.

#include <iostream>
#include <conio.h>
#include <iomanip>
#include <string>
using namespace std;

const double t = 0.20; // tax

struct menuItemtype
{
       string fm; // food menu
       double fp; // food price
       double ad; // amount due
       };
       
menuItemtype getData();
void showMenu();       
int main()
{
showMenu();
 getche();
 return 0;   
}

menuItemtype getData()
{
  menuItemtype menuList[8] = { {"Plain Egg", 1.45}, {"Bacon and Egg", 2.45}, {"Muffin", 0.99}, 
  {"French Toast", 1.99}, {"Fruit Basket", 2.49}, {"Cereal", 0.69}, {"Coffee", 0.50}, {"Tea", 0.75}};    
return menuList[8];
 }

void showMenu()
{
     menuItemtype m[8]; // menu
    int i;
    m[8] = getData();
    for(i = 0; i < 8; i++)
    {
          cout << m[i].fm << "   " << m[i].fp << endl;
          }

 }

Anyway, it is not actually a complete work. I'm still going to add somethings in here..
I just want to know why it is crashing.

If you are going to use Visual Studio, here's a 2nd version of my work.

#include "iostream"
#include "stdfax.h"
#include "iomanip"
#include "string"
using namespace std;

const double t = 0.20; // tax

struct menuItemtype
{
string fm; // food menu
double fp; // food price
double ad; // amount due
};

menuItemtype getData();
void showMenu(); 
int main()
{
showMenu(); // displays food menu and food prices
system("pause");
 /* I am going to change this after I'm done with knowing why my program is crashing */
return 0; 
}

menuItemtype getData()
{
menuItemtype menuList[8] = { {"Plain Egg", 1.45}, {"Bacon and Egg", 2.45}, {"Muffin", 0.99}, 
{"French Toast", 1.99}, {"Fruit Basket", 2.49}, {"Cereal", 0.69}, {"Coffee", 0.50}, {"Tea", 0.75}}; 
return menuList[8];
}

void showMenu()
{
menuItemtype m[8]; // menu
int i;
m[8] = getData();
for(i = 0; i < 8; i++)
{
cout << m[i].fm << " " << m[i].fp << endl;
}

}

--------

Care to help me out?

Actually, the main problem here is the function named "showMenu". "showMenu" displays all of the food and their prices. I was thinking a while ago maybe the problem is with the calling of the function? what do you think?

There's a lot of things wrong with your program actually.
Let me point out a few:

1. You can't initialize an array of structs like that.
2. Your function getData() has a prototype where it returns 1 struct. But you create an array of 8 structs which you want to return
3. menuItemtype menuList[8] creates an array of 8 structs. That means 0-7, so when you do this: return menuList[8]; you're accessing the 9th elements and get an exception.
4. return menuList[8]; only returns ONE struct, but in you showmenu function you treat it like an array.
5. more.

I think you need to read up on how to use arrays, or better yet: vectors

Comments
Thank you this is very helpful :)

I see.. Thank you very much, Nick Evan. :)

Just what I need! But still I need another answer.
I'll just wait for more answers unil tomorrow.. I hope I'll get another reply soon fromt his thread. :)

But still I need another answer.

Yup, he's right. Nick's been doing this for a "couple" of years. I'm confused as to what you need more answers about?

I guess this thread is solved. Thank you very much once again, Nick Evan :) more power to you!

This question has already been answered. Start a new discussion instead.