Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribed

This is what is in the file

  1. #include

  2. #include

  3. using namespace std;

  4. struct Node{

  5. int position;

  6. char letter;

  7. Node* next;

  8. };

  9. void insert(Node*& head, int position, char letter);

  10. void print(Node* head);

  11. void deleteALL(Node*& head);

  12. const string FILENAME = "/home/fac/sreeder/class/cpsc1430/lab9input.dat";

  13. int main()

  14. {

  15. ifstream infile;

  16. char thisletter;

  17. int thisposition;

  18. Node* message = nullptr;

  19. infile.open(FILENAME);

  20. cout

  21. if(infile.fail()){

  22. cout

  23. cin.get();

  24. return 0;

  25. }

  26. while(infile.get(thisletter)){

  27. infile >> thisposition;

  28. infile.get();

  29. insert(message, thisposition, thisletter);

  30. }

  31. print(message);

  32. deleteALL(message);

  33. cout

  34. return 0;

  35. }

  36. void insert (Node*& head, int position, char letter)

  37. {

  38. Node* temp = new Node();

  39. if(head == nullptr || head -> position > temp -> position){

  40. temp -> next = head;

  41. head = temp;

  42. }

  43. else{

  44. Node* ptr = head;

  45. while(ptr -> next != nullptr && ptr -> next -> position position){

  46. ptr = ptr -> next;

  47. }

  48. temp -> next = ptr -> next;

  49. ptr -> next = temp;

  50. }

  51. }

  52. void print (Node* head)

  53. {

  54. Node* current = head -> next;

  55. while(current != nullptr){

  56. cout position;

  57. current = current -> next;

  58. }

  59. }

  60. void deleteALL (Node*& head)

  61. {

  62. Node* current;

  63. while(head != nullptr){

  64. current = head;

  65. head = head -> next;

  66. delete current;

  67. }

  68. }

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 23

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions