텍스트 파일에 학생 목록 (이름, 나이 및 GPA)을 유지하는 C++를 사용하는 프로그램을 만들기위한 과제 프로젝트가 있습니다. 이 프로그램은 다음과 같은 기능이 있습니다 : 기록 기록 텍스트 파일에서 레코드 삭제하기
- 삽입을
내 코드는 문자열 이름을 사용 기록을 삭제 텍스트 파일에서 제거합니다. 그러나 파일의 다음 두 줄 (해당 학생의 경우 & gpa)이 남아 있습니다. 어떻게 제거합니까?
다음은 내 프로그램 코드입니다.
는#include <iostream>
#include <fstream>
using namespace std;
void writing();
void deleting();
void searching();
class student {
public:
int age = 0;
string name;
void SetGpa(float x)
{
gpa = x;
}
float GetGpa()
{
return gpa;
}
private:
float gpa = 0.0;
};
int main()
{
int opt;
cout << "Please Enter an option number to continue:\n ";
cout << "\nPress 1 for New Record insertion";
cout << "\nPress 2 for Record Deletion";
cout << "\nPress 3 for Searching a Record";
cout << "\nPress 4 for Updating a Record";
cout << "\nEnter option Number: ";
cin >> opt;
switch (opt)
{
case 1:
{
writing();
break;
}
case 2:
{
deleting();
break;
}
case 3:
{
searching();
break;
}
case 4:
{
deleting();
writing();
cout << "Record has been updated! ";
break;
}
}
}
void writing()
{
float a;
student moiz;
cout << "Please enter name of student: ";
cin >> moiz.name;
cout << "Please enter the age of student: ";
cin >> moiz.age;
cout << "Pleae enter the Gpa of student: ";
cin >> a;
moiz.SetGpa(a);
ofstream myfile;
myfile.open("record.txt", ios::app | ios::out);
myfile << endl << moiz.name << endl;
myfile << moiz.age << endl;
myfile << moiz.GetGpa();
myfile.close();
}
void deleting()
{
string line, name;
cout << "Please Enter the name of record you want to delete: ";
cin >> name;
ifstream myfile;
ofstream temp;
myfile.open("record.txt");
temp.open("temp.txt");
while (getline(myfile, line))
{
if (line != name)
temp << line << endl;
}
cout << "The record with the name " << name << " has been deleted if it exsisted" << endl;
myfile.close();
temp.close();
remove("record.txt");
rename("temp.txt", "record.txt");
}
void searching()
{
ifstream fileInput;
fileInput.open("record.txt");
string line, search;
cout << "Please enter the term to search: ";
cin >> search;
for (unsigned int curLine = 0; getline(fileInput, line); curLine++)
{
if (line.find(search) != string::npos)
{
cout << "found: " << search << " on line: " << curLine << endl;
}
else
{
cout << "Error! Not found on Line" << curLine << endl;
}
}
}
당신은 요구하지 않았다 어떤 질문도 어떤 문제도 제시하지 않았다. 좀 더 구체적이어야합니다. 그리고 숙제 문제에 downvotes의 비를 얻지 않기 위하여 당신은 진짜로 명확해야한다. 사람들은 여기에있는 사람들을 도우려고하지만, 묻는 부분에 대한 상당한 노력이 보여 져야합니다. – luk32
나는 질문의 시작 부분에 묻는 부분을 보여 주었다. 현재이 프로그램은 텍스트 파일에서 이름 만 삭제합니다. 텍스트 파일에서 이름 + 나이 + GPA를 삭제해야합니다. –
좋아, 시도 했어. 어쩌면 내 영어가 너무 나쁘다. "* C++로 텍스트 파일에서 레코드를 지우는 중"* "다음 작업을 수행 할 C++을 사용하여 프로그램을 만드는 프로젝트가있다. . * ","* 프로젝트 완료만으로 문제가 해결되었습니다. [...] * ", 나에게 아무런 문제가 없습니다. 당신은 당신의 문제가 아니라 당신이 원하는 것을 말합니다. 당신이 새로운 것 같아서 방금 몇 가지 커뮤니티 표준을 소개하려고했습니다. 무언가를 시도한 다음 작동하지 않는 것, 예를 들어 말하고 그 이유를 묻습니다. 나는 아무 것도 좋아질 것이고, 나는 새로운 사용자가'-5'를 얻는 것을 싫어할 뿐이다. – luk32