5

私の C++ プログラムに問題があります... 私のプログラム全体は、学生の名前、成績、年齢のデータベースであり、ユーザーが 1 人の学生のデータを削除したい場合、機能に問題があります。コードは次のとおりです。

void deletestudentdata()
{
    string name, grade, tname;
    int age, x=0; // x - "counter" to check if user entered wrong name

    system("cls");
    cout << "Enter name of the student you want to erase from database" << endl;
    cin >> tname;

    ifstream students("students.txt");
    ofstream temp("temp.txt"); // temp file for input of every student except the one user wants to delete

    while(students >> name >> grade >> age)
    {
        if(tname!=name){ // if there are students with different name, input their data into temp file
            temp << name << ' ' << grade << ' ' << age << endl;
        }
        if(tname==name){ // if user entered correct name, x=1 for later output message that the user data has been deleted
            x=1;
        }
    }
    students.clear(); // clear eof and fail bits
    students.seekg(0, ios::beg);
    students.close();
    temp.close();
    remove("students.txt"); 
    rename("temp.txt","students.txt");
    if(x==0){ // x was set to 0 at start, so if it didn't change, it means user entered the wrong name
        cout << "There is no student with name you entered." << endl;
    }
    else{ // x is not 0, it means user entered the correct name, print message that students data has been deleted
        cout << "Student data has been deleted." << endl;
    }
}

それは機能しますが、問題は、学生データを入力し、この関数を介して削除したいときに削除されないことです。最初にプログラムを閉じてからプログラムを再度開いてから、その関数を呼び出して学生を削除する必要がありますデータ。

最初にプログラムを閉じなくても、入力後すぐに生徒のデータを削除できるように変更するにはどうすればよいですか?

4

1 に答える 1

3

生徒の名前が見つからない場合は機能しません。見つからなかったと言う代わりに、生徒の名前が削除されたと言うだけです

#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;




void displaystudentdata()
{
   string name, grade, tname;
    int age, x=0; // x - "counter" to check if user entered wrong name

    system("cls");

    ifstream students("students.txt");


    cout<<"-------------------------------------------------------------------\n\n";
    while(students >> name >> grade >> age)
    {

        cout<<"Name= "<<name <<", Grade= "<< grade <<" , Age= " <<age<<"\n";
    }
    students.clear(); // clear eof and fail bits
    students.seekg(0, ios::beg);
     students.close();
}

void deletestudentdata()
{
    string name, grade, tname;
    int age, x=0; // x - "counter" to check if user entered wrong name



    ifstream students("students.txt");
    ofstream temp("temp.txt"); // temp file for input of every student except the one user wants to delete


    cout<<"-------------------------------------------------------------------\n\n";

    cout << "Enter name of the student you want to erase from database >" << endl;
    cin >> tname;

    //ifstream students("students.txt");
    //ofstream temp("temp.txt"); // temp file for input of every student except the one user wants to delete

    while(students >> name >> grade >> age)
    {
        if(tname!=name){ // if there are students with different name, input their data into temp file
            temp << name << ' ' << grade << ' ' << age << endl;
        }
        if(tname==name){ // if user entered correct name, x=1 for later output message that the user data has been deleted
            x=1;
        }
    }
    students.clear(); // clear eof and fail bits
    students.seekg(0, ios::beg);
    students.close();
    temp.close();
    remove("students.txt"); 
    rename("temp.txt","students.txt");
    if(x==0){ // x was set to 0 at start, so if it didn't change, it means user entered the wrong name
        cout << "There is no student with name you entered." << endl;
    }
    else{ // x is not 0, it means user entered the correct name, print message that students data has been deleted
        cout << "Student data has been deleted." << endl;
    }
}


int main(void)
{



  displaystudentdata();
  deletestudentdata();
  displaystudentdata();
  cout << "Student data has been deleted. \n\n" << endl;
  cout<<" \nPress any key to continue\n";
  cin.ignore();
  cin.get();

   return 0;
}
于 2013-11-13T08:11:04.137 に答える