Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, this is my c++ code and I am confused on how to fix this error in my code an

ID: 3790925 • Letter: H

Question

Hi, this is my c++ code and I am confused on how to fix this error in my code and I'm curious if it's an easy solution or is it a fundemental problem with my code. Thank You!

***EDIT***: The Error Code comes up as:

" [main] C:UsersOwner.atom.exe 1008 (0) handle_exceptions: Exception: STATUS_ACCESS VIOLATION

[main] a 1008 (0) handle_exceptions : Dumping stack trace to a.exe.core "

And heres my code:

#include
#include
#include
using namespace std;
int first_and_only_function(string Entered_Word, string Another_Word)
   {
  
   vector NumberofWords;
   string Key_Word_Needed = "*endl*";
   int word_count = 0;
   int Number_Of_Times = 0;
   string new_Entered_Word;
   string New_Another_Word;
   while(true)
   {
   cout<<"Please Enter a search word (Terminating word is '*endl*'. ";
  
   getline(cin,new_Entered_Word);
  
  
       if ((Key_Word_Needed.compare(new_Entered_Word)==0))
       {
           break;
       }
       NumberofWords.push_back(new_Entered_Word);
       word_count++;
   }
       cout<<"The amount of tries were: "< cout<<"Let's find the number of times the word you'd like to find comes up! ";
   cout<<"So, if you wanted to find out how many times 'bear' came up, just type in 'bear' again to see. ";
  
   getline(cin,New_Another_Word);
   for(int i=0; i<=word_count;i++)
   {
       if(NumberofWords[i].compare(New_Another_Word)==0)
       {
       Number_Of_Times++;
       }
   }
   cout<<"The word came up:"<

C Cygwin B20 Please Enter a search word (Terminating word is '*endl* bear Please Enter a search word (Terminating word is '*endl*' bear Please Enter a search word (Terminating word is '*endl*' bear Please Enter a search word (Terminating word is '*endl*' bear Please Enter a search word KTerminating word is *endl* goose Please Enter a search word (Terminating word is '*endl* COW Please Enter a search word (Terminating word is '*endl*' *enda Please Enter a search word (Terminating word is '*endl*' Eendl The amount of tries were 8 including correct search word Let's find the number of times the word you' d like to find comes up! So if you wanted to find out how many times bear came up just type in bear again to see. bear [main 1 C: Users owner ..atom a.exe 1017 K0 handle exceptions Exception STATUS ACCESS UIOLATION [main a 1017 C0 handle exceptions Dumping stack trace to a-exe -core bash-2.02$

Explanation / Answer

Although your code is not complete, I did a debug on this by adding the remaining items by myself based on the function.

I received a runtime error. Then when I altered the for loop, it worked fine. Instead of checking for less than equal to, check for less than. It works fine.

for(int i=0; i <= word_count;i++)
   {
       if(NumberofWords[i].compare(New_Another_Word)==0)
       {
       Number_Of_Times++;
       }
   }

Instead of the above code, use the below one

for(int i=0; i <word_count;i++)
   {
       if(NumberofWords[i].compare(New_Another_Word)==0)
       {
       Number_Of_Times++;
       }
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote