Question
The file lab9input.dat contains a secret message. Each line of the file has a single character, followed by a space, which is followed by an
The file "lab9input.dat" contains a secret message. Each line of the file has a single character, followed by a space, which is followed by an integer (could be one or two digits). The integer indicates the placement of the character within the message.
Your job is to write a C++ program that reads in the file, placing using the characters in the correct order, then printing the message.
Please declare the file name as a constant string, as in:
const string FILENAME = "/home/fac/sreeder/class/cs1430/lab9input.dat";
The file read portion will be more complex than in the past - you must read a single character, then use the standard streaming operator to read the integer, then ignore the end-of-line before reading the next character.
This is what is in the file
-
#include
-
#include
-
using namespace std;
-
-
struct Node{
-
int position;
-
char letter;
-
Node* next;
-
};
-
-
void insert(Node*& head, int position, char letter);
-
-
void print(Node* head);
-
-
void deleteALL(Node*& head);
-
-
const string FILENAME = "/home/fac/sreeder/class/cpsc1430/lab9input.dat";
-
-
int main()
-
{
-
ifstream infile;
-
char thisletter;
-
int thisposition;
-
Node* message = nullptr;
-
-
infile.open(FILENAME);
-
cout
-
-
if(infile.fail()){
-
cout
-
cin.get();
-
return 0;
-
}
-
-
while(infile.get(thisletter)){
-
infile >> thisposition;
-
infile.get();
-
insert(message, thisposition, thisletter);
-
}
-
-
print(message);
-
-
deleteALL(message);
-
-
cout
-
return 0;
-
}
-
-
void insert (Node*& head, int position, char letter)
-
{
-
Node* temp = new Node();
-
if(head == nullptr || head -> position > temp -> position){
-
temp -> next = head;
-
head = temp;
-
}
-
else{
-
Node* ptr = head;
-
while(ptr -> next != nullptr && ptr -> next -> position position){
-
ptr = ptr -> next;
-
}
-
temp -> next = ptr -> next;
-
ptr -> next = temp;
-
}
-
}
-
-
void print (Node* head)
-
{
-
Node* current = head -> next;
-
while(current != nullptr){
-
cout position;
-
current = current -> next;
-
}
-
}
-
-
void deleteALL (Node*& head)
-
{
-
Node* current;
-
while(head != nullptr){
-
current = head;
-
head = head -> next;
-
delete current;
-
}
-
}
-
This is my code, unless you need a picture please ask. I seem to be just printing 0000 as the position and the letter is blank.
07 14 10 29 e 25 e 27 5 i 2 a 16 19 d 28 ! 9 1 24 b 8 b 36 h 15 e 4 c 20 a 35 t 30 m 22 33 e 32 12 o 21 . 37 Y 11 e 18 1 34 c3 v 17 h 31 j 6 N1 t 26 u 13 p 23Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started