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

I have a word file named words.1 and am trying to run it in the command line of

ID: 672551 • Letter: I

Question

I have a word file named words.1 and am trying to run it in the command line of c++ on visual studio. I do not understand where to save the word file to and what to put on command line to access it. I feel like I've tryed everything. Anyone know the answer to my problem?

#include <iostream>
#include <ctype.h>
#include <string>
#include <fstream>
using namespace std;
struct CmdLineRecord
{

   enum { MAX_NAME_LENGTH = 32 }; // Max allowable filename chars
   int SyntaxError; // True if user syntax is incorrect
   int WantsLineCount; // set to true if user wants line count
   int WantsWordCount; // set to true if user wants word count
   int WantsCharCount; // set to true if user wants char count
};
struct CountsRecord
{
   unsigned LineCount; // number of lines
   unsigned WordCount; // number of words
   unsigned CharCount; // number of characters
};
void DetermineWhatUserWants(CmdLineRecord & User, int arg, char * arv[]);
int CountLineWordChar(CountsRecord & Data, char File[]);
void ReportResults(CmdLineRecord User, const CountsRecord & Data);
int main(int argc, char * argv[])
{
   CmdLineRecord User;
   CountsRecord Data;
   int FileOpened = 1;
   DetermineWhatUserWants(User, argc, argv);
   if (!User.SyntaxError)
       FileOpened = CountLineWordChar(Data, argv[1]);
   if (FileOpened)
       ReportResults(User, Data);
   else
       cerr << "Cannot open file " << argv[1];
   return 0;
}
/************** DetermineWhatUserWants ****************************
Action : Function will determine what the user wants to have displayed
for the count, based on command line input, i.e. does user
want to display how many lines or words or characters or
combination of them, or if an input error occurred. Second
argument in command line must be the file name the third argument
is the option of display.
Command line options are inputed after the "/" char and are:
c : character count
w : word count
l : line count
If no options are inputed then the default of display all count
totals is done, otherwise the option is done. One option or two
is permitted after the "/", illegal character option will result
in a syntax error.
Parameters:
Reference :
U : User variable of struct type given above
Value
arg : number of command line arguments
*arv[] : array of pointers each pointing to different argument
Returns : nothing
Preconditions : None
=======================================================================*/
void DetermineWhatUserWants(CmdLineRecord & U, int arg, char * arv[])
{
   U.SyntaxError = 0;
   U.WantsLineCount = 0;
   U.WantsCharCount = 0;
   U.WantsWordCount = 0;
   int i;

  
   if (arg == 1) // no file name
   {
       cerr << "No filename input" << endl;
       U.SyntaxError = 1;
   }
   else if (arg == 2)
   {
       U.WantsCharCount = 1;
       U.WantsLineCount = 1;
       U.WantsWordCount = 1;
   }
   else if (arg == 3)
   {
       if (arv[2][0] != '/') //doesn't start with a slash
       {
           cerr << "Argument must start with a slash" << endl;
           U.SyntaxError = 1;
       }
       else
       {
           for (i = 1; arv[2][i]; i++)
           {
               switch (tolower(arv[2][i]))
               {
               case 'l':
                   U.WantsLineCount = 1;
                   break;
               case 'c':
                   U.WantsCharCount = 1;
                   break;
               case 'w':
                   U.WantsWordCount = 1;
                   break;
               default:
                   U.SyntaxError = 1;
                   cerr << "Not a valid" << endl;
               }
           }
       }
   }
   else //too many arguments
   {
       cerr << "Too many arguments" << endl;
       U.SyntaxError = 1;
   }
}
/********************* CountLineWordChar *****************************
Action : Function will count the number of characters, words and lines
in given input stream of text ended by control Z, ^Z, EOF
Parameters:
Reference :
Data : variable of type struct given above
Value
File[] : second command line argument that has the file to read from
Returns : 1 if file opened, 0 if can not open file or not found
NOTE : Characters are everything, including newline and form feed.
Words are delimited by whitespace characters and EOF.
Does not take into consideration hypentation, words are composed
numbers and letters, punctuation also included in words.
Precondition : none
=======================================================================*/
int CountLineWordChar(CountsRecord & Data, char File[])
{
   char ch; // current character in stream
   char NextCh; // Next character in stream
   ifstream FileIn; // declare FileIn to be input file
   FileIn.open(File);
   if (FileIn.fail())
       return 0;

   Data.CharCount = 0;
   Data.WordCount = 0;
   Data.LineCount = 0;

   while (FileIn.get(ch))
   {
       NextCh = FileIn.peek();
       Data.CharCount++;
       if (ch == ' ')//end of the line
           Data.LineCount++;
       else if (NextCh == EOF)
           Data.LineCount++;
       if (isspace(ch) || (NextCh == EOF) && !isspace(NextCh))
       {
           Data.WordCount++;
       }
   }
   FileIn.close();
   return 1;
}
/********************** ReportResults ********************************
Action : Function will display the number of words, lines or characters
or all of them depending on what the user entered on the command
line input.
Parameters :
Value : 2 value parameters, User - what to display
Data - holds the number to display
Returns : nothing
======================================================================*/
void ReportResults(CmdLineRecord User, const CountsRecord & Data)
{
   if (User.SyntaxError)
       return;
   if (User.WantsCharCount)
   {
       cout << "Characters: " << Data.CharCount << endl;
   }
   if (User.WantsLineCount)
   {
       cout << "Lines: " << Data.LineCount << endl;
   }
   if (User.WantsWordCount)
   {
       cout << "Words: " << Data.WordCount << endl;
   }
}

Explanation / Answer

Hi,

I checked your program it is correct no error found.You can use Command line arguments to access your code.

Command line arguments are optional string arguments that a user can give to a program upon execution. These arguments are passed by the operating system to the program, and the program can use them as input.On the command line, to run a program, you can simply type in its name.

Then go to its installation directory.Inside that command line window, browse to where your source code (CPP file) is at. You do so by using the cd command.

C: directory name >Words.txt

I hpe information is useful for you

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