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

24th Nov 2020, 4:25 AM
Misbah Ahmed
Misbah Ahmed - avatar
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.
24th Nov 2020, 6:26 AM
ChaoticDawg
ChaoticDawg - avatar
+ 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
24th Nov 2020, 4:31 AM
Arsenic
Arsenic - avatar
+ 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
24th Nov 2020, 4:50 AM
Arsenic
Arsenic - avatar
+ 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?
24th Nov 2020, 4:36 AM
Arsenic
Arsenic - avatar
+ 3
What do you mean by "decorate" the problem ? Do you want me to fix the indents or something ?
24th Nov 2020, 4:55 AM
Arsenic
Arsenic - avatar
+ 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
24th Nov 2020, 6:20 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Arsenic after using ' ', I still get an error!
24th Nov 2020, 4:33 AM
Misbah Ahmed
Misbah Ahmed - avatar
+ 1
Arsenic nope. Test the code.. It doesn't work!
24th Nov 2020, 4:38 AM
Misbah Ahmed
Misbah Ahmed - avatar
+ 1
It crush the program.. Like hang!
24th Nov 2020, 4:38 AM
Misbah Ahmed
Misbah Ahmed - avatar
+ 1
Arsenic I didn't get you! I input the values of sounds, how didn't change the value?
24th Nov 2020, 4:45 AM
Misbah Ahmed
Misbah Ahmed - avatar
+ 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"
24th Nov 2020, 4:53 AM
Misbah Ahmed
Misbah Ahmed - avatar
+ 1
ChaoticDawg +1, I agree, I have done this exercise with stream string because of easy of use.
24th Nov 2020, 4:03 PM
Ilyas Bakirov
Ilyas Bakirov - avatar
0
Arsenic I want you to fix my problems. Like I am not getting output as I planned.
24th Nov 2020, 4:56 AM
Misbah Ahmed
Misbah Ahmed - avatar
0
ChaoticDawg I didn't study vector or Data Structure yet. It looks like hard to me.
24th Nov 2020, 6:22 AM
Misbah Ahmed
Misbah Ahmed - avatar
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.
24th Nov 2020, 4:00 PM
Ilyas Bakirov
Ilyas Bakirov - avatar
0
Misbah Ahmed Where is you link to your code?
24th Nov 2020, 4:01 PM
Ilyas Bakirov
Ilyas Bakirov - avatar