将程序转换为结构体和类 [英] to convert program to structuer and classes

查看:113
本文介绍了将程序转换为结构体和类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我将此程序转换为structuer和class

Plz help me to convert this program to structuer and class

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int values=0;		// global variable to hold number of entries in the arrays
struct Student
{
	int studentID;
	int totalCredits;
	string gpa; 
	string major;
	string department;
	string college;
};
void ReadData (Student[]);
void ShowData (Student[]);
void StopPrint (Student[]);
void DisplayMenu(int&, int&);
void FindStud(Student[]);
void AddStud(Student[], int);
void DeleteStud(int[], int);
bool found (int[], int, int&);
int main()
{
	Student info[50];
	int option;
	int id;
	ReadData (info);
	ShowData (info);
	do
	{
		
		option=0;
		DisplayMenu(option, id);
		if (option == 1) FindStud (info);
		else if (option == 2) AddStud (info, id);
		else if (option == 3) DeleteStud(info, id);
	} while (option != 4);
	StopPrint (info);
	
	return 0;
}
bool found (Student studentID, int id, int& loc)
{
	bool flag=false;
	for (loc=0; loc<values; loc++)
		if (info[loc].studentID==id)
		{
			flag=true;
			break;
		}
	return flag;
}

void DeleteStud(Student[], int id)
{
	int n;
	if (found (sid, id, n))
	{
		cout<<"Student with id= "<<id<<" has been deleted"<<endl;
		sid[n]=0;
	}
	else cout<<"Student with id = "<<id<<" was not found."<<endl;
}

void AddStud (Student[])
{
 	int n;
	if (!found(sid, id, n))
	{
		
		cout<<endl;
		sid[values]=id;
		cout<<"Enter Student First Name:";
		cin>>fname[values];
		cout<<"Enter Student Major:";
		cin>>major[values];
		cout<<"Enter Student Gpa:";
		cin>>gpa[values];
		cout<<"Enter Student Credits:";
		cin>>hrs[values];
		 
	}
	else
		cout<<"Student with id = "<<id<<" already exists."<<endl;
	values++;
}

void FindStud (Student[])
{
 	int n;
	if (found(sid, id, n))
	{
		cout<<endl;
		cout<<setw(20)<<"Student Id:"<<setw(20)<<sid[n]<<endl;
		cout<<setw(20)<<"First Name:"<<setw(20)<<fname[n]<<endl;
		cout<<setw(20)<<"Major:"<<setw(20)<<major[n]<<endl;
		cout<<setw(20)<<"Gpa:"<<setw(20)<<gpa[n]<<endl;
		cout<<setw(20)<<"Total Credits:"<<setw(20)<<hrs[n]<<endl;
	}
	else
		cout<<"Student with id = "<<id<<" does not exist."<<endl;
}
		

void DisplayMenu(int& option, int& id)
{
	cout<<"\n\nStudent Information System"<<endl;
	cout<<" Comp 2102, Assignment 2"<<endl;
	cout<<"         Fall 2009"<<endl;
	cout<<"1. Find Student Data"<<endl;
	cout<<"2. Add New Student"<<endl;
	cout<<"3. Delete Student Data"<<endl;
	cout<<"4. Stop and Print Report"<<endl;
	cout<<"\nEnter your option: ";
	 
	cin>>option;
	if (option>=1 && option<4)
	{
		cout<<"Enter the id: ";
		cin>>id;
	}
	else if (option == 4) cout<<"Thank you. Here is the final report\n"<<endl;
	else 
	{
		cout<<"Option is not valid. Please re-enter your new option again\n"<<endl;
		cin.clear();
		cin.ignore(200, ''\n'');
	}
		 
	
}

void ReadData (Student[])
{
	ifstream indata;
	indata.open ("P2InputData.dat");
	indata>>sid[values]>>fname[values]>>major[values]>>gpa[values]>>hrs[values];
	while (indata)
	{					 
		values++;
		indata>>sid[values]>>fname[values]>>major[values]>>gpa[values]>>hrs[values];
	}
	 
	indata.close();
}

void ShowData (Student[])
{
	int n=0;
	while (n < values)
	{
		cout<<sid[n]<<setw(10)<<fname[n]<<setw(10)<<major[n]<<setw(6)<<gpa[n]<<setw(6)<<hrs[n]<<endl;
		n++;
	}
	 
}
// I am prinint the final results on the screen
void StopPrint (Student[])
{
	int n=0;
	while (n < values)
	{
		if (sid[n] > 0)	cout<<sid[n]<<setw(10)<<fname[n]<<setw(10)<<major[n]<<setw(6)<<gpa[n]<<setw(6)<<hrs[n]<<endl;
		n++;
	}
	 
}

推荐答案

您需要什么帮助"?

-Saurabh
What "help" do you require?

-Saurabh


这篇关于将程序转换为结构体和类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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