In: Computer Science
The c++ program is supposed to store the txt into a stack depending on the what the random number generator assigns each student and stores them into the stack. But it keeps giving me the same outputs and I can't figure out why?
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<stack>
using namespace std;
struct student {
int ID;
string last_name;
double gpa;
};
void read_std_file(student* std);
void store_std_into_stack(stack <student> ss, student
std[]);
void wite_output_file(student std);
void read_std_file(student* std)
{
ifstream file;
file.open("C://temp//students.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].gpa = stod(token);
counter++;
}
index++;
}
file.close();
}
void store_std_into_stack(stack <student> ss, student
std[])
{
int rand_num = 0;
char choice;
for (int i = 0; i < 10; i++)
{
rand_num = rand() % 20 +1;
if (rand_num % 2 == 0)
ss.push(std[i]);
if (rand_num % 2 != 0)
if (rand_num % 3 == 0)
{
cout << "\nPress Y to see stack and N for quit ";
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" << "GPA"
<< endl;
for (int i = 0; i <= ss.size(); i++)
{
student s = ss.top();
cout << s.ID << "\t\t" << s.last_name <<
"\t\t" << s.gpa << endl;
ss.pop();
}
}
cout << endl << endl;
exit(0);
}
}
else
{
if (!ss.empty())
{
wite_output_file(ss.top());
ss.pop();
}
}
}
}
void wite_output_file(student std)
{
ofstream file;
file.open("C://temp//output.txt");
if (file.fail())
{
cout << "\nError Opening output file to write!!";
exit(1);
}
file << std.ID << "," << std.last_name <<
"," << std.gpa << "\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;
}
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stack>
#include <ctime>
#include <cstdlib>
using namespace std;
struct student {
int ID;
string last_name;
double gpa;
};
void read_std_file(student* std);
void store_std_into_stack(stack <student> ss, student
std[]);
void wite_output_file(student std);
void read_std_file(student* std)
{
ifstream file;
file.open("C://temp//students.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].gpa = stod(token);
counter++;
}
index++;
}
file.close();
}
void store_std_into_stack(stack <student> ss, student
std[])
{
int rand_num = 0;
char choice;
for (int i = 0; i < 10; i++)
{
rand_num = rand() % 20 +1;
if (rand_num % 2 == 0)
ss.push(std[i]);
if (rand_num % 2 != 0)
if (rand_num % 3
== 0)
{
cout << "\nPress Y to see stack and N for
quit ";
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" << "GPA" <<
endl;
for (int i
= 0; i <= ss.size(); i++)
{
student s = ss.top();
cout << s.ID << "\t\t" <<
s.last_name << "\t\t" << s.gpa << endl;
ss.pop();
}
}
cout << endl <<
endl;
exit(0);
}
}
else
{
if (!ss.empty())
{
wite_output_file(ss.top());
ss.pop();
}
}
}
}
void wite_output_file(student std)
{
ofstream file;
file.open("C://temp//output.txt");
if (file.fail())
{
cout << "\nError Opening
output file to write!!";
exit(1);
}
file << std.ID << "," <<
std.last_name << "," << std.gpa << "\n";
file.close();
}
//main driver function
int main()
{
// seed the random number generator with
current time value, so that every time the program runs a different
sequence of integers are generated
// without this statement, the random number generator
will generate the same sequence of integers every time the program
is run
srand(time(0));
//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;
}