+ 1
The following code is showing "ISO C++ forbids comparison between pointer and integer" as an error on VS code!
The following code is showing "ISO C++ forbids comparison between pointer and integer" as an error on VS code! But I didn't even use pointer! What's the problem?? #include <iostream> #include <string> int main(){ std::string sounds{0}; std::getline (std::cin , sounds); int len {sounds.length()}; std::string temp{0}; for(int i=0; i<=len; i++){ while (sounds[i] != " ") { temp += sounds[i]; } if(temp == "Grr"){ std::cout << "Lion" << " "; }else if(temp == "Rawr"){ std::cout << "Tiger" << " "; }else if(temp == "Ssss"){ std::cout << "Snake" << " "; }else if(temp == "Chirp"){ std::cout << "Bird" << " "; } } std::cout << std::endl; return 0; }
16 Réponses
+ 3
You could literally copy the code and just replace
"Grr Rawr Ssss Chirp"
with your input variable then handle your output in the for loop. Treat the vector similar to an array using an index to retrieve the data, just like it is in the current code.
+ 5
One line 9 => while(sound[I] != " ")
You are comparing integer(sound[I]) with a pointer pointing at the start of STRING( { ' ' , '\0'} )which is not allowed.
Replace it with while(sound[I] != ' ') to fix
+ 4
Misbah Ahmed your program is not comming out of your while loop, as value of sound[I] will remain the same every time as the value of *i* is not changing inside the loop
so it will result in an infinite loop
+ 3
Misbah Ahmed here is the working version of what I said👇
https://code.sololearn.com/c0xZKcu5Tz9N/?ref=app
(There are no errors generated, only some warnings(because of comparing long unsigned int to signed int) which can be ignored)
If error still persisted then what is the error message you are receiving?
+ 3
What do you mean by "decorate" the problem ?
Do you want me to fix the indents or something ?
+ 2
These may help you with splitting the input string by a space. Personally I'd use stringstream.
https://code.sololearn.com/cdBL3hTLK746/?ref=app
https://code.sololearn.com/c13pKU43WzH1/?ref=app
+ 1
Arsenic after using ' ', I still get an error!
+ 1
Arsenic nope. Test the code.. It doesn't work!
+ 1
It crush the program.. Like hang!
+ 1
Arsenic I didn't get you! I input the values of sounds, how didn't change the value?
+ 1
Arsenic I got it. But I can't decorate the program. Could you please decorate the whole program for me? It is a code-coach problem taken from sololearn named "Jungle camping"
+ 1
ChaoticDawg +1, I agree, I have done this exercise with stream string because of easy of use.
0
Arsenic I want you to fix my problems. Like I am not getting output as I planned.
0
ChaoticDawg I didn't study vector or Data Structure yet. It looks like hard to me.
0
Misbah Ahmed You should reset temp variable in the end of comparison is done, so your while next iterations to next " " forms new word.
0
Misbah Ahmed Where is you link to your code?