Could someone please modify the following code to print the results to the file,
ID: 3714411 • Letter: C
Question
Could someone please modify the following code to print the results to the file, along with the screen.
//directives used
#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;
//just the programs driver
int main(){
struct node *root = NULL;
string line;
ifstream infile("A3.DAT");
while (getline(infile, line)){
istringstream iss(line);
char c;
int num;
while (iss >> c){
if(c == 'I' || c == 'i'){
iss >> num;
root = insert(root, num);
cout << num << " has been inserted." << endl;
}
else if(c == 'D' || c == 'd'){
iss >> num;
root = deleteNode(root, num);
cout << num << " has been deleted." << endl;
}
}
}
cout << "Inorder traversal of the BST:" << endl;
inorder(root); //call print function
return 0;
}
Explanation / Answer
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include <stdlib.h>
using namespace std;
//just the programs driver
int main(){
struct node *root = NULL;
string line;
ifstream infile("A3.DAT");
ofstream outfile("OUTPUT.DAT");
while (getline(infile, line)){
istringstream iss(line);
char c;
int num;
while (iss >> c){
if(c == 'I' || c == 'i'){
iss >> num;
root = insert(root, num);
outfile << num << " has been inserted." << endl;
}
else if(c == 'D' || c == 'd'){
iss >> num;
root = deleteNode(root, num);
outfile << num << " has been deleted." << endl;
}
}
}
outfile << "Inorder traversal of the BST:" << endl;
inorder(root); //call print function
return 0;
}
========================
Output In main methods are into FILE OUTPUT.DAT
Since Delete, Insert and and Inorder functions are NOT Provided. So Output from them will not Go into file
Thanks, PLEASE UPVOTE if helpful
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.