Question

In: Computer Science

The c++ program is supposed to store the txt into a stack depending on the what...

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;
}

Solutions

Expert Solution

#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;
}


Related Solutions

I'm supposed to create a c++ program which is supposed to take a sentence as an...
I'm supposed to create a c++ program which is supposed to take a sentence as an input and then output a sorted list of the occurrence of each letter. ex. Enter a phrase: It's a hard knock life A2 I2 K2 C1 D1 E1 F1 H1 L1 N1 O1 R1 S1 T1 I was also given a recommended code to use as a bubble sort procedure bubbleSort( A : list of sortable items ) n = length(A) repeat swapped =...
In 2 to 3 paragraphs describe the C program written below (What the program is supposed...
In 2 to 3 paragraphs describe the C program written below (What the program is supposed to do). State the requirements for the program (How does the program meet the requirements) and discuss the logical process your program uses to meet those requirements (The process steps to meet the requirements). #include "stdio.h" int main(void) { //initialize array int arr[100];   //initialize variables   int i=0, j=0, n=0;    //infinite loop which will stop when user enters -1   while(n != -1) {   ...
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing...
Create a c++ program that: Create an input file using notepad ( .txt ). When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File” marker. Input date...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like...
JAVA - Quick Sort .txt and Return ----------------------------------------------------------------------------- The program should input a .txt file like below and must use a Quick sort Algorithm ================ 6 10 4 19 10 12 8 6 0 1 2 3 ================ The first number "6" represents the index of the element to return after sort the second number on the top "10" represents the number of elements or size of array. The following numbers and lines are the elements that need to go...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT