Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#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.....
Posted
Updated 18-Dec-18 21:33pm
Comments
Mohibur Rashid 19-Dec-18 0:45am    
What error?

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!
 
Share this answer
 
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.

C++
#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;
}
 
Share this answer
 
Comments
KarstenK 19-Dec-18 3:41am    
Nice design, but it needs a lot more formatted output in the input and output of the student.

Also the number of students should be an input and so scalable.
CPallini 19-Dec-18 4:52am    
Thank you. It was intended to be as raw as possible. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900