0

Actually there is a party thrown by a millionaire,i am trying to store their names and ages using a structure array of 2 guests, then at the end i am trying to display total number of guests and divide them by their age group. help me guys!this is my first time posting!spare my mistakes please!thank you!

#include "iostream"
#include "string"

using namespace std;
struct guests
{
    string name;
    int age;
};

int main()
{
    int i=0,j=0;
    guests guest[1];
    try
    {
        do{
            cout<<"enter your name"<<endl;
            getline(cin,guest[i].name).get();
            cout<<"enter your age"<<endl;
            cin>>guest[i].age;
            cin.get();
            i++;
        }
        while(i>0 && i<2);

        cout<<i<<"number of guests attended the party"<<endl;
        for(i=0;i<2;i++)
        {
            if((guest[i].age>19 && guest[i].age<31))
            {
                j++;
                cout<<j<<"number of guests of age group between 20 and 30 are her"<<endl;
            }
            else if((guest[i].age>29 && guest[i].age<41))
            {
                j=0;
                j++;
                cout<<j<<"number of guests of age group between 30 and 40 are here"<<endl;
            }
            else
                cout<<j<<"number of guests of agegroup between 40 and 50 are here"<<endl;
        }
    }
    catch (int e)
    {
        cout << "An exception occurred. Exception Nr. " << e << endl;
    }
    return 0;
    cin.get();
}
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
jeevanreddymandali
  • 395
  • 3
  • 8
  • 23
  • Can you give us some details about the error? – Paddyd Aug 16 '13 at 15:21
  • hello paddyd thanks for responding this fast, AndyG solved it answer is i used guest[1] but it should be guest[2], i am a beginner with a lot of interest in programming so i started to keep myself busy with these big ideas(atleast for me!haha..)...thanks again! – jeevanreddymandali Aug 16 '13 at 15:31
  • Once you fix the access violation, I strongly suggest running this through codereview.stackexchange.com. there is a plethora of problematic and/or ineffective code present. – WhozCraig Aug 16 '13 at 15:33

2 Answers2

3

It looks like your error is here:

guests guest[1];

You are only allocating enough memory for one guest. When you try to access the second guest, you are going out of bounds.

If you want to have two guests, declare your guest array as:

guests guest[2];

AndyG
  • 39,700
  • 8
  • 109
  • 143
  • thanks for responding this fast, AndyG, i am a beginner with a lot of interest in programming, so i started to keep myself busy with these big ideas(atleast for me!haha..)...thanks again! – jeevanreddymandali Aug 16 '13 at 15:32
0

You error is when you declare the array :

guests guest[1];

You want 2 guests but you are declaring an array of only one item. It can not works... You are having an out-of-bounds error.

You declare your array like that :

guests guest[2];
//           ^ An array of 2 items

But don't forget that accessing members begin at the index 0 :

guest[0];   // Access the first element of the array
guest[1];   // Access the second element
Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62