结构,功能和数组 [英] Structures, Functions and arrays

查看:123
本文介绍了结构,功能和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计划的详细信息:程序会显示菜单,这将有4个选项


  1. 学生新注册

  2. 编辑详细的学生

  3. 更新详情

  4. 所有学生的名单显示

如果报名超过45就应该发出的信息。应该有使用结构和功能。入学,编辑和更新单独的函数等等不胜枚举。我已经从code我写的问题。我有关于如何使用结构功能的混乱。我DONOT知道,如果我是对还是错。如何使用指针,在这种情况下??

我的更新code,但仍然给人怪异的输出,如何使用功能与指针,当我们存储此code数据数据,如多个数据[45]。

 的#include<&iostream的GT;
#包括LT&;&cctype GT;
#包括LT&; CString的GT;
使用命名空间std;
结构日期{
日整型;
INT月;
INT年;
};结构数据{
INT ID;
焦炭的firstName [20];
焦炭的lastName [20];
浮PrimaryMarks;
浮secondaryMarks;
日期日期;
};报名无效(数据* dtai){
        静态INT I = 0;        如果(ⅰ&下; 45){
        dtai-和SEQ ID = i + 1的;
        COUT<<输入学生的名字<< ENDL;
        CIN>> dtai->的firstName;
        COUT<<输入学生的姓<< ENDL;
        CIN>> dtai-> lastName的;        COUT<<输入学生的小学百分比<< ENDL;
        CIN>> dtai-> PrimaryMarks;
        COUT<<输入学生的中学百分比<< ENDL;
        CIN>> dtai-> secondaryMarks;
        COUT<<输入注册的日子<< ENDL;
        CIN>> dtai-> date.day;        COUT<<输入注册的月份<< ENDL;
        CIN>> dtai-> date.month;        COUT<<输入入学当年<< ENDL;
        CIN>> dtai-> date.year;
        }我++;
}
诠释主(){
//以学生信息菜单显示
数据数据[45];// INT I = 0;
INT选项;
焦炭前哨;做{INT X = 0;
//显示菜单
COUT<<$ P $ 1 PSS新招生<< ENDL;
COUT<<&LT编辑学生的详细preSS 2;< ENDL;
COUT<<&LT更新学生的详细preSS 3;< ENDL;
COUT<<preSS四看学生的名单<< ENDL;CIN>>选项;    开关(选配件){
        情况1:            登记(安培;数据[X]);
            打破;
        案例2:        情况4:
    }
COUT<<preSS米进入菜单再;
CIN>>哨点;}而(前哨=='M');返回0;
}


  1. 请告诉我如何使用与结构,多个数据的功能。

我刚才写我的code对于第一种选择招生其余剩下的,请回答我上面的问题在此先感谢


解决方案

要使用结构,功能,改变功能咯,所以它接收的参数:

 无效报名(数据&安培; data_to_fill)
{
    ...
    CIN>> data_to_fill.firstName;
    ...
}

然后,调用函数时发送的参数:

 入学(数据由[i]);

另外,使用返回值,而不是一个参数:

 数据招生()
{
    数据data_to_fill;
    ...
    CIN>> data_to_fill.firstName;
    ...
    返回data_to_fill;
}...数据[I] =招生();

我不知道选择哪个方式,并且它可能难以推荐的一个或另一个。第一种方式使用通通过引用技术,它与你可能并不熟悉 - 所以你可能要使用的第二种方式

然而,如果有错误(超过45入学),该函数应该可能返回一个错误code。第一种方式可能与这一要求更兼容。

只是为了回答您的其他问题:


  

我已经在全球范围宣布它


它认为是不好的风格。局部声明它在的main()功能。


  

如何两种结构,日期和数据链路


就像你这样做是在code:结构数据{日期日期;}


  

会不会有指针的使用与否?


您不需要指针在这里。


  

我有一个函数正确使用静态变量?


几乎正确的 - 只需要添加 ++我所以它会算入学率

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

解决方案

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.

这篇关于结构,功能和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆