delete words from a string

Hello everybody, today my C++ teacher gave us an homework to do:

"Type a function that take in a string containing numbers and others characters and print on stdout all the numbers contained into the string, where for number is mean every maximal numerical sequence, of numbers not separed by spaces. Numbers printed on exit must be separated by commas. For example, if i put in the function the string "paje27hg78 2k f562" my function must print:

27, 78, 2, 562 "


So i started my code like so: (I WANT TO NOTICE THAT WE HAVE ONLY USED IOSTREAM AND FSTREAM LIBRARY FOR NOW, SO PLEASE DON'T USE OTHERS LIBRARY ELSE MY TEACHER WON'T CORRECT MY HOMEWORK!)




#include <iostream>
#include <fstream>

using namespace std;


void delete_words [] = {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z};


int main()

{
string stringa;
getline (cin, stringa);
cout << stringa;

for (stringa)
}





If I have committed some error in some area code, please let me know as I can understand where i am failing.


Thanks a lot in advance to who will help me :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include<string>
#include <sstream> //////using cpp header file
using namespace std;

int main()
{   char ch;
    int no;
    string str1{"abc27efgh9pq555uu6"};
    stringstream ss{str1,ios::in|ios::out};

    while(!ss.eof()){
            ss.get(ch);
     if(ch>='0'&&ch<='9'){
        ss.putback(ch);
        ss>>no;
       cout<<no<<",";
    }
    }
  return 0;
}


sujitnag (45)


#include <iostream>
#include<string>
#include <sstream> //////using cpp header file
using namespace std;

int main()
{ char ch;
int no;
string str1{"abc27efgh9pq555uu6"};
stringstream ss{str1,ios::in|ios::out};

while(!ss.eof()){
ss.get(ch);
if(ch>='0'&&ch<='9'){
ss.putback(ch);
ss>>no;
cout<<no<<",";
}
}
return 0;
}
Edit & Run





http://s27.postimg.org/nqqhhnn5f/error.png





Zaita (2737)
http://www.cplusplus.com/forum/beginner/155478/





Already watched that thread, here there isn't what i need, thanks anyway :)
Last edited on
I use code block and dev c++ ide
in both ide its run ok.

I think you run on visual studio. because I did't use VC++ so i can't help you.
Turn off precompiled headers.
in dev c++ he says:

missing ; at the end of those 2 strings:

string str1{"abc27efgh9pq555uu6"};
stringstream ss{str1,ios::in|ios::out};


in codeblocks he says:

Target uses an invalid compiler; run aborted


any help?
who is he?

code block output


27,9,555,6,
Process returned 0 (0x0) execution time : 0.337 s
Press any key to continue.

dev c++ output


27,9,555,6,
--------------------------------
Process exited after 0.3202 seconds with return value 0
Press any key to continue . . .



copy and past error shows in your ide.
Last edited on
Last edited on
It's really hard to read the code in that picture because you zoomed out so far. The text is tiny.

Did you end your line with a colon (:) instead of a semicolon (;) ?
It looks like even C++11 is not enabled and compiler cannot parse uniform initialization.

Disch (13181)
It's really hard to read the code in that picture because you zoomed out so far. The text is tiny.

Did you end your line with a colon (:) instead of a semicolon (;) ?


No as I have copy-pasted the code.


MiiNiPaa (6724)
It looks like even C++11 is not enabled and compiler cannot parse uniform initialization

If C++11 isn't enabled how i can solve it?



EDIT:

this is what i have in compiler settings on codeblocks:

http://s11.postimg.org/o72bqs2pf/Schermata_2015_02_05_alle_09_58_56.png
Last edited on
Settings → Compiler.

Now depending on GCC version you need either select Have g++ follow the C++11 ISO C++ language standard (to enable C++11 if GCC version < 4.9), or create your own flag -std=c++1y (To enable C++14 if GCC version >= 4.9)
Thanx, added Your string on compiler settings, now i have that:

http://s21.postimg.org/5s3tc0scn/Schermata_2015_02_05_alle_10_03_22.png

damn all this errors will make me crazy!


in the debugger settings i have this:

http://s3.postimg.org/c50umuxmb/Schermata_2015_02_05_alle_10_06_56.png
Last edited on
You need to specify path to gdb. If path on your computer is configured, then writing gdb in executable pat will be enough. Otherwise look where it is and enter path to it.
Just for asking, the same code with C++98? (standard libraries)

As i'm getting mad trying to solve that damn thing.

Thanx guys
replace {} with () on lines where error was.
for positive numbers
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
#include <iostream>

int main() {	
	char ch[500] = "sfsf1v643bh45 er363.36,g25 bn";
	char ar[500];
	int i=0, j=0;
	
	while(ch[i] != '\0'){
		
		if(ch[i] >= '0' && ch[i] <= '9') {
			ar[j] = ch[i];
			if(ch[i+1] <'0' || ch[i+1] >'9'){
				ar[++j] = ',';
				ar[++j] = ' ';
			}
			j++;
		}
		i++;
			
	}
	ar[j]='\0';	
	
	std::cout << ar; // 1, 643, 45, 363, 25,
	
return 0;	
}

anup30 (600)
for positive numbers


#include <iostream>

int main() {
char ch[500] = "sfsf1v643bh45 er363.36,g25 bn";
char ar[500];
int i=0, j=0;

while(ch[i] != '\0'){

if(ch[i] >= '0' && ch[i] <= '9') {
ar[j] = ch[i];
if(ch[i+1] <'0' || ch[i+1] >'9'){
ar[++j] = ',';
ar[++j] = ' ';
}
j++;
}
i++;

}
ar[j]='\0';

std::cout << ar; // 1, 643, 45, 363, 25,

return 0;
}



MiiNiPaa (6727)
replace {} with () on lines where error was.



Thanks to both of You, both codes are now working! If i want to delete the numbers and keep only the words i just need to change the numbers into the letters, right?

Topic archived. No new replies allowed.