In: Computer Science
The assignment: C++ program or Java
You need to use the following programming constructs/data structures on this assignment.
1. A structure named student which contains:
a. int ID; student id;
b. last name; // as either array of char or a string
c. double GPA;
2. An input file containing the information of at least 10 students.
3. An array of struct: read the student information from the input file into the array.
4. A stack: you can use the standard library template for the stack or you can write your own code for the different stack operations. The stack stores elements of type student.
5. Output file containing a duplicate of all output sent to the console.
What to do:
1. Read all students rows from the input file into an array.
2. Store the information about seven different students in the stack: This is intended to initialize the stack.
3. Generate a random number between 1 and 20.
4. If the randomly generated number is even, read one row from the array and store it (push it) into the stack.
5. If the randomly generated number is odd then check If the number is divisible by 3, if so ask the user if they want to quit.
6. If the user selects to continue or the odd number is not divisible by 3 then pop one element from the stack and send it to the output file.,
7. If the stack is not empty and you quit: it will display the number of the students left in the stack, and display the information of each student left in the stack.
8. If you choose to quit, and the stack is empty when a pop is needed, or the array is empty when a push is required then do the following:
a. Display on the console and send to the output file: “The reason for quitting”, how many elements are left on the stack when quitting, and the content of each student record found in the stack when quitting.
//C++ CODE TO COPY//
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<stack>
using namespace std;
//student struct to keep student data
struct student{
int ID; //student id
string last_name;
double cgpa;
};
//function decleration
void read_std_file(student *std);
void store_std_into_stack(stack <student> ss, student
std[]);
void wite_output_file(student std);
//function to read data from file into array
void read_std_file(student *std)
{
ifstream file;
file.open("student.txt");
if (file.fail())
{
cout << "\nFile opening
error!!";
exit(1);
}
string line;
int counter = 0;
int index = 0;
while (getline(file, line))
{
stringstream ss(line);
string token;
counter = 0;
while (getline(ss, token,
','))
{
if (counter ==
0)
std[index].ID = stoi(token);
else if (counter
== 1)
std[index].last_name = token;
else if (counter
== 2)
std[index].cgpa = stod(token);
counter++;
}
index++;
}
file.close();
}
//function to store data from array to stack for 7
students
void store_std_into_stack(stack <student> ss, student
std[])
{
int rand_num = 0;
char choice;
for (int i = 0; i < 7; i++)
{
rand_num = rand() % 20;
if (rand_num%2==0)
ss.push(std[i]);
if (rand_num%2!=0)
if (rand_num % 3 == 0)
{
cout <<
"\npress y to quit n for not: ";
cin >>
choice;
if (choice ==
'y')
{
if (!ss.empty())
{
cout << "\nNumber of
students on stack: " << ss.size()<<endl;
cout << "ID\t\t"
<< "last name\t\t" << "CGPA" << endl;
for (int i = 0; i <=
ss.size(); i++)
{
student s
= ss.top();
cout
<< s.ID << "\t\t" << s.last_name << "\t\t"
<< s.cgpa<<endl;
ss.pop();
}
}
cout << endl << endl;
exit(0);
}
}
else
{
if
(!ss.empty())
{
wite_output_file(ss.top());
ss.pop();
}
}
}
}
//writing data to output file
void wite_output_file(student std)
{
ofstream file;
file.open("output_student.txt");
if (file.fail())
{
cout << "\nError Opening
output file to write!!";
exit(1);
}
file << std.ID << "," <<
std.last_name << "," << std.cgpa<<"\n";
file.close();
}
//main driver function
int main()
{
//array for 10 students
student std[10];
//stack to store student data from array
stack <student> ss;
//calling function to read student data from
student.txt file
//and store it in std array
read_std_file(std);
//calling function to store data from array to
stack
store_std_into_stack(ss, std);
cout << endl << endl;
}
//OUTPUT//
Comment down for any queries!
Please give a thumbs up if you are satisfied and helped with the
answer :)