I Can't Remove a Vector of Input from a Text File

I just need help on how to use lines of code to remove text from a tex

Page 1 of 1

1 Replies - 870 Views - Last Post: 03 April 2008 - 04:34 AM Rate Topic: -----

#1 derbear  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-April 08

I Can't Remove a Vector of Input from a Text File

Post icon  Posted 02 April 2008 - 09:32 PM

My program is for a gradebook that I am making with menus and outfiling to a text file. I can make a vector and put it in the text file, but I don't know how to delete a particular vector in the text file or even the entire text file with just code. My entire program is below. Thank you in advance!!


//GRADBOOK.H

#ifndef GRADBOOK_H_
#define GRADBOOK_H_
#include <string.h>
#include <vector.h>

struct date
{
	int day;
	int month;
	int year;
};
	
struct eachgrade
{
	string assignment;
	string type;
	date dueDate;
	int pointValue;
	int pointsEarned;
};
	
struct oneStudent
{
	string ID;
	string lastName;
	string firstName;
	string middleInitial;
	string gender;
	string className;
	string classHour;
	
	int numGrades;
	vector<eachgrade>theirGrades;
	float gradeAverage;
	string lettergrade;
};

class gradbook
{
	public:
	
		gradbook();
		void loadBook();
		void addStudent();
		void saveBook();
		void addAssignment();
		void addGrade();

	private:
	
		vector<oneStudent>thisClass;
		int numberOfStudents;
		
		char databaseName[60];
};

//#include "gradbook.cpp"
#endif	// GRADBOOK_H_





//GRADBOOK.CPP

using namespace std;
#include <iostream>
#include <vector>
#include <fstream>
#include <string.h>
#include "gradbook.h"


gradbook::gradbook()
{
	numberOfStudents=0;
	thisClass.resize(numberOfStudents);
}

void gradbook::loadBook()
{
	cout<<"\nGradebook Name : ";
	cin>>databaseName;
	ifstream infile;
	bool success=false;
	
	if (!success)
	{
		infile.open (databaseName, ios::in);
		if (infile.fail())
		{
			cout<<"\n\tFILE... '"<<databaseName<<"' ...could not be found."<<endl;
		}
		else
		success=true;
	}
	
	infile>>numberOfStudents;
	thisClass.resize(numberOfStudents);
	for (int j=0; j<numberOfStudents; j++)
	{
		infile>>thisClass[j].ID;
		infile>>thisClass[j].lastName;
		infile>>thisClass[j].firstName;
		infile>>thisClass[j].middleInitial;
		infile>>thisClass[j].gender;
		infile.ignore();
		getline(infile, thisClass[j].className);
		getline(infile, thisClass[j].classHour);
	}
	
	cout<<"\nThere is/are "<<numberOfStudents<<" student(s) in this class."<<endl;
	
	for (int k=0; k<numberOfStudents; k++)
	{
		cout<<"\n"<<thisClass[k].ID;
		cout<<" "<<thisClass[k].lastName;
		cout<<" "<<thisClass[k].firstName;
		cout<<" "<<thisClass[k].middleInitial;
		cout<<" "<<thisClass[k].gender;
		cout<<" "<<thisClass[k].className;
		cout<<" "<<thisClass[k].classHour;
	}
}


void gradbook::addStudent()
{
	numberOfStudents+=1;
	thisClass.resize(numberOfStudents);
	
	cout<<"\n\n\t\tNEW STUDENT"<<endl;
	
	cout<<"\nEnter student ID : ";
	cin>>thisClass[numberOfStudents-1].ID;
	
	cout<<"Enter student's last name : ";
	cin>>thisClass[numberOfStudents-1].lastName;
	
	cout<<"Enter student's first name : ";
	cin>>thisClass[numberOfStudents-1].firstName;
	
	cout<<"Enter student's middle initial : ";
	cin>>thisClass[numberOfStudents-1].middleInitial;
	
	cout<<"Enter student's gender : ";
	cin>>thisClass[numberOfStudents-1].gender;
	
	cin.ignore();
	
	cout<<"Enter class name : ";
	getline(cin,thisClass[numberOfStudents-1].className);
	
	cout<<"Enter class hour : ";
	getline(cin, thisClass[numberOfStudents-1].classHour);
}
	
void gradbook::saveBook()
{
	cout<<"\n--Gradebook Save Name (include .txt)--"<<endl;
	cin>>databaseName;
	ofstream fout;
	fout.open(databaseName, ios::out);
	fout<<numberOfStudents<<endl;
	
	for (int i=0; i<numberOfStudents; i++)
	{
		fout<<thisClass[i].ID<<endl;
		fout<<thisClass[i].lastName<<endl;
		fout<<thisClass[i].firstName<<endl;
		fout<<thisClass[i].middleInitial<<endl;
		fout<<thisClass[i].gender<<endl;
		fout<<thisClass[i].className<<endl;
		fout<<thisClass[i].classHour<<endl;
	}
	
	fout.close();
	
	cout<<"\n\tGradebook "<<databaseName<<" updated!"<<endl;
}
	
void gradbook::addAssignment()
{
	cout<<"\nGradebook Name : ";
	cin>>databaseName;
	ifstream infile;
	bool success=false;
	
	if (!success)
	{
		infile.open (databaseName, ios::in);
		if (infile.fail())
		{
			cout<<"\n\tFILE... '"<<databaseName<<"' ...could not be found."<<endl;
		}
		else
		success=true;
	}
	
	infile>>numberOfStudents;
	thisClass.resize(numberOfStudents);
	for (int j=0; j<numberOfStudents; j++)
	{
		infile>>thisClass[j].ID;
		infile>>thisClass[j].lastName;
		infile>>thisClass[j].firstName;
		infile>>thisClass[j].middleInitial;
		infile>>thisClass[j].gender;
		infile.ignore();
		getline(infile, thisClass[j].className);
		getline(infile, thisClass[j].classHour);
	}
	
	cout<<"\nThere is/are "<<numberOfStudents<<" student(s) in this class."<<endl;
	
	cout<<"\nWhich Student would you like to add an assignment to?"<<endl;
	
	for (int k=0; k<numberOfStudents; k++)
	{
		cout<<"\n"<<k+1<<". "<<thisClass[k].ID;
		cout<<" "<<thisClass[k].lastName;
		cout<<" "<<thisClass[k].firstName;
		cout<<" "<<thisClass[k].middleInitial;
		cout<<" "<<thisClass[k].gender;
		cout<<" "<<thisClass[k].className;
		cout<<" "<<thisClass[k].classHour;
	}
	
	cout<<"\n\nChoice : ";
	int student_choice;
	cin>>student_choice;
	
	cout<<"\n"<<thisClass[student_choice-1].ID;
	cout<<" "<<thisClass[student_choice-1].lastName;
	cout<<" "<<thisClass[student_choice-1].firstName;
	cout<<" "<<thisClass[student_choice-1].middleInitial;
	cout<<" "<<thisClass[student_choice-1].gender;
	cout<<" "<<thisClass[student_choice-1].className;
	cout<<" "<<thisClass[student_choice-1].classHour;
	
	thisClass[student_choice-1].numGrades=0;
	thisClass[student_choice-1].numGrades+=1;
	thisClass[student_choice-1].theirGrades.resize(thisClass[student_choice-1].numGrades);

	
	string name;
	cout<<"\n\nAssignment Name : ";
	cin>>name;
	thisClass[student_choice-1].theirGrades[thisClass[student_choice-1].numGrades-1].assignment=name;
	
	cout<<"\nAssignment Type : ";
	cin>>thisClass[student_choice-1].theirGrades[thisClass[student_choice-1].numGrades-1].type;
	
	for (int u=0;u<thisClass[student_choice-1].numGrades;u++)
	{
		cout<<"\n"<<thisClass[student_choice-1].theirGrades[u].assignment;
		cout<<" "<<thisClass[student_choice-1].theirGrades[u].type;
	}
}
	
void gradbook::addGrade()
{
	
}





//MAINPROG.CPP

#include <iostream>
using namespace std;
#include "gradbook.h"

void menu1();
void menu2();

int main (void)
{
	menu1();
}

void menu1()
{
	int choice;
	gradbook roster;
	
	cout<<"\n\n\t\t\t==> MAIN MENU <==\n\n";
	cout<<"1. Load a Gradebook\n";
	cout<<"2. Make a New Gradebook\n";
	cout<<"3. Add a Student to an Existing Gradebook\n";
	cout<<"4. View and Edit Student Assignments\n";
	cout<<"5. Exit\n";
	cout<<"\nChoice : ";
	cin>>choice;
	
	switch (choice)
	{
		case 1:
			roster.loadBook();
			menu1();
		
		case 2:
			roster.saveBook();
			menu1();
			
		case 3:
			roster.loadBook();
			roster.addStudent();
			roster.saveBook();
			menu1();
			
		case 4:
			menu2();
			
		
		case 5:
			exit(0);
		
		default:
			cout<<"\nNo Option Available"<<endl;
			menu1();
			
	}
}

void menu2()
{
	int choice2;
	gradbook assignment;
	
	cout<<"\n\n\t==> Student Assignments Menu <==\n\n";
	cout<<"1. Add Assignment for a Student\n";
	cout<<"2. View Assignment(s) for a Student\n";
	cout<<"3. Go to Main Menu\n";
	cout<<"4. Exit\n";
	cout<<"\nChoice : ";
	
	cin>>choice2;
	
	switch (choice2)
	{
		case 1:
			assignment.addAssignment();
			menu2();
			
		case 2:
			assignment.loadBook();
	
		case 3:
			menu1();
			
		case 4:
			exit(0);
	}
}




I'm just not sure how to delete text from the file or deleting the entire text file using lines of code. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: I Can't Remove a Vector of Input from a Text File

#2 Pontus  Icon User is offline

  • Cattlebruiser

Reputation: 17
  • View blog
  • Posts: 612
  • Joined: 28-December 06

Re: I Can't Remove a Vector of Input from a Text File

Posted 03 April 2008 - 04:34 AM

Copy all of the text file contents, search for the strings u want te remove, then open the file in trunc mode (Click here).
So all of the contenst gets removed then paste the contents u copied.

Hope this will help...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1