删除记录(ClassModule :: deleteModule())后输入数据(ClassModule :: insert_info())时遇到问题

时间:2019-02-06 08:53:30

标签: c++

~~你好社区~~

  

我正在研究一个简单的学生信息系统的项目,   将给定类的记录添加到名为.csv文件的内部   ClassMaster.csv。它还提供了删除任何   选择的记录。

     

我为学生信息项目创建了2个头文件   系统,即:   -AddStdModule.hpp   -ClassModule.hpp我在main.cpp文件中使用这些标头。

     

我面临的问题是,“当我尝试插入新记录时,使用ClassModule :: deleteModule(string)函数删除了某个记录后,   使用ClassModule :: insert_info()函数进行记录,该记录正在   在“跳过”一行之后插入。

     

我个人认为这与我的“ \ n”字符有关   我每次插入新记录后都会使用,但我无法   找出原来是不是这个问题。

     

已经在代码中停留了几天了。您的帮助和意见   将不胜感激!

#include<iostream>
#include<fstream>
#include<string>
#include<string.h>
#include<sstream>
#include<iomanip>
#include<regex>
#include<stdio.h>
using namespace std;

class ClassModule
{
fstream file1;
ifstream file1i;
ofstream file1o;
string Headers,test;
int intinp;
string strinp;
bool strap =true;
bool HeaderFlag = false;


public: 
ClassModule()   //Constructor 
{

//Initialize the value of headers to be checked
Headers                                                                                           ="Unique_ID_Class,Dept,Sem_No,Section,Sub1,Sub2,Sub3,Sub4";
//Check whether the file is open or Not
file1.open("C:/SIS/Class_Master.csv");
if(!file1.is_open())

//If the file is not available in the desired location, create a                                new file
{
cout<<"File has not been loaded in the memory";
file1.close();
file1.open("C:/SIS/Class_Master.csv",ios::app);
}
file1.close();
}

void insert_info()
{
bool loopflag = false;
string testx;
string sizeinfo;
stringstream ss;
string strinparr[8];
int current_Sr_No=0;
int choice;


file1.open("C:/SIS/Class_Master.csv",ios::app);
if(file1.is_open())
{
cout<<"File is open";



cout<<"Please choose a DEPARTMENT:";
cout<<"\n1.MCA\n2.BCA";
cin>>choice;
switch(choice)
{
case 1:
strinparr[0] = "MCA";                           
break;
case 2:
strinparr[0] = "BCA";
break;
default:
cout<<"Invalid Input!\n";
}


cout<<"\nPlease enter the current Semester Number(1,2,3,4,5,6):";
cin>>strinparr[1];                                    

cout<<"Please choose a SECTION:";
cout<<"\n1.A\n2.B \n3.C \n4. D";
cin>>choice;
switch(choice)
{
case 1: 
//strinparr[2] -> Section Number
strinparr[2] = "A";
break;
case 2:
strinparr[2] = "B";
break;
case 3:
strinparr[2] = "C";
break;
case 4 :
strinparr[2] = "D";
break;
default:
cout<<"Invalid Input!\n";
}


//Generating the Unique ID and checking for 
//it in the records using ID_Checker

strinparr[3] = strinparr[0] + strinparr[1] + strinparr[2]; 
//GENERATING A UNIQUE ID FOR THE CLASS
cout<<"\n\nUnique ID: "<<strinparr[3];
file1.close();
if(UID_Checker(strinparr[3])==false)
{
cout<<"\nThe Chosen Class already Exists!";
}
else
{

file1.open("C:/SIS/Class_Master.csv",ios::app);

cout<<"\nNow please enter the name of the subjects ";
cout<<"\nWrite the first three initials of the subject: ";

for(int i=4;i<=7;i++)
{
cout<<"\nEnter name of Subject "<<i-3<<": ";
cin>>strinparr[i];
}


if(file1.is_open()==true){
cout<<"File1 is open";
}
else
{
cout<<"File1 is not open";
}
file1 <<strinparr[3]<<","<<strinparr[0]<<","<<strinparr[1]<<","<<strinparr[2]<<","<<strinparr[4]<<","<<strinparr[5]<<","<<strinparr[6]<<","<<strinparr[7];
file1<<"\n";

cout<<"\nClass Successfully Created!";
file1.close();
}
}

}

void deleteModule(string ID)
{
ifstream file1i;
fstream file1o;
string sub;

file1i.open("C:/SIS/Class_Master.csv",ios::in);

file1o.open("C:/SIS/temp_Class_Master.csv",ios::out);

while(file1i.eof()==false)
{

getline(file1i,test,'\n');  //fetches every line
sub = test.substr(0,5); 
//Fetches the first five characters from the whole line (the ClassID)
if(sub==ID)
{
cout<<"ID to be deleted is: "<<sub;
continue;
}
else
{
file1o<<test;
file1o<<"\n";
}
}
file1o.close();
file1i.close();
remove("C:/SIS/Class_Master.csv");
string add = "C:/SIS/Classes/" + ID +".csv";
//Removing the associated class from the classes directory
remove(add.c_str());        

rename("C:/SIS/temp_Class_Master.csv", "C:/SIS/Class_Master.csv");
}


void Header_Check()    //Perform a check on the headers
{
file1.open("C:/SIS/Class_Master.csv",ios::in);

if(file1.is_open())
{
cout<<"Class Master is Open\n\n";
getline(file1,test,'\n');
file1.close();

if(strcmp(test.c_str(),Headers.c_str())==0)
{
cout<<"\nHeaders Match! You are ready to roll!\n";
}

else
{
file1.open("C:/SIS/Class_Master.csv",ios::out);
cout<<"\nHeaders don't match.\nCreating a well structured file";
file1 << Headers;
file1<<"\n";
}

file1.close();

}
}


private:
bool UID_Checker(string strinput123)
{
int i=0;
string UID_Store[100];
string testx,test;


file1.open("C:/SIS/Class_Master.csv",ios::in);                 
getline(file1,test,'\n');



//Stores all GRs inside the GR_Store[]

while(!file1.eof())
{

getline(file1,test,',');
UID_Store[i] = test;

getline(file1,testx,'\n');
i++;
}
//After the entire column of GR_No is fetched inside the
// Gr_Store[] array, we need to compare every element of the array
//with the value
//Given by the user

for(i=0;i<100;i++)
{
if(strcmp(strinput123.c_str(),UID_Store[i].c_str())==0)
{
cout<<"The given Class "<<UID_Store[i]<<" already exists";
return false;
}
}
file1.close();
return true;
}

};

以下是mainFile的代码

#include "C:\Users\Priyank Rupareliya\Desktop\Project\SIS - Using CSV\ClassModule.hpp"
#include<stdlib.h>
int main()
{
int choice;
ClassModule obx;
string Class_ID;
unsigned int choice1;


cout<<"\nWelcome to the Student Information System";
while(1)
{
cout<<"Please select any one of the choices from below to continue: ";
cout<<"\n\n1. ADD A NEW CLASS \n2. Delete a Class (Not Properly Working) \n3. Exit";
cin>>choice;


switch(choice)
{
case 1:
obx.Header_Check();
obx.insert_info();
break;


case 2:
{
Class_ID = obx.get_data(1);
if(Class_ID!="NULL")
{
obx.deleteModule(Class_ID);
}
else
{

}
break;

}


case 3:
exit(0);
break;

default:
cout<<"\nInvalid CHOICE";
break;

}
}


}
  

问题再次出现在deleteModule()和   insert_info()。

     

这是添加2个类,删除一个类,然后再次添加一个新类后得到的输出。

     

实际上期望记录“ BCA2B”应该出现   在“ MCA1A”之后。

enter image description here

0 个答案:

没有答案