Help With code to continue if user answers (Y/y)

First off there is a header file that goes with this source code so i'll post that first.
--------------------------------------------------------------------------
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
38
#ifndef STACK_H
#define STACK_H

template<class T, int n>

class STACK
{
private: T a[n];
		 int counter;

public: void MakeStack()
		{
			counter = 0;
		}

		bool FullStack()
		{
			return (counter==n)? true:false;
		}

		bool EmptyStack()
		{
			return (counter==0)? true:false;
		}

		void PushStack(T x)
		{
			a[counter] = x;
			counter++;
		}

		T PopStack()
		{
			counter--;
			return a[counter];
		}
};
#endif 


--------------------------------------------------------------------------

Now for the output for the source code is supposed to look as follows:

Enter a sentence: Computer Science
In reverse order: ecneicS retupmoC
CONTINUE(y/n)? y
Enter a sentence: A Toyota
In reverse order: atoyoT A

My Source 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
38
#include <iostream>
#include <ctime>
#include "STACK.h"
using namespace std;

int main()
{

	STACK<char, 80> sent,temp;
	char c,x;
	time_t a;

		time(&a);
		cout<<"Today is: "<<ctime(&a)<<endl;

		sent.MakeStack();
		temp.MakeStack();

		cout<<"Enter a sentence: ";

		while (cin.get(c), c != '\n')
		{
			sent.PushStack(c);
		}

		cout<<"In reverse order: ";

		while (! sent.EmptyStack())
		{
			c = sent.PopStack();
			cout<<c;
		}

		cout<<endl;

		
	return 0;
}


--------------------------------------------------------------------------

I've tried a do while loop, a while loop, if else. The same problems seems to be happening. After I enter the y to continue it will skip over the sentence input from the user and prints the "In reverse order:" on the same line as "Enter a sentence:" then it goes straight back to the continue question.



Last edited on
Try using a bool variable called Quit, along with a while loop:

1
2
3
4
5
6
7
8
bool Quit = false;

while(!Quit) {
//do stuff here

//user wants to quit
Quit = true;
}


HTH
alright new code with your Idea.

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
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <ctime>
#include "STACK.h"
using namespace std;

int main()
{

	STACK<char, 80> sent,temp;
	char c,x,ans;
	bool Quit = false;
	time_t a;

		time(&a);
		cout<<"Today is: "<<ctime(&a)<<endl;

			sent.MakeStack();
			temp.MakeStack();

			while(!Quit) 
			{
				cout<<"Enter a sentence: ";

				while (cin.get(c), c != '\n')
				{
					sent.PushStack(c);
				}

				cout<<"In reverse order: ";

				while (! sent.EmptyStack())
				{
					c = sent.PopStack();
					cout<<c;
				}

				cout<<endl;
				cout<<"CONTINUE(y/n)? ";
				cin>>ans;

				if (ans == 'n' || ans == 'N')
				{ 
					Quit = true;
				}
			}

	return 0;
}


output from cmd:

1
2
3
4
5
6
7
8
9
10
11
12
Today is: Thu Sep 20 00:40:38 2012

Enter a sentence: Please God let this work!
In reverse order: !krow siht tel doG esaelP
CONTINUE(y/n)? y
Enter a sentence: In reverse order:
CONTINUE(y/n)? y
Enter a sentence: In reverse order:
CONTINUE(y/n)? y
Enter a sentence: In reverse order:
CONTINUE(y/n)? n
Press any key to continue . . .

Should this bit:

while (cin.get(c), c != '\n')

be:

while (cin.get(c) != '\n')


If you make use of the toupper function on the variable ans, then you won't have a double test.

But that version of get does not return a character. Removing the c should make it work.
while (cin.get() != '\n')
I think Kentlol's way of writing it actually works.
Ok Peter, I am busted again !!! :D Hope my other advice is OK.
Yeah, using toupper (or tolower) is a good advice ;)
Topic archived. No new replies allowed.