Hello. I carry out the next task from the book in C ++. I made a simple static stack and wrote several functions for working with it. When I display its contents (lines), I get additional pieces of "garbage" after each word.
Please explain what's the matter?
Code:
#include <iostream> using namespace std; struct str_stack { char mas[10][100]; int top; }; void init_stack(str_stack &s); void push_stack(str_stack &s,char st[]); char* pop_stack(str_stack &s); void print_stack(str_stack &s); int main() { str_stack ss; char str[100]; init_stack(ss); cout << "\n***Simple stack***\n"; do { cout<<"Input string in stack: "; cin>>str; push_stack(ss,str); }while(str[0]!='*'); print_stack(ss); return 0; } void init_stack(str_stack &s) { s.top=0; } void push_stack(str_stack &s,char st[]) { int i=0; while(st[i]!='\0'&&i<100) { s.mas[s.top][i]=st[i]; i++; } s.top++; } void print_stack(str_stack &s) { int i; char *ch; cout<<"\n"; while(s.top>0) { i=0; ch=pop_stack(s); while(ch[i]!='\0') { cout<<ch[i]; i++; } cout<<"\n"; } } char* pop_stack(str_stack &s) { return s.mas[--s.top]; }