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

#include <iostream> #include <string> #include <cstdlib> #include \"Word.h\" #in

ID: 3548888 • Letter: #

Question

#include <iostream>

#include <string>

#include <cstdlib>

#include "Word.h"

#include <fstream>

#include <vector>

?

using namespace std;

?

int main()

{

vector<string> W;

string word;

ifstream wordFile("C:\Users\David\Desktop\wordlist.txt");

if (wordFile.is_open())

{

while (getline(wordFile, word))

{

W.push_back(word);

for (int i = 0; i < W.size(); i++)

{

cout << W[i] << endl;

}

}

wordFile.close();

}

else

{

cout << "Unable to open file";

    }

string randomWord = rand() % W.size();

cout << randomWord;

system("pause");

return 0;

}

Explanation / Answer

#include <iostream>
#include <string>
#include <cstdlib>
#include "Word.h"
#include <fstream>
#include <vector>
using namespace std;
int main()
{
vector<string> W;
string word;
ifstream wordFile("C:\Users\David\Desktop\wordlist.txt");
if (wordFile.is_open())
{
while (getline(wordFile, word))
{
W.push_back(word);
}
wordFile.close();
}
else
{
cout << "Unable to open file";
}
for (int i = 0; i < W.size(); i++)
{
cout << W[i] << endl;
}
int random_value = rand() % W.size();
cout << W[random_value];
system("pause");
return 0;
}