+ 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; }

14th Jan 2020, 10:48 PM
Hyst1k
Hyst1k - avatar
20 ответов
+ 9
You need to say how long your array is when you declare it.
14th Jan 2020, 10:57 PM
HonFu
HonFu - avatar
+ 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!
14th Jan 2020, 11:09 PM
HonFu
HonFu - avatar
+ 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
14th Jan 2020, 11:56 PM
Shadow
Shadow - avatar
+ 2
Well
14th Jan 2020, 10:58 PM
Hyst1k
Hyst1k - avatar
+ 2
The "strrev" doesnt work either
14th Jan 2020, 10:58 PM
Hyst1k
Hyst1k - avatar
+ 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
16th Jan 2020, 3:33 PM
Marc Oukass Gomis
Marc Oukass Gomis - avatar
+ 2
strrev is not standard function
16th Jan 2020, 6:05 PM
ASIYA SHAIKH
ASIYA SHAIKH - avatar
+ 1
well, I haven't seen that library yet, so....
14th Jan 2020, 11:12 PM
Hyst1k
Hyst1k - avatar
+ 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.
14th Jan 2020, 11:16 PM
Hyst1k
Hyst1k - avatar
+ 1
As Shadow pointed out, strrev() is not present in GCC for Linux. As HonFu pointed out, you can use reverse() after including <algorithm>: for example reverse(msg, msg+strlen(msg)) And you cannot declare char msg[] without a size
15th Jan 2020, 6:34 AM
Bilbo Baggins
Bilbo Baggins - avatar
+ 1
That's cheating 😄
16th Jan 2020, 3:31 PM
Bilbo Baggins
Bilbo Baggins - avatar
+ 1
Why is getline cheating? *looks around naively serious*
16th Jan 2020, 3:42 PM
HonFu
HonFu - avatar
+ 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.
16th Jan 2020, 6:07 PM
Bilbo Baggins
Bilbo Baggins - avatar
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[]; | ^~~~
16th Jan 2020, 2:58 PM
Bilbo Baggins
Bilbo Baggins - avatar
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
16th Jan 2020, 3:20 PM
Flayer
Flayer - avatar
0
Hello have you tried to use a String with a getline() function instead?
16th Jan 2020, 3:29 PM
Marc Oukass Gomis
Marc Oukass Gomis - avatar
0
#include<iostream.h> This while this is wrong
16th Jan 2020, 5:32 PM
Bathri Narayanana .M
Bathri Narayanana .M - avatar
0
Hlo
16th Jan 2020, 6:29 PM
rajesh
rajesh - avatar
- 1
eed
15th Jan 2020, 12:02 PM
Mehdi Kaya
Mehdi Kaya - avatar
- 1
cin >> char msg[] It will find the size it self
16th Jan 2020, 12:41 PM
Flayer
Flayer - avatar