0

Program details: Program will display menu which will have 4 options

  1. New Enrollment of student
  2. editing students detail
  3. Updating details
  4. Show list of all students

If enrollment exceeds 45 it should give a message. There should be use of structures and functions. separate function for enrollment, edit and update so goes on. I have question from the code I have written. I am having confusion on how to use structure with function. I donot know if I am right or wrong. How to use pointers in this situation??

My updated code but still giving weird output, How to use functions with pointer when we are storing multiple data like in this code Data data[45].

#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;


struct Date{
int day;
int month;
int year;


};

struct Data{
int id;
char firstName[20];
char lastName[20];
float PrimaryMarks;
float secondaryMarks;
Date date;
};



void enrollment(Data *dtai){
        static int i=0;

        if(i<45){
        dtai->id=i+1;


        cout<<"Enter the student's First Name"<<endl;
        cin>>dtai->firstName;


        cout<<"Enter the student's Last Name"<<endl;
        cin>>dtai->lastName;

        cout<<"Enter the student's Primary School Percentage"<<endl;
        cin>>dtai->PrimaryMarks;


        cout<<"Enter the student's Secondary School Percentage"<<endl;
        cin>>dtai->secondaryMarks;


        cout<<"Enter the day of enrollment"<<endl;
        cin>>dtai->date.day;

        cout<<"Enter the month of enrollment"<<endl;
        cin>>dtai->date.month;

        cout<<"Enter the year of enrollment"<<endl;
        cin>>dtai->date.year;
        }

i++;
}


int main(){ 
//taking students information menu display
Data data[45];

//int i=0;
int option;
char sentinal;

do{ 

int x=0;
//display menu
cout<<"Press 1 for New Enrollment"<<endl;
cout<<"Press 2 for editing student's detail"<<endl;
cout<<"Press 3 for updating student's detail"<<endl;
cout<<"Press 4 to see list of students"<<endl;

cin>>option;    

    switch(option){
        case 1:

            enrollment(&data[x]);
            break;
        case 2:

        case 4:


    }




cout<<"Press m to go to the menu again ";
cin>>sentinal;

}while(sentinal=='m');



return 0;
}
  1. Please tell me how to use structures with functions for multiple data.

I have just written my code for 1st option enrollment rest are remaining, Please answer my above question Thanks in advance

user3100177
  • 65
  • 1
  • 9

3 Answers3

2

To use a structure with a function, change the function slightly, so it receives a parameter:

void enrollment(Data& data_to_fill)
{
    ...
    cin>>data_to_fill.firstName;
    ...
}

Then, send the parameter when calling the function:

enrollment(data[i]);

Alternatively, use the return value instead of a parameter:

Data enrollment()
{
    Data data_to_fill;
    ...
    cin>>data_to_fill.firstName;
    ...
    return data_to_fill;
}

...

data[i] = enrollment();

I don't know which way to choose, and it may be difficult to recommend one or the other. The first way uses the pass-by-reference technique, which you might not be familiar with - so you probably want to use the second way.

However, if there is an error (more than 45 enrollments), the function should probably return an error code. The first way might be more compatible with this requirement.

Just to answer your other questions:

I have declared it globally

It's considered bad style. Declare it locally in the main() function.

How to link two structures, Date and Data

Just like you did it in your code: struct Data {Date date;}

Will there be a use of pointers or not?

You don't need pointers here.

Have I used static variable in a function correctly?

Almost correctly - just add ++i so it will count the enrollments.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • Thanks a lot. Can u guide me a little but, I am actually new to c++, from where can i find good tutorials?? or good reference – user3100177 Dec 14 '13 at 19:20
  • As mentioned in the comments, read the [C++ definitive book list](http://stackoverflow.com/q/388242/509868) – anatolyg Dec 14 '13 at 19:22
1

Having a globally declared structure as you do (data[45]) is one way of going about it. It's not a great idea to have functions modifying global variables (over the course of development you may end up with forgotten global-variable-modifications happening inside other functions). If you must use a global variable, consider making it obviously global via a naming convention - e.g. gdata[45].

To avoid having this be globally declared, declare it in main and pass the pointer to the enrollment function. In fact, wouldn't it make more sense to have the student counter being incremented in main, and passing the pointer to that element of the array of Data structs?

void enrollment(Data *mydatai){
    /* stuff */
    cin>>mydatai->firstName;
    /* more stuff */
}

int main(){
    /* stuff */
    Data data[45];
    int i = 0;
    do { 
      /* other stuff */
      switch(option){
        case 1:
          enrollment(&data[i]);
        /* other cases */
      } // switch end
    } while (some_condition);
}  

The way that a Date struct is inside a Data struct is fine - but that doesn't mean it's necessarily what you want.

kbshimmyo
  • 578
  • 4
  • 13
  • So u declared i inside main, How will i increment?? I also have to use i for id generate – user3100177 Dec 14 '13 at 20:43
  • Not working for multiple data just for single data – user3100177 Dec 14 '13 at 21:08
  • @user3100177 _'So u declared i inside main, How will i increment??'_ Just put a `++i;` statement inside the `do`/`while` loop. – πάντα ῥεῖ Dec 15 '13 at 08:23
  • Data data[45], how will i use pointer in this case, above code is only working for single data – user3100177 Dec 15 '13 at 09:23
  • You should look at pointers in a good reference (as suggested by earlier commenters) and try things you think should work. If they don't work but you think they should, then we can answer your question. While Kernighan & Ritchie is not specific to C++, it has a great explanation of pointers. – kbshimmyo Dec 17 '13 at 14:35
0

To pass a single data structure to your enrollment function you'll need to write

void enrollment(Data& data) {
    // ...
}

and call it

enrollment(data[i]);

from you loop in main.

To pass the entire struct array you can either do it fixed size

void enrollment(Data data[45]) { // ...

or do it dynamically sized

void enrollment(Data* data, std::size_t maxRecords) { // ...
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190