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:"<
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++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.