C++ programming help please!! stack push, pop, and display my data to read from
ID: 656552 • Letter: C
Question
C++ programming help please!!
stack push, pop, and display
my data to read from the program text is: A 3A 5A 9D 0A 7
My program should read the file, one line at a time, reading the symbol (A or D) and the integer. If an A is read, the following integer should be pushed onto the stack. If a D is read, the following integer is ignored, and the stack is popped. When your program reaches end of file, it should print out the contents of the stack. and I need help with the code here is my code
#include
#include
using namespace std;
#include "IStack.h"
int main(int argc, char* argv[])
{
// Try to open the file; if fail, report and quit
ifstream inf(argv[1]);
if (!inf)
{
cerr << "Can't open input file " << argv[1] << endl;
return 1;
}
// I need some HELP here with code it needs to also close the file..
return 0;
}
Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
#include "IStack.h"
int main(int argc, char* argv[])
{
// Try to open the file; if fail, report and quit
ifstream inf(argv[1]);
if (!inf)
{
cerr << "Can't open input file " << argv[1] << endl;
return 1;
}
// I need some HELP here with code it needs to also close the file..
IStack intStack; // Assuming IStack is stack of integers.
char ch; int k;
while(!inf.eof()){
inf >> ch >> k;
if(ch == 'A')
intStack.push(k);
else if(ch=='D')
intStack.pop();
}
inf.close(); // close file.
while(!intStack.empty()){ // print remaining elements of stack.
cout << intStack.top() << " ";
intStack.pop();
}
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.