任何人都可以帮我把这个C cpde转换成C ++ [英] Can anyone help me convert this C cpde into C++

查看:124
本文介绍了任何人都可以帮我把这个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.....

推荐答案

将C代码更改为C ++很简单:使用不同的编译器就可以了。

它不会产生什么是好的C ++代码:C不是面向对象的语言,而C是。所以不要将C转换为C ++,使用C代码的算法编写新的C ++。



创建一个名为Name,Roll No和Department的类并使用它而不是单独的数组。让班级做一些操作它自己的数据的工作,而不是在 main 中完成所有工作。



抓住一堆代码并将其转换为C ++将不会给你一个好成绩!
Changing C code to C++ is simple: use a different compiler and it'll work.
What it won't produce is good C++ code: C is not an object oriented language, and C is. So don't "convert C to C++", write new C++ using the algorithms of the C code.

Create a class with a Name, Roll No, and Department and use that instead of separate arrays. Let the class do the work of manipulating it's own data, instead of doing it all in main.

Just grabbing a lump of code and "converting it to C++" won't get you a good grade!


我给你的应用程序的骨架,你必须填写细节,但然后你必须知道 C ++ 编程语言。



I am giving you the skeleton of the application, you have to fill the details, but then you have to know the C++ programming language.

#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 ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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