Problem with an open Buffer

Well the sorting isnt the problem. The problem is whenever I go to do it again in a loop I guess the buffer is still open and it doesn't work. Here's the code and Id appreciate some help please.


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
#include <iostream>
#include <string>
#include <stack>

using namespace std;

/*
 * 
 */

void reverse(string& x) {
    stack<char> s;
    const int n = x.length();
    
    //Put characters from x onto the stack
    for(int i=0; i<n; ++i){
        s.push(x[i]);
    }
    
    //take characters off of stack and put them back into x
    for(int i=0; !s.empty(); ++i, s.pop()){
        x[i]=s.top();
    }
}

int main() {
    int go =1;
    char* s = (char *)malloc(80);
    
    while(go == 1){
        cout << "This program reverses a string using the STL stack" << endl;
        cin.getline(s,81,'\n');
        string word(s);
        reverse(word);
        cout << word << endl;
        cout << "Enter another? 1 = continue. Anything else to stop" << endl;
        cin >> go;
    }
    
    return 0;
}
Last edited on
adding a c=getchar()
after cin>> go; fixed it.
Topic archived. No new replies allowed.