Hi, I\'m trying to write a code that uses word searching andsorting. I was told
ID: 3615726 • Letter: H
Question
Hi, I'm trying to write a code that uses word searching andsorting. I was told to download the shakespeare txt fromhttp://www.gutenberg.org/files/23041/23041-8.txtthe problem is that after my code is compiled i receive an errormessage:
This application has requested the runtime to terminate it in anunusual way. Please contact the the application's support team"myself and whom ever will help..." for more information"
here is my source code:
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
int main()
{
// set up
srand(time(NULL));
vector<string> poet;
string word;
string phrase; // this is used tobuild each line in poem
string filename;
ifstream read; // used for reading thefile
int nxt_wrd = 0; // declare to search for the new word
int max_words = 80; // default amount formax words
int columns = 40; // default amount for max column
int index; //used for storing the index
cout << "Enter a filename: ";
getline(cin, filename);
read.open(filename.c_str());
while (!read.eof()) //don't read end of file
{
read >>word; // read the next word inthe file
poet.push_back(word);
}
read.close();
//end of set up
cout << "Specify a target length for thepoem[80]: ";
cin >> max_words;
cout << "How wide would you like theoutput in columns[40]: ";
cin >> columns;
index = rand()%poet.size();
word = poet[index];
word = toupper(word.at(0)); //+ word.substr(1);
phrase = phrase + word + " ";
// loop continues until nxt_wrd == max_words
while (nxt_wrd < max_words)
{
// search for next appearanceof words
for (int i = index; i <poet.size(); i=(i+1)%poet.size())
{
if(poet[i] == word)
{
word = poet[(i+1)%poet.size()];
i++;
nxt_wrd++;
phrase = phrase + word + " ";
}
if(phrase.size() >= columns)
{
cout << phrase << endl;
phrase = "";
}
}
}
return 0;
}
Explanation / Answer
sounds like a buffer overflow. when programs stop an EXE fileit usually means that the allocated space of memory has run out.its a run time error, but is a buffer overflow. most programers arenot trained (ie educated ) in bufferoverflows. need a EOF flagmarker is my guess. but I am not a C++ programmer. but I bet if youallocate more memory for the program to use it will work. Thatwould be an option in your compiler. No need to rate, as for I may be mistakenRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.