Question: I ' m having issues with this prompt below. I included my code and can only pass 4 / 8 test cases / Fix the
Im having issues with this prompt below. I included my code and can only pass test cases Fix the other test cases without failing the first asap. THanks.
Prompt:
Write a C program that accepts one command line argument for a file name, and ignores any extra command line arguments that may be provided after the first one. If no file name is provided, the program should print on a new line NO SPECIFIED INPUT FILE NAME.", and exit. If the file cannot be opened, print on a new line "CANNOT OPEN THE FILE: followed by the file name, and exit. The program should read from the file lines until the end of file is found. If the input file is empty, it prints out the message "File is empty." on a new line and then exits.
The program should count the number of lines, the number of nonblank lines, the number of words that contain or less characters, the number of words that contain more than characters, the number of names special names and the number of unsigned integers, seen in the file.
A word is defined as a sequence of one or more nonwhitespace characters separated by whitespace. A word is defined as a name if it starts by a letter and followed by zero or more letters, digits, underscores or @ characters. For example, value, val@l numten are valid names, but val, num and @num are not. A word is defined as an unsigned integer if it starts by a digit and followed by zero or more digits ignoring signed integers For example, is an unsigned integer word, while and today are not unsigned integers. Note that a line having only whitespace characters is a nonblank line as well.
#include
#include
#include
#include
#include
using namespace std;
bool isNameconst string& word
static const regex nameRegexazAZazAZ@$;
return regexmatchword nameRegex;
bool isUnsignedIntegerconst string& word
static const regex unsignedIntegerRegexd$;
return regexmatchword unsignedIntegerRegex;
int mainint argc, char argv
if argc
cout NO SPECIFIED INPUT FILE NAME." endl;
return ;
ifstream fileargv;
if file
cout "CANNOT OPEN THE FILE: argv endl;
return ;
string line;
int totalLines nonBlankLines wordsWithFiveOrLessChars wordsWithMoreThanFiveChars ;
int namesCount unsignedIntegersCount ;
while getlinefile line
bool hasNonWhitespace regexsearchline regexS;
if hasNonWhitespace nonBlankLines;
totalLines;
stringstream ssline;
string word;
while ss word
if wordlength wordsWithFiveOrLessChars;
else wordsWithMoreThanFiveChars;
if isNameword namesCount;
else if isUnsignedIntegerword unsignedIntegersCount;
if totalLines
cout "File is empty." endl;
else
cout "Total Number of Lines: totalLines endl;
cout "Number of nonblank lines: nonBlankLines endl;
cout "Number of Words with or Less Characters: wordsWithFiveOrLessChars endl;
cout "Number of Words with More Than Characters: wordsWithMoreThanFiveChars endl;
cout "Number of Names: namesCount endl;
cout "Number ofTotal Number of Lines:
Number of nonblank lines:
Number of Words with or Less Characters:
Number of Words with More Than Characters:
Number of Names:
Number of Unsigned Integers:
Number of nonblank lines:
Number of Words with or Less Characters:
Number of Words with More Than Characters:
Number of nonblank lines:
Number of Words:
Number of Integers:
Number of Unsigned Integers: Number of Words Greater Than Characters:
RESULT: allspaces
RUNNING infile
Total Number of Lines:
Number of nonblank lines:
Number of Words with or Less Characters:
Number of Words with More Than Characters:
Number of Names:
Number of Unsigned Integers:
Number of nonblank lines:
Number of Words with or Less Characters:
Number of Words with More Than Characters:
Number of nonblank lines:
Number of Words:
Number of Integers:
Number of Unsigned Integers: Number of Words Greater Than Characters:
RESULT: intsonly
RUNNING infile
Total Number of Lines:
Number of nonblank lines:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
