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++; } }
14 Antworten
+ 1
You can use getline() and then convert to a c_string or char array.
+ 5
std::cin stops taking input when encountered by a whitespace character.
Use getline() or fgets() to get strings with spaces in them.
+ 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 )
+ 2
Arsenic getline() 😉
+ 2
Hakam
"File not found in this compiler"
Which file?
Which compiler?
+ 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
+ 1
ChaoticDawg editing done 👍🙂
+ 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
}
+ 1
Use <cctype> (no .h extension) instead of <ctype.h> (load error) 👍
+ 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
0
getline() won't work since isalpha() doesn't work with strings, and getline works only with strings
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.
0
Ipang file not found in this compiler
rodwynnejones still not working, I would appreciate it if you would demonstrate in a simple code
0
Hello I don't speak English well. french?yes