Using Stack to Reverse Name

Hey there guys so my C++ class has me creating a program that lets the user enter a character one letter at a time for ex.
Enter name: A
N
G
E
L
And then ends when the user adds a SPACE and is supposed to output the entered characters in reverse order.

Now my question is why does it output it the same way it was inputted? I know this might be a simple solution but any advice would be helpful.
Here's my code:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <stack>
using namespace std;


void reverseName(stack<char> &name);

int main()
{
	
	stack<char> name;
	char singleChar;

	cout << "Enter your name one character at a time: ";

	while (cin.get(singleChar) && singleChar != ' ')
	{
		name.push(singleChar);
	}

	cout << "Your name reversed is: " << endl;

	
	reverseName(name);
	cout << endl;

	return 0;
}

void reverseName(stack<char> &name)
{
	while (!name.empty())
	{
		cout << name.top();
		name.pop();
	}
}
Last edited on
It helps a lot to get out some paper and a pencil and draw what is happening.

That said, I don't see what is wrong.
Topic archived. No new replies allowed.