0

Need help with spaces

I am trying to write a code that deletes numbers from a string and outputs words only. It worked just fine when the input is one word without a space, but what if the input had a space in it, the string will read what is before the space . Therefore I need help with removing numbers from a string with spaces. #include <iostream> #include <ctype.h> using namespace std ; int main (){ int i = 0; char b[i]; string a; cin >> b; while ( b[i]) { } if (isalpha(b[i])) { a = a+b[i]; cout << a; } if (b[i] = ' ') { a = a+b[i]; cout << a; } i++; } }

28th Dec 2020, 2:12 AM
Hakam
14 Antworten
+ 1
You can use getline() and then convert to a c_string or char array.
28th Dec 2020, 2:53 AM
ChaoticDawg
ChaoticDawg - avatar
+ 5
std::cin stops taking input when encountered by a whitespace character. Use getline() or fgets() to get strings with spaces in them.
28th Dec 2020, 2:19 AM
Arsenic
Arsenic - avatar
+ 3
Hakam I just had a look at your program and are you sure it is the correct program you have shared here. and if it is then how is it even compiling without errors ? Because in its current state there are a some errors in it.( And also the initial size of the input string is 0 )
28th Dec 2020, 3:31 AM
Arsenic
Arsenic - avatar
+ 2
Arsenic getline() 😉
28th Dec 2020, 2:24 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Hakam "File not found in this compiler" Which file? Which compiler?
29th Dec 2020, 8:44 AM
Ipang
+ 2
Hakam <ctype.h> is for C, for C++ we include <cctype> instead, note that no .h extension is used, just the name. #include <cctype> See this for more info about C headers usage in C++ 👇 https://en.cppreference.com/w/cpp/header
29th Dec 2020, 2:28 PM
Ipang
+ 1
ChaoticDawg editing done 👍🙂
28th Dec 2020, 3:18 AM
Arsenic
Arsenic - avatar
+ 1
Arsenic true it had errors, but now thanks to your previous comment, i managed to correct it. Here is my code after the edit. #include <iostream> #include <ctype.h> #include <string> #include <algorithm> using namespace std ; int main (){ int i = 0; char b[i]; string a, s; getline(cin, a); for (i = 0; i < a.length(); i++) { b[i] = a[i]; // i used ur advice here if (isalpha(b[i])) {s = s +b[i];} if (b[i] == ' ') {s = s+ " "; } } reverse(s.begin(), s.end()); cout << s; //i needed to reverse the output here }
28th Dec 2020, 3:45 AM
Hakam
+ 1
Use <cctype> (no .h extension) instead of <ctype.h> (load error) 👍
28th Dec 2020, 4:34 AM
Ipang
+ 1
Ipang i am talking about <ctype.h> in this app's compiler. Whenever i try to include it, an error occurs that says file not found, therefors i had to include <ctype> instead
29th Dec 2020, 2:18 PM
Hakam
0
getline() won't work since isalpha() doesn't work with strings, and getline works only with strings
28th Dec 2020, 2:50 AM
Hakam
0
@Hakam. strings can be indexed just like char arrays, so is alpha() will work. There are two versions of getline one for char arrays and one for string.. cin.getline(strName, SIZE)....... for c-style string (char arrays) getline(cin, strName)....... for C++ strings.
28th Dec 2020, 10:41 AM
rodwynnejones
rodwynnejones - avatar
0
Ipang file not found in this compiler rodwynnejones still not working, I would appreciate it if you would demonstrate in a simple code
29th Dec 2020, 8:09 AM
Hakam
0
Hello I don't speak English well. french?yes
29th Dec 2020, 10:34 PM
Koffi sandrino Tamati
Koffi sandrino Tamati - avatar