Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in C + + Chapter 9 Use an STL stack to reverse a line of input characters that are read into a string. Your stack

in C++ Chapter 9
Use an STL stack to reverse a line of input characters that are read into a string. Your stack should contain the characters of the user's string. Use getline() for input it needs to be part of your C++ tool inventory.
NOTE: Do NOT write your own stack class.
A note on getline: Suppose I am doing the following -
This program reverses a string using the STL stack
Enter your string of less than 80 characters followed by an ENTER
a string input
Enter another? 1= continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an ENTER
a second string
This code will not work by simply using the following loop:
int go =1;
cout "This program reverses a string using the STL stack" endl;
while(go ==1){
cout "Enter your string of less than 80 characters followed by an ENTER" endl;
char* s =(char *)malloc(80);
cin.getline(s,81,'
');
cout s endl;
cout "Enter another? 1= continue. Anything else to stop" endl;
cin >> go;
}
Try it and see what happens! Also note that I never got rid of the memory I allocated with malloc your code must get rid of it. Also, malloc outside the loop for more efficient code.
You must use a getchar()(part of the cstdio library) after cin >> go;
The reason is that when you enter 1 you also use the Enter key. This is still in the buffer when you hit getline again! So you will read in '
'
Also note that when you get something off of the STL stack you must use .top() to look at it followed by .pop() to remove it.
Example:
This program reverses a string using the STL stack
Enter your string of less than 80 characters followed by an Enter
m&m cheeto mayo
oyam oteehc m&m
Enter another? 1= continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an Enter
celery lettuce apple
elppa ecuttel yrelec
Enter another? 1= continue. Anything else to stop
0
Press any key to continue ...
The reason is that when you enter 1 you also use the Enter key. This is still in the buffer when you hit getline
again! So you will read in 'In'
Also note that when you get something off of the STL stack you must use .top() to look at it followed by .pop()
remove it.
Example:
This program reverses a string using the STL stack
Enter your string of less than 80 characters followed by an Enter
m&m cheeto mayo
oyam oteehc m&m
Enter another? 1= continue. Anything else to stop
1
Enter your string of less than 80 characters followed by an Enter
celery lettuce apple
elppa ecuttel yrelec
Enter another? 1= continue. Anything else to stop
0
Press any key to continue ...
image text in transcribed

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

Step: 3

blur-text-image

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

Database And Expert Systems Applications Dexa 2022 Workshops 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 In Computer And Information Science 33

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Alfred Taudes ,Atif Mashkoor ,Johannes Sametinger ,Jorge Martinez-Gil ,Florian Sobieczky ,Lukas Fischer ,Rudolf Ramler ,Maqbool Khan ,Gerald Czech

1st Edition

3031143426, 978-3031143427

More Books

Students also viewed these Databases questions

Question

I receive useful feedback about my performance.

Answered: 1 week ago

Question

I am encouraged to offer opinions/suggestions.

Answered: 1 week ago