Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You have been supplied a file with data in it (data2.txt). The line contains a s

ID: 3575079 • Letter: Y

Question

You have been supplied a file with data in it (data2.txt). The line contains a series of integers. Your program tries to push each integer onto the stack, but if the stack is full, your program should just print the value that would have been one integer too many and terminate. For this purpose, the first item is number 1 and its value is 4. Given the data2.txt file, your program should print:

Stopping before trying to push item 11, whose value is 17.

Your grader may, of couurse replace data2.txt with a file of his own to make sure you haven't "faked out" the code.

INSTRUCTIONS:

CURRENT PROGRAM:

DATA2.TXT FILE:

Problem 2. Stack push with safeguards The IStack class used in this problem is the same as used in Problem 1 The data file is different, and your challenge is to write code that tests the stack to make sure it's safe to put something into it You have been supplied a IStack class with the following characteristics: It holds only integers It recognizes only the following functions Stack constructs an empty stack void push (int x) pushes the argument onto the stack void pop pops and discards the top item int top returns a copy of the top item. if this function is called on an empty stack, the program will crash bool empty returns true if the stack is empty bool full returns true if the stack is full that is pushing onto a full stack the program will crash. These functions are sufficient to handle any sequence of data without crashing your program however, only a small stream of data is provided for testing and you need not protect your code against all possible inputs You have been supplied a file with data in it (data 2.txt). The line contains a series of integers Your program tries to push each integer onto the stack, but if the stack is full, your program should just print the value that would have been one integer too many, and terminate. For this purpose, the first item is item number 1 and its value is 4 Given the data2. txt file, your program should print Stopping before trying to push item 11 whose value is 17 Your grader may of course, replace data2.txt with a file of his own, to make sure you haven't "faked out" the code

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string.h>
#include<sstream>
using namespace std;

#include "IStack.h"

int main(int argc, char* argv[]) {
  
ifstream myfile("data2.txt");
string line ;
//getting stack variable
IStack stack = new IStack();
//reading line from file
getline(myfile,line);
istringstream iss(line);

//splitting line to words with delimitter space
//maintaining counter to print the item number
int counter = 1;
int x;
while(iss) {
string word;
iss>>word;
  
stringstream convert(word);
convert>>x;
//if stack is not full add to stack
if(!stack.full())
stack.push(stoi(word));
else {
//cout<<x<<endl;
cout<<"Stopping before trying to put item "+counter;
cout<<", whose value is "+x;
break;
}
counter++;
}
  
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote