Can someone help me change the code to read in a input file instead of user inpu
ID: 3872029 • Letter: C
Question
Can someone help me change the code to read in a input file instead of user inputting all the data.
The input file will be of the following format:
student_name_1
student_1_gpa_1
student_1_course_name_1
student_1_course_1_unit_worth
studnet_1_course_1_unit_grade
etc
blank line
student_name_2
student_2_gpa_2
student_2_course_name_1
student_2_course_1_unit_worth
studnet_2_course_1_unit_grade
student_2_course_name_2
student_2_course_2_unit_worth
studnet_2_course_2_unit_grade
etc
blank line
student_name_3
student_gpa_3
student_course_name_1
etc
student_3_course_name_5
etc
blank line
etc
This is the format of the input file. Each student can have 1 or more courses to their records. The units are rated 1 -4, and can be graded by 0-4. For example if calculus is a 4 unit class and the student makes an 'A' in the class then their unit grade is a 4. And a "C" would be a 2. GPA is also on a 4 point scale. Here is the code I have written in visual studios c++.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
class course
{
private:
char studentGrades;
int courseUnits;
string studentName;
public:
course()
{
}
course(string n, int u, char g)
{
this->studentName = n;
this->courseUnits = u;
this->studentGrades = g;
}
void print()
{
cout << endl;
cout << "Student's course: " << studentName << endl;
cout << "Number of course units : " << courseUnits << endl;
cout << "Student's Grade: " << studentGrades << endl;
cout << endl;
}
string gName()
{
return studentName;
}
};
class node
{
public:
course studentData;
node *next;
node(course data)
{
this->studentData = data;
next = NULL;
}
};
class student
{
private:
double gpa;
node *head;
string student_Name;
public:
student(string n, double gpa)
{
this->student_Name = n;
this->gpa = gpa;
head = NULL;
}
string gName()
{
return student_Name;
}
double getGPA()
{
return gpa;
}
void courseAdd(course c)
{
node *newNode = new node(c);
newNode->next = head;
head = newNode;
}
void courseRemove(string n)
{
if (head != NULL)
{
if (head->studentData.gName() == n)
{
head = head->next;
cout << "*************************************************" << endl;
cout << "The course was removed from the record...." << endl;
cout << "*************************************************" << endl;
return;
}
else
{
node *temp = head;
if (temp->next != NULL)
{
if (temp->next->studentData.gName() == n)
{
temp->next = temp->next->next;
cout << "*************************************************" << endl;
cout << "The course was removed from the record!!" << endl;
cout << "*************************************************" << endl;
return;
}
temp = temp->next;
}
}
}
cout << "*************************************************" << endl;
cout << "This course does not exist in this database..." << endl;
cout << "*************************************************" << endl;
}
void print()
{
cout << "Student's Name: " << student_Name << endl;
cout << "Student's GPA: " << gpa << endl;
cout << "The courses: " << endl;
node *temp = head;
while (temp != NULL)
{
temp->studentData.print();
temp = temp->next;
}
}
};
int studentF(vector s, string n)
{
for (int i = 0; i < s.size(); ++i)
{
if (s[i].gName() == n)
{
return i;
}
}
return -1;
}
void studentAdd(vector &s)
{
double sGPA;
string sName;
cout << "Enter the Student's Name with no spaces: ";
cin >> sName;
cout << "Enter the Student's GPA 'Example: 3.7': ";
cin >> sGPA;
s.push_back(student(sName, sGPA));
}
void studentRemove(vector &s)
{
string sName;
cout << "Enter the Student's name that is to be removed with no spaces:";
cin >> sName;
int index = studentF(s, sName);
if (index == -1)
{
cout << "*************************************************" << endl;
cout << "This student does not exist in the system!!" << endl;
cout << "*************************************************" << endl;
}
else
{
for (int i = index; i < s.size() - 1; ++i)
{
s[i] = s[i + 1];
}
s.pop_back();
}
}
void studentPrint(vector students)
{
string n;
cout << "Enter the Student's Name with no spaces, to pull up the students records: ";
cin >> n;
int index = studentF(students, n);
if (index == -1)
{
cout << "*************************************************" << endl;
cout << "This student does not exist!!!" << endl;
cout << "*************************************************" << endl;
}
else
{
student s = students[index];
s.print();
}
}
void courseAdd(vector &students)
{
string n;
cout << "Enter the Student's Name with no spaces, for which you wish to add a course for their record: ";
cin >> n;
int index = studentF(students, n);
if (index == -1)
{
cout << "*************************************************" << endl;
cout << "This student does not exist!!" << endl;
cout << "*************************************************" << endl;
}
else
{
int u;
char g;
cout << "Enter course name: ";
cin >> n;
cout << "Enter the course units '1 through 4' : ";
cin >> u;
cout << "Enter the Student's unit grade '1 through 4': ";
cin >> g;
students[index].courseAdd(course(n, u, g));
}
}
void courseRemove(vector &students)
{
string n;
cout << "Enter the Student's Name with no spaces, for which you wish to remove a course from their record: ";
cin >> n;
int index = studentF(students, n);
if (index == -1)
{
cout << "*************************************************" << endl;
cout << "The student does not exist!!!" << endl;
cout << "*************************************************" << endl;
}
else
{
cout << "Enter the courses name with no spaces: ";
cin >> n;
students[index].courseRemove(n);
}
}
int menuChoice()
{
cout << "###############################################" << endl;
cout << "Enter '1' to add a student!!" << endl;
cout << "Enter '2' to remove a student!!" << endl;
cout << "Enter '3' to add a course!!" << endl;
cout << "Enter '4' to remove a course!!" << endl;
cout << "Enter '5' to display the info. on all students!!" << endl;
cout << "Enter '6' to exit the program!!" << endl;
cout << "###############################################" << endl;
cout << endl;
int c;
while (true)
{
cout << "Enter a number that matches your choice!! ";
cin >> c;
if (c < 1 || c > 6)
{
cout << "*************************************************" << endl;
cout << "That input does not exist on the menu!!" << endl;
cout << "*************************************************" << endl;
}
else
{
return c;
}
}
}
int main()
{
vector students;
int c;
while (true)
{
c = menuChoice();
switch (c)
{
case 1:
studentAdd(students);
break;
case 2:
studentRemove(students);
break;
case 3:
courseAdd(students);
break;
case 4:
courseRemove(students);
break;
case 5:
studentPrint(students);
break;
case 6:
return 0;
}
cout << endl << endl;
}
return 0;
}
Explanation / Answer
ANSWER::
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char text[200];
fstream file;
file.open ("example.txt", ios::out | ios::in );
cout << "Write text to be written on file." << endl;
cin.getline(text, sizeof(text));
file << text << endl;
file >> text;
cout << text << endl;
file.close();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.