Backspaces in string

In my Template listNode code doesn`t take a backspaces while using string. How can I fix it?
Example I input "Saudi Arabia". But in output I get only "Saudi"

There is function: create node
1
2
3
4
5
6
7
8
9
10
11
  ...
   Node<T> *newNode; 		
    newNode = new Node<T>;	
    cout<<"\nAdd: ";
    cin>>newNode->data;       
  ////////////////  getline(cin,newNode->data); // I try this but didn`t help
    newNode->link=NULL;	
    cout<<newNode->data;
  
	return newNode;
...


There is function: print
1
2
3
4
5
6
7
8
Node<T> *cur;
    cout<<"List:\n";
    cur=head;		
    while (cur!=NULL)
        {
        cout<<cur->data<<" \n";	
        cur=cur->link;        
        }


I hope somebody can help with this.
Last edited on
I try this but didn`t help
getline() should be what you need to take in a name containing spaces, such as your example "Saudi Arabia".
Just a plain cin >> definitely won't do the job.

However, there's a bigger picture. Whatever is entered via cin is first stored in a buffer. If there is anything remaining in the buffer from any previous input, then results may not be what you want.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int age;
    string name;
    
    cout << "enter age: ";
    cin >> age;
       
    cout << "enter name: ";
    getline(cin, name);
    
    cout << "Name: " << name << " Age: " << age << endl;
}
Above, it is not possible to enter the name as requested. That's because there is a trailing newline character remaining in the input buffer from the previous cin >> operation.

The solution is to clear the buffer after that input.
 
#include <limits> 

11
12
13
    cout << "enter age: ";
    cin >> age;
    cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


It's hard to say how to apply this to your problem, without seeing the rest of the code and what other input there may be.
Ok, there will be rest of 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include <string>

using namespace std;


template< class T >
class Node{
      
    public:
 
    T data;  
    Node<T> *link; 
    };
    
     

template< class T >
class List
{  private:Node<T>*head;
    public:
 List() 
        {
            head = NULL; 
        } 
        Node<T>* CreateNode(T); 
        void AddFirst();
        void PrintList();
        void DeleteNode();
        void DeleteAll();
        
                 int counts();   
             
};


template< class T >
Node<T>* List<T>::CreateNode(T value) 
{
    Node<T> *newNode; 		
    newNode = new Node<T>;	
    cout<<"\nAdd: ";
    
    cin>>newNode->data;
  // getline(cin,newNode->data);
     
    newNode->link=NULL;	
    cout<<newNode->data;
  
	return newNode;
    	

}

//-----------------------------------------------------
template< class T >

void List<T>::AddFirst() //(Node *head, Node *newNode)
{ 
   
   T value;
    Node<T> *newNode, *cur;

    newNode = CreateNode(value);
    if (head == NULL)
    {
        head = newNode;
        head->link = NULL;          
    }
    else
    {
        cur = head;
        head = newNode;
        head->link = cur;
    }
   
   
   
}
//----------------------------------------------------
template< class T >

void List<T>::PrintList() 
{ 
    Node<T> *cur;
    cout<<"List:\n";
    cur=head;		
    while (cur!=NULL)
        {
        cout<<cur->data<<" \n";	
        cur=cur->link;        
        }
}



template< class T >

void List<T>::DeleteNode() 
{  int pos, i, counter = 0;
    if (head == NULL)
    {
        cout<<"What delete?"<<endl;
        return;
    }
    cout<<"Enter position to delete: ";cin>>pos;
    
    
    
    Node<T> *cur, *oldNode;
    cur = head;
    if (pos == 1)
    {
        head = cur->link;
    }
    else
    {
        while (cur != NULL)
        {
            cur = cur->link;
            counter++;  
        }
        if (pos > 0 && pos <= counter)
        {
            cur = head;
            for (i = 1;i < pos;i++)
            {
                oldNode = cur;
                cur = cur->link;
            }
            oldNode->link = cur->link;
        }
      
        delete cur;
      
    }

}


template< class T >

void List<T>::DeleteAll() 
{Node<T> *cur1, *cur;
    if(head==0)
   {cout << "List is empthy!";}
    cur=head;
        while(cur)
        {
        cur1=cur;
        cur=cur->link;
        delete cur1;
        }
        head=NULL;
        
       // return 0;
    
}

//------------------------------------------
template< class T >

int List<T>::counts() 
{Node<T> *cur; *head;
    int count=0;
    cur=head;
    while(cur)
    {
        count++;
        cur=cur->link;
    }
    return count;
}

int main()
{

int newNode, cur, oldNode, cur1;

List<string> cl;

int izv;

do
{
cout << "1. Add\n";
cout << "2. Delete\n";
cout << "3. Print\n";
cout << "4. Count\n";
cout << "5. Delete All\n";

cout<<"0. Exit\n";
cin >> izv;

if(!cin)
{
    
    system("Pause>null");
}

switch(izv)
	{	
        case 1:        
    	cl.AddFirst();
        	break;
		case 2:		
			cl.DeleteNode();
			break;
		case 3:
			cl.PrintList();
			break;				
		case 4:  
			cl.counts();
			break;
		case 5:
			cl.DeleteAll();
			break;
		case 0: exit(0); break;
		
       
	}
} while(izv!=0);

system("PAUSE>nul");
return 0;
}
Thanks for posting the rest of the code.

I can see two places where there is a cin >> which need looking at.
The change I suggest is to add the ignore() code which will read and discard characters from the input buffer until it finds a newline character.

The most important one is ther menu selection in main().
195
196
    cin >> izv;
    cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

and the other is in the delete node code.
108
109
    cout<<"Enter position to delete: ";cin>>pos;
    cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );


Now you should be able to use getline as in the original question at line 46:
46
47
    // cin>>newNode->data;  // replaced by getline
    getline(cin,newNode->data);



I've not tested this, but it should be pretty much what you need.

* remember to add the #include <limits>
Thanks, it`s works
Topic archived. No new replies allowed.