How to remove digit, punct w/o ' ' spacing.

This is the code I want to improve.
Is it possible to remove words and punctuation without having to use spacing ' '?
If so can anyone show me how or show me an example? thanks.

** greentxt is failed old code( due to having trouble making it work).**
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
while (is.good()){
		getline (is,line);
			
			for (int i = 0; i< line.size(); i++) {
				if (isalpha(line[i]));
					line[i] =' ';
			}
			for (int i = 0; i< line.size(); i++){
				if (ispunct(line[i]));
				line[i] ='0';                          // remember to add two zero's for each % and /  in the other vector
			}
		
		/*for (int i = 0; i< line.size(); i++) {
		/	if(
				//if (isalpha(line[i]));
		line.erase(
      //std::remove_if(line.begin(), line.end(), &isalpha(line[i])),    // removes words (alpha)
		//line.end());
		
	   line.erase( std::remove_if(line.begin(), line.end(), &ispunct(line[i])),  //  PUNCH THEM punctuations outta here!
		line.end());
		}
		// convert a string to an int
		*/

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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
	
#include "std_lib_facilities_3.h"	
#include <iostream>
#include <vector>
#include <fstream>
#include <iterator>
#include <algorithm> 




vector<string>& store_appointments(string file1, vector<string> appointments){
	ifstream is(file1.c_str());
	if (!is)
	{
		error ("Can't open the input file ", file1);
	}
  
	string line;
	vector<string> *appointments1 = new vector<string>();
	while (is.good())
    {
		getline (is,line);
				
			for (int i = 0; i< line.size(); i++) {
				if (isdigit(line[i]));
					line[i] =' ';
			}
			for (int i = 0; i< line.size(); i++){
				if (ispunct(line[i]));
				line[i] =' ';                          // remember to add two zero's for each % and /  in the other vector
			}
		
		appointments1->push_back(line);   
    }
	
	for (int j = 0; j<appointments.size()-1;++j){
		string temp1 = appointments[j] ;
		appointments1->push_back(temp1);
		}
	return *appointments1;
}
	
	

vector<int>& store_dates(string file1){
	vector<int> *dates = new vector<int>();
	
		
	ifstream is(file1.c_str());
	if (!is)
	{
		error ("Can't open the input file ", file1);
	}
	string line;
	while (is.good()){
		getline (is,line);
			
			for (int i = 0; i< line.size(); i++) {
				if (isalpha(line[i]));
					line[i] =' ';
			}
			for (int i = 0; i< line.size(); i++){
				if (ispunct(line[i]));
				line[i] ='0';                          // remember to add two zero's for each % and /  in the other vector
			}
		
		/*for (int i = 0; i< line.size(); i++) {
		/	if(
				//if (isalpha(line[i]));
		line.erase(
      //std::remove_if(line.begin(), line.end(), &isalpha(line[i])),    // removes words (alpha)
		//line.end());
		
	   line.erase( std::remove_if(line.begin(), line.end(), &ispunct(line[i])),  //  PUNCH THEM punctuations outta here!
		line.end());
		}
		// convert a string to an int
		*/
	istringstream buffer(line);
	int value;
	buffer >> value;  
		
		dates->push_back(value);   
    }
	return *dates;
}


	

vector<int>& store_months(string file1,vector<int> months)							// we gonna load that vector and divide each slot by  1000000 since its int and not double, we will get the exact month
{
		vector<int>dates = store_dates(file1.c_str());
		vector<int> *months1 = new vector<int>();

	for (int j = 0; j<dates.size()-1;++j){
		int	temp4 = dates[j] / 100000000 ;              // added 2 more zero's because / * 2   turns into two zeros
		months1->push_back(temp4);
	}
	for (int j = 0; j<months.size()-1;++j){
		int temp3 = months[j] ;
		months1->push_back(temp3);
		}
	return *months1;
}
		
		
		
vector<int>& store_dayyear(string file1)            					// ok now we make another vector temporary call it dayyear,  by taking %  1000000 of  dates
{

vector<int> dates = store_dates(file1.c_str());
vector<int>*dayyear = new vector<int>();
for (int j = 0; j<dates.size()-1;++j){
		int temp5 = dates[j] %100000000 ;
		dayyear->push_back(temp5);
		}
				return *dayyear;
}

vector<int>& store_days(string file1,vector<int> days)    // ok now we make  vector days1 divide temporary vector dayyear,  by /10000 gives us exact day.
{

vector<int>dayyear=store_dayyear(file1.c_str());

vector<int>*days1 = new vector<int>();
for (int j = 0; j<dayyear.size()-1;++j){
		
	int	temp6 = dayyear[j] /100000 ;
		days1->push_back(temp6);
		}		
		for (int j = 0; j<days.size()-1;++j){
		int temp4 = days[j] ;
		days1->push_back(temp4);
		}
				return *days1;
}
	
vector<int> store_years(string file1,vector<int> years)  // ok now we make  vector years1 modulo temporary vector dayyear,  by %10000 gives us exact year.
{

vector<int>dayyear= store_dayyear(file1.c_str());

vector<int>*years1 = new vector<int>();

for (int j = 0; j<dayyear.size()-1;++j){
		
	int	temp7 = dayyear[j] %100000 ;
		years1->push_back(temp7);
		}
		for (int j = 0; j<years.size()-1;++j){
		int temp2 = years[j] ;
		years1->push_back(temp2);
		}
				return *years1;
}
int main()
{	
	string file1;
	cout << "Please enter the name of the  input file \nEx: file1.txt \n";
	cin >> file1;

	int a = 0;
	vector<string> appointments; //  name of Appointments.
	string appointment;    
	vector<int> years;     //      I split old dates vector into 3 like you wanted   this one has years.
	vector<int> months;    //       Months
	vector<int> days;      //     Days.       
	
	int year;
	int month;
	int day;
	int temp1;
	int temp2;
	string temp3;
	string yes;
	while( a < 99) // limiting you to 99 appointments you HARD WORKING MAN YOU!
	{
		cout << "Would you like to add additional  appointments? type 'Yes' if you do or 'No' to keep going"  <<"\n";
		cin>>yes;
		if(yes!="Yes"){
			break;
		}
		
	
		cout << " Enter a name for an appointment -- 'NO SPACES' --" <<"\n";
		std::cin >> appointment;
		appointments.push_back (appointment);
		cout << "Enter Year  example: '2013' "  <<"\n";
		std::cin >> year;
		years.push_back (year);
		cout << "Enter month  example: '5' "  <<"\n";
		std::cin >> month;
		months.push_back (month);
		cout << "Enter day  example: '15' "  <<"\n";
		std::cin >> day;
		days.push_back (day);

		 
		
		a++;
    }

for (int j = 0; j<months.size()-1 && j<years.size()-1 && j<appointments.size()-1;++j)
{
	temp1 = months[j];
	temp2 = years[j];
	temp3 = appointments[j];	
		
	for(int i = 0; i<days.size()-1;++i)
	{
		if (days[i]> days[i+1])
		{
			days.push_back(i),
			months.push_back(temp1);
			years.push_back(temp2);
			appointments.push_back(temp3);
		}
	}
}
	vector<int> months1;
	vector<int> years1;
	vector<int> days1;
	vector<string> appointments1;

		
for (int j = 0; j<months1.size()-1 && j<years1.size()-1 && j<appointments1.size()-1;++j)
{
	temp1 = months1[j];
	temp2 = years1[j];
	temp3 = appointments1[j];	
		
	for(int i = 0; i<days1.size()-1;++i)
	{
		if (days1[i]> days1[i+1])
		{
			days1.push_back(i),
			months1.push_back(temp1);
			years1.push_back(temp2);
			appointments1.push_back(temp3);
		}
	}
}	
		return 0;
}


Last edited on
You were on the right track with remove_if.

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


int main()
{
    std::string line ;

    while ( getline(std::cin,line) && line.size() )
    {
        line.erase(std::remove_if(line.begin(), line.end(), std::isalpha), line.end()) ;
        std::cout << line << '\n' ;

        line.erase(std::remove_if(line.begin(), line.end(), std::ispunct), line.end()) ;
        std::cout << line << '\n' ;

        // Or, if you want to do it manually:
        //auto letter = line.begin() ;
        //while ( letter != line.end() )
        //{
        //    if ( std::isalpha(*letter) || std::ispunct(*letter) )
        //        letter = line.erase(letter) ;
        //    else
        //        ++letter ;
        //}        
    }

}
Topic archived. No new replies allowed.