我试图显示每门课程的学生人数,但是当我进入第一门课程时,它首先为我提供了适当的数量,但是当我进入第二门课程时,它添加了所有课程。 主要问题在于案例6:id非常感谢您的帮助。我有点像编程的初学者。 这是下面的代码部分:
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string>
#include<iomanip>
#include<fstream>
#include<cstring>
#include<vector>
#include<assert.h>
using namespace std;
struct student
{
char name[80];
char sname[80];
int idno;
char cours[100];
int age;
int modules;
};
int main()
{
ofstream cfile;
ofstream sfile;
char dow;
int arr=0;
do{
student stud[100];
cout<<"Press 1 Enter record \n";
cout<<"Press 2 Delete record \n";
cout<<"Press 3 Update record \n";
cout<<"Press 4 Search record \n";
cout<<"Press 5 To check number of students doing each course \n";
cout<<"Press 6 To check student course file \n";
cout<<"Press 7 Display record \n";
cout<<"\n\t Select option::";
int idcheck=0;
int sw;
cin>>sw;
switch(sw)
{
case 1:
cout<<"\n Enter the data of the student no "<<arr+1<<" is :\n";
cout<<"\t Enter the Roll No = ";
int id2;
int id;
cin>>id;
for(int j=0; j<=arr; j++)
{
id2=id;
if(id2==stud[j].idno)
{
idcheck=1;
}
}
if(idcheck!=1){
stud[arr].idno=id;
cout<<"\t Enter the Name = ";
cin>>stud[arr].name;
cout<<"\t Enter the Surname = ";
cin>>stud[arr].sname;
cout<<"\t Enter the age = ";
cin>>stud[arr].age;
cout<<"\t Enter the Course = ";
cin>>stud[arr].cours;
cout<<"\t Enter the Modules = ";
cin>>stud[arr].modules;
arr=arr+1;
}
else
{
cout<<"This Record is Already Entered \n";
}
break;
case 2:
cout<<"\n Enter the Roll no of the student To Delete :: \n";
cin>>id;
for(int j=0; j<=arr; j++)
{
id2=id;
if(id2==stud[j].idno)
{
stud[j].idno='d';
cout<<"\t Record Deleted";
}
}
break;
case 3:
cout<<"\n Enter the Roll no of the student To Update ::\n";
cin>>id;
for(int j=0; j<=arr; j++)
{
id2=id;
if(id2==stud[j].idno)
{
cout<<"\t ID = ";
cout<<stud[j].idno;
cout<<"\t Name = ";
cout<<stud[j].name;
cout<<"\t Surname = ";
cout<<stud[j].sname;
cout<<"\t Age = ";
cout<<stud[j].age;
cout<<"\t Course = ";
cout<<stud[j].cours;
cout<<"\t Modules = ";
cout<<stud[j].modules;
cout<<"\n\t Re-Enter Data ";
cout<<"\n\t Enter the Name = ";
cin>>stud[j].name;
cout<<"\n\t Enter the Surname = ";
cin>>stud[j].sname;
cout<<"\n\t Enter the Age = ";
cin>>stud[j].age;
cout<<"\n\t Enter the Course = ";
cin>>stud[j].cours;
cout<<"\n\t Enter the Modules = ";
cin>>stud[j].modules;
}
}
break;
case 4:
cout<<"\n Enter the ID number of the student To Search ::";
cin>>id;
for(int j=0; j<=arr; j++)
{
id2=id;
if(id2==stud[j].idno)
{
cout<<"\n\t ID = ";
cout<<stud[j].idno;
cout<<"\n\t Name = ";
cout<<stud[j].name;
cout<<"\n\t Surname = ";
cout<<stud[j].sname;
cout<<"\n\t Age = ";
cout<<stud[j].age;
cout<<"\n\t Course = ";
cout<<stud[j].cours;
cout<<"\n\t Modules = ";
cout<<stud[j].modules;
}
}
break;
case 5:
{
string str;
int i, count = 0;
cout<<"Enter Course: ";
cin>>str;
for( i=0; i<=str.size(); i++)
{
if(str == stud[i].cours)
{
count++;
}
}
cout<<"Number of students doing "<<str<<" are "<<count;
}
break;
case 6:
{
string str;
int n, count = 0;
cout<<"Enter number of courses: ";
cin>>n;
for(int i=0; i<=n-1; i++)
{
cout<<"Enter course: ";
cin>>str;
for(int i=0; i<=str.size(); i++)
{
if(str == stud[i].cours)
{
count++;
}
}
cout<<"Number of students doing "<<str<<" are "<<count<<endl;
}
}
cout<<"\n__________________________________________________________________________________________________________________________________________________________________"<<endl;
for(int i=0; i<1; i++)
{
cout<<setw(12)<<"Rollno ||";
cout<<setw(12)<<" Name ||";
cout<<setw(12)<<" Surname || ";
cout<<setw(12)<<"Age ||";
cout<<setw(12)<<" Course ||";
cout<<setw(12)<<" Module ||";
for(int k=0; k<arr; k++)
{
if(stud[k].idno!='d')
{
cout<<"\n";
cout<<" ";
cout<<setw(5)<<stud[k].idno;
cout<<setw(15)<<stud[k].name;
cout<<setw(14)<<stud[k].sname;
cout<<setw(14)<<stud[k].age;
cout<<setw(12)<<stud[k].cours;
cout<<setw(14)<<stud[k].modules;
}
}
}
cout<<"\n______________________________________________________________________________________________________________________________________________________________________________"<<endl;
{
cfile.open("CourseFile.txt");
string str;
int i,count = 0;
for(i=0; i<2; i++)
{
for( i=0; i<=sizeof(str); i++)
{
if(str == stud[i].cours)
{
count++;
}
}
cfile<<"Number of students doing "<<str<<" are "<<count;
}
}
cfile<<"\n__________________________________________________________________________________________________________________________________________________________________"<<endl;
for(int i=0; i<1; i++)
{
cfile<<setw(12)<<"Rollno ||";
cfile<<setw(12)<<" Name ||";
cfile<<setw(12)<<" Surname || ";
cfile<<setw(12)<<"Age ||";
cfile<<setw(12)<<" Course ||";
cfile<<setw(12)<<" Module ||";
for(int k=0; k<arr; k++)
{
if(stud[k].idno!='d')
{
cfile<<"\n";
cfile<<" ";
cfile<<setw(5)<<stud[k].idno;
cfile<<setw(15)<<stud[k].name;
cfile<<setw(14)<<stud[k].sname;
cfile<<setw(14)<<stud[k].age;
cfile<<setw(12)<<stud[k].cours;
cfile<<setw(14)<<stud[k].modules;
}
}
}
cfile<<"\n______________________________________________________________________________________________________________________________________________________________________________"<<endl;
cfile.close();
break;
case 7:
{
sfile.open("StudentFile.txt");
cout<<"\n__________________________________________________________________________________________________________________________________________________________________";
for(int i=0; i<1; i++)
{
cout<<setw(12)<<"Rollno ||";
cout<<setw(12)<<" Name ||";
cout<<setw(12)<<" Surname || ";
cout<<setw(12)<<"Age ||";
cout<<setw(12)<<" Course ||";
cout<<setw(12)<<" Module ||";
for(int k=0; k<arr; k++)
{
if(stud[k].idno!='d')
{
cout<<"\n";
cout<<" ";
cout<<setw(5)<<stud[k].idno;
cout<<setw(15)<<stud[k].name;
cout<<setw(14)<<stud[k].sname;
cout<<setw(14)<<stud[k].age;
cout<<setw(12)<<stud[k].cours;
cout<<setw(14)<<stud[k].modules;
}
}
}
cout<<"\n______________________________________________________________________________________________________________________________________________________________________________";
for(int i=0; i<1; i++)
{
sfile<<setw(12)<<"Rollno ||";
sfile<<setw(12)<<" Name ||";
sfile<<setw(12)<<" Surname || ";
sfile<<setw(12)<<"Age ||";
sfile<<setw(12)<<" Course ||";
sfile<<setw(12)<<" Module ||";
for(int k=0; k<arr; k++)
{
if(stud[k].idno!='d')
{
sfile<<"\n";
sfile<<" ";
sfile<<setw(5)<<stud[k].idno;
sfile<<setw(15)<<stud[k].name;
sfile<<setw(14)<<stud[k].sname;
sfile<<setw(14)<<stud[k].age;
sfile<<setw(12)<<stud[k].cours;
sfile<<setw(14)<<stud[k].modules;
sfile.close();
}
}
}
}
break;
default:
cout<<"\t Wrong option Selected ";
break;
}
cout<<"\n\n\t Do You Want to Continue Again [Y/N]";
cin>>dow;
}
while(dow=='y');
return 0;
getch();
}