本文介绍了任何人都可以帮我把这个C cpde转换成C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
struct college
{
	int roll,year;
	char name[50],depart[50],crs[50];
}c[4];  //Number of student
int main()
{
	int i,no,year1,roll_no;
	printf("===========Enter Student's Details==========\n");
	for(i=0;i<=3;i++)
	{
		printf("\nEnter Roll no %d : ",i+1);
		scanf("%d",&c[i].roll);
		printf("\nEnter Name %d : ",i+1);
		fflush(stdin);
		scanf("%s",&c[i].name);
		printf("\nEnter Department %d : ",i+1);
		fflush(stdin);
		scanf("%s",&c[i].depart);
		printf("\nEnter Course %d : ",i+1);
		fflush(stdin);
		scanf("%s",&c[i].crs);
		printf("\nEnter Joining Year %d : ",i+1);
		scanf("%d",&c[i].year);
		printf("\n");
	}
	printf("================================================");
	printf("\n\n");
	printf("\n==============Student Info==============\n");
	printf("\n1.Names of all students who joined in a particular year :\n");
	printf("\n2.Find data of student by its roll number :\n");
	printf("\n\nEnter your choice : ");
	scanf("%d",&no);
	switch(no)
	{
		case 1:
			printf("Enter joining year : ");
			scanf("%d",&year1);
			printf("\n\n===============List of student who join in %d year=============\n\n",year1);
			for(i=0;i<=3;i++)
			{
				if(c[i].year==year1)
				{
					printf("\nRoll no : %d ||Name : %s ||Department : %s ||Course : %s ||Year : %d",c[i].roll,c[i].name,c[i].depart,c[i].crs,c[i].year);
				}
			}
			break;
		case 2:
			printf("\nEnter Roll number of student :");
			scanf("%d",&roll_no);
			printf("\n\n===============Student Number : %d =============\n\n",roll_no);
			for(i=0;i<=3;i++)
			{
				if(c[i].roll==roll_no)
				{
					printf("\nRoll no   : \t%d\nName      :  \t%s\nDepartment: \t%s\nCourse    :   \t%s\nYear      :\t%d",c[i].roll,c[i].name,c[i].depart,c[i].crs,c[i].year);
				}
			}
			break;
		default:
			printf("\nInvalid choice ");
	}
	return 0;
}





我的尝试:





What I have tried:

#include<iostream>
using namespace std;

struct student{

	char name [15];
	int rollno;
	char department [15];
	char course [10];
	int year;

}; student s;

int main(){

	int *ptr , p;
	int year_u, rollno_u;

	int i;
	for(i=0;i<4;i++){
		cout<<"\nEnter Name: ";
		cin.getline(name, 15);

		cout<<"\nEnter Roll no: ";
		cin>>rollno;

		cout<<"\nEnter Department: ";
		cin.getline(department, 15);

		cout<<"\nEnter Course: ";
		cin.getline(course, 10);

		cout<<"\nEnter year: ";
		cin>>s.year;

		system("cls");
	}
	int choice;
	cout<<"\nEnter your choice: ";
	cin>>choice;

	switch(choice){
		case 1:
			cout<<"\nEnter year: ";
			cin>>year_u;
			system("cls");
		for(i=0;i<3;i++){

			cout<<"\nStudents in : "<<year_u;
			cout<<"\n\n Name: "<<name[i];
			cout<<"\nRoll No: "<<rollno[i];
			cout<<"\nDepartment and Course: "<<department[i]<<" "<<course[i];

		}
		case 2:
			cout<<"\nEnter a roll no: ";
			cin>>rollno_u;
			system("cls");
		for(i=0;i<3;i++){

		}

	}

}







和更改但仍然出错......




and changing but still getting errors.....

推荐答案


#include <iostream>
#include <vector>
using namespace std;

class Student
{
  int roll, year;
  string name, depart, crs;

public:
  // getter and setter methods here, if required.
  Student();
  Student( int roll, string name, string depart, string crs, int year);
  friend ostream & operator << (ostream &, const Student & );
  friend istream & operator >> (istream &, Student & );
};

int main()
{
  vector<Student> student;
  for (size_t n=0; n<2; ++n)
  {
    Student s;
    cout << "please enter the details of the student " << n << "\n";
    cin >> s;
    student.push_back(s);
  }

  for ( const auto & s : student )
    cout << s << "\n";
}

Student::Student():
  roll(-1)
{
}

Student::Student( int roll, string name, string depart, string crs, int year):
  roll(roll), year(year), name(name), depart(depart), crs(crs)
{
}

ostream & operator << (ostream & os, const Student & s)
{
  os << s.roll << ", " << s.name << ", " << s.depart << ", " << s.crs << ", " << s.year;
  return os;
}

istream & operator >> (istream & is, Student & s)
{
  is >> s.roll >> s.name >> s.depart >> s.crs >> s.year;
  return is;
}


这篇关于任何人都可以帮我把这个C cpde转换成C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 22:19