-2

Ok so im doing a homework assignment where im suppose to read a a text file with a 100 selected words and then read another "text" file and compare how many of those 100 selected words show up on the "text" file and what is the longest and shortest words and how many time those selected words are in the text file. I need complete help this is what i got so far. by the way i still havent inputed the selected files yet so thats why those ifs statements are incomplete

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>


using namespace std;


int main ()
{
int fileNumber;
string Words [100];
string wordCounts [100];
string Frequencies [100];
string shortest = "abcdefghijklmnopqrstuvwxyz";
string longest = "";
int totalWordCount = 0;
ifstream inData;
int I=0;

inData.open("SelectWords.txt"); 

while ( !inData.eof () ) {
    inData >> Words [I];

}
int irow;
while ( irow=0, irow<=100, irow++) {
cout<<Words[I]<<endl;
}
cout<<"Choose a file you want to open"<<endl;
cout<<"1= A Modest Proposal"<<endl;
cout<<"2=Apology" <<endl;
cout<<"3=The Call of the Wild"<<endl;
cout<<"4=The Adventures of Tom Sawyer"<<endl;
 cin>> fileNumber;
 while ((fileNumber<1) || (fileNumber>4))

    {
cout<<"Error please try again"<<endl;
cout<<"Choose a file you want to open"<<endl;
cout<<"1 - A Modest Proposal"<<endl;
cout<<"2 - Apology" <<endl;
cout<<"3 - The Call of the Wild"<<endl;
cout<<"4 - The Adventures of Tom Sawyer"<<endl;
cin>> fileNumber;
 }

  if (fileNumber==1)
 {
 }
 if (fileNumber==2)
 {
 }
 if (fileNumber==3)
 {
 }
 if (fileNumber==4)
 {
 }



system ("pause");
return 0;
}
awesoon
  • 32,469
  • 11
  • 74
  • 99
PopperJuan
  • 175
  • 1
  • 16

1 Answers1

2

I would do something like this:

std::string filename;
switch (filenumber)
{
  case 1:
    filename = "modest_proposal.txt";
    break;
  case 2:
    filename = "apology.txt";
    break;
  //...
}

std::ifstream target_file(filename.c_str());
if (!target_file)
{
  std::cerr << "Error opening file " << filename << endl;
  return EXIT_FAILURE;
}

std::string word;
while (target_file >> word)
{
  // Search word list
}

Notice how I only use the switch for determining the file name. The remaining part of the algorithm is the same regardless of the file name.

Edit 1: Suggestions

  1. Use std::map<word, count> instead of two separate arrays.
  2. Create one text variable for the menu and print the variable twice instead of duplicating the code.
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154