• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
10 views

Issue:

I am having issue with this find() function in C++, with this little piece of program i have written.

I am getting this error when i run code:

"Runtime error (commonly due to an invalid array/vector access, divide by 0, etc.).

Tests aborted." from coming up.

The challenge activity doesn't give any hints as to help solve the task, but the directions given are as follows...

"Print 'Censored' if userInput contains the word 'love', else print userInput. End with newline."

#include <iostream>
#include <string>
using namespace emp;

int main() {
   string myinput;

   myInput = "Some love snakes .";


    userInput.find('s');
     if (userInput.substr(userInput.find('d'), 4) == "love") {
       std::cout << "Censored" << std::endl;
     }
     else{
       std::cout << myInput << std::endl;
     }


   return 0;
}

by (4k points)  
edited by

1 Answer

0 votes

Solution:

Following code runs fine for me, try this:

#include <iostream>

#include <stdio.h>

#include <stdlib.h>

#include <iostream>

#include <string>

using namespace emp;

int main() {

   string myinput;

   myInput= "Some love snakes.";

    userInput.find('d');

     if (userInput.substr(userInput.find('d'), 4) == "love") {

       std::cout << "Censored" << std::endl;

     }

     else{

       std::cout << myInput<< std::endl;

     }

     userInput = "Some love snakes.";

     if (userInput.substr(userInput.find('d'), 4) == "love") {

       std::cout << "Better language" << std::endl;

     }

     else{

       std::cout << myInput<< std::endl;

     }

   return 0;

}

Conclusion:

The hint telling us that ; find doesn't return false if the substring is not getting found
by (1.4k points)  
edited by
...