+ 2
Why is this wrong (C++)??
#include <iostream> #include <string.h> using namespace std; int main() { char msg[]; cin >> msg; strrev(msg); cout << msg; return 0; }
20 Antworten
+ 9
You need to say how long your array is when you declare it.
+ 6
You should get accustomed with the C++ specific string type.
If we want to make your code work as it is, this will work:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
char msg[10];
cin >> msg;
int i=-1;
while(msg[++i]);
reverse(msg, &msg[i]);
cout << msg;
return 0;
}
EDIT: Downvote? Hmm...
EDIT: Two already... LOL!
+ 4
For the strrev() part, the function is not a standard function, so it is not mandatory for compilers to support it.
SoloLearn recently shifted from a Windows environment to Linux and updated their compiler, that's probably why it is not supported here anymore:
https://stackoverflow.com/questions/8534274/is-the-strrev-function-not-available-in-linux
Also see the excellent answer to a similar question in this thread:
https://www.sololearn.com/discuss/2100807/?ref=app
+ 2
Well
+ 2
The "strrev" doesnt work either
+ 2
Lol😂😂 i understand. But sometimes we need to cheat. Otherwise we will not have what we really want. It's too difficult to do this by that way Bilbo Baggins
+ 2
strrev is not standard function
+ 1
well, I haven't seen that library yet, so....
+ 1
I just wanted to know why it doesn't work, and I still don't understand why there is no such method if I just saw it.
+ 1
That's cheating 😄
+ 1
Why is getline cheating? *looks around naively serious*
+ 1
You are right, HonFu, std::getline() is perfect in C++ language, maybe the best way to get strings from input...
But it requires std::string.
In the current thread I was just (academically) curious to see if C++ had changed the way in which a character array (in the C acception) can be defined.
And I am afraid that the answer is no: a character array must always be defined along with a fixed size.
This is eventually the reason to prefer std::string, which is dynamic.
By the way, in Python a character array (string) is even more restrictive: it is immutable, not just fixed size.
0
Flayer
unfortunately it does not work...
./Playground/file0.cpp: In function 'int main()':
./Playground/file0.cpp:10:12: error: expected primary-expression before 'char'
10 | cin >> char msg[];
| ^~~~
0
Bilbo Baggins I works in c#
But try to do something like
cin >> anyvar
char[] anyarr = anyvar.ToCharArray()
I did half of it in c# but try to translate it
0
Hello have you tried to use a String with a getline() function instead?
0
#include<iostream.h>
This while this is wrong
0
Hlo
- 1
eed
- 1
cin >> char msg[]
It will find the size it self