Question

In: Computer Science

Step 1: Your program will now take its input from this file instead of from the...

Step 1: Your program will now take its input from this file instead of from the user. Here's what you want to do:

  1. Remove the call to getUserInput(). You can also remove its definition and prototype if you like.
  2. For reasons that will become clear later, don't close the file until just before the return statement at the end of main.

Run your code to make sure that the file is opened and read correctly. Hint: you may test the correctness of code by printing the contents of this file, one string at a time.

//Code to be adjusted

#include<iostream>

#include<string>

using namespace std;

//prompts user for input and returns a string containing userinput

string getUserInput(){

string input;

getline(cin,input);

return input;

}

//returns true if given character is vowel else false

bool isVowel(char c){

if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')

return true;

return false;

}

//returns true if given character is an alphabet else false

bool isAlphabet(char c){

if((c >= 'a' && c<='z') || (c >= 'A' && c<='Z'))

return true;

return false;

}

//returns true if given character is digit else false

bool isNumber(char c){

if(c >= '0' && c <= '9')

return true;

return false;

}

//Prints the report

void printReport(int vowels, int cons, int digit, int special){

cout<<"Vowels: "<<vowels<<endl;

cout<<"Consonants: "<<cons<<endl;

cout<<"Digits: "<<digit<<endl;

cout<<"Special characters: "<<special<<endl;

}

//driver function

int main(){

//to store user string

string input;

//counters for number of vowels, consonants, digits and special characters respectively

int vowel = 0;

int consonant = 0;

int digit = 0;

int specialChar = 0;

//gets user input

input = getUserInput();

//iterating over input and counting the number of vowels, consonants, digits and special characters

for (int i=0;i<input.length();i++){

if(isVowel(input[i])){

vowel++;

}

else if (isAlphabet(input[i])){

consonant++;

}

else if(isNumber(input[i])){

digit++;

}

else{

specialChar++;

}

}

//Displaying the report

printReport(vowel,consonant,digit,specialChar);

return 0;

}

Ex: If the input file is:

Input

words.txt

Your output (end the following message with a newline)

Could not open file words.txt 

Step 2:

  • Process each line in the file to get and print if a variable name is valid or not. For this purpose, you will need to modify printReport function (both definition and prototype) to print whether a given variable name is valid or not.
void printReport(string userText, int vowelCount, int consoCount, int digitCount, int specialCount)
{
    //implement the three rules of checking if the input word is a valid C++ variable or not    

   //you may copy the code below in your printReport() function
    if (isValid) {
        cout << "The variable name - " << userText <<  " - is valid." << endl;
    }
    else {
        cout << "The variable name - " << userText << " - is invalid." << endl;
    }

}

You must print the message in printReport() for each word in the input file (words.txt).

At this moment, your main() is going to have code that opens and reads contents of a file, does counting (vowels, consonants, digits, and special characters) and calls printReport(). Go ahead and run it to make sure that your code gives the correct output for a test string.

Step 3: Build a function called getCount() that takes a string parameter (for the inputText that is being analyzed) and four additional parameters corresponding to the count of each kind of character. These would be passed by reference and their value will be set by this function.

Prototype: void getCount(string userText, int& vowelCount, int& consoCount, int& digitCount, int& specialCount);.

Refactor your main() so that it only calls getCount and printReport by passing it appropriate arguments. At this stage, your main() should have code that opens and reads from a file and calls getCount() and printReport() for each word read from the input file. Run your code to make sure it works and gives the correct output for a test string.

getCount() function that does all the counting so as to reduce the code inside main() even further!

Text files:

data1.txt

Ihaveadream

Elponitnatsnoc

yellowtiger

x

cat?dog

star!search

Gameover!

data2.txt

This

file

contains

15

words

It

has

lots

of

letters

and

very

few

special

characters!

Fallis@wesomein$onomaCounty!

Solutions

Expert Solution

/***************************************************/

Where we have to paste the input file...?

/***************************************************/

// data1.txt (Input file)

Ihaveadream
Elponitnatsnoc
yellowtiger
x
cat?dog
star!search
Gameover!
data2.txt
This
file
contains
15
words
It
has
lots
of
letters
and
very
few
special
characters!
Fallis@wesomein$onomaCounty!

/***************************************************/

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void getCount(
string userText, int& vowelCount, int& consoCount, int& digitCount, int& specialCount);
bool isVowel(char c);
bool isAlphabet(char c);
bool isNumber(char c);
void printReport(string userText, int vowelCount, int consoCount, int digitCount, int specialCount);

// driver function

int main()
{

// to store user string

string input, str;
ifstream dataIn;

// counters for number of vowels, consonants, digits and special characters respectively

int vowel = 0;

int consonant = 0;

int digit = 0;

int specialChar = 0;

cout << "Enter the input file : ";
cin >> input;
dataIn.open(input.c_str());
/*
* checking whether the file name is valid or not
*/
if (dataIn.fail())
{
cout << "could not open file " << input << endl;
;
return 1;
}
else
{
/*
* Reading the data from the file
*/
while (getline(dataIn, str))
{
getCount(str, vowel, consonant, digit, specialChar);
printReport(str, vowel, consonant, digit, specialChar);
}
/*
* closing the file
*/
dataIn.close();
}


return 0;
}


void getCount(string input, int& vowelCount, int& consoCount, int& digitCount, int& specialCount)
{
// iterating over input and counting the number of vowels, consonants, digits and special
// characters

for (int i = 0; i < input.length(); i++)
{

if (isVowel(input[i]))
{

vowelCount++;
}

else if (isAlphabet(input[i]))
{

consoCount++;
}

else if (isNumber(input[i]))
{

digitCount++;
}

else
{

specialCount++;
}
}
}
// returns true if given character is vowel else false

bool isVowel(char c)
{

if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I'
|| c == 'O' || c == 'U')

return true;

return false;
}

// returns true if given character is an alphabet else false

bool isAlphabet(char c)
{

if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))

return true;

return false;
}

// returns true if given character is digit else false

bool isNumber(char c)
{

if (c >= '0' && c <= '9')

return true;

return false;
}

// Prints the report
void printReport(string userText, int vowelCount, int consoCount, int digitCount, int specialCount)
{
cout << "Vowels: " << vowelCount << endl;

cout << "Consonants: " << consoCount << endl;

cout << "Digits: " << digitCount << endl;

cout << "Special characters: " << specialCount << endl;

// implement the three rules of checking if the input word is a valid C++ variable or not
bool isValid = true;
if (!(isAlphabet(userText[0]) || userText[0] == '_'))
{
isValid = false;
}
for (int i = 1; i < userText.length(); i++)
{
if (!(isAlphabet(userText[i]) || isNumber(userText[i]) || userText[0] == '_'))
{
isValid = false;
break;
}
}


// you may copy the code below in your printReport() function
if (isValid)
{
cout << "The variable name - " << userText << " - is valid." << endl;
}
else
{
cout << "The variable name - " << userText << " - is invalid." << endl;
}
}

/***************************************************/

/***************************************************/

/***************************************************/


Related Solutions

Program Language C++ How do I take lines of string from an input file, and implement...
Program Language C++ How do I take lines of string from an input file, and implement them into a stack using a double linked list? Example input, command.txt, and output file. Ex: Input.txt postfix: BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D / E+ Command.txt printList printListBackwards Output.txt List: postfix:BAC-* prefix:+A*B/C-EF postfix:FE-C/B*A+ postfix:AB-C-D/ postfix:AB-CF*-D/E+ Reversed List: postfix:AB-CF*-D/E+ postfix:AB-C-D/ postfix:FE-C/B*A+ prefix:+A*B/C-EF postfix:BAC-*
Write a program that takes its input from a file of number type double and outputs...
Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks and/ or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. File name: pr01hw05input.txt 78.0 87.5 98.1 101.0 4.3 17.2 78.0 14.5 29.6 10.2 14.2 60.7 78.3 89.3 29.1 102.3 54.1...
Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
Your assignment is to build a program that can take a string as input and produce...
Your assignment is to build a program that can take a string as input and produce a “frequency list” of all of the words in the string (see the definition of a word below.) For the purposes of this assignment, the input strings can be assumed not to contain escape characters (\n, \t, …) and to be readable with a single input() statement. When your program ends, it prints the list of words. In the output, each line contains of...
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
Write a program that will read in from input file one line at a time until...
Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces, commas and periods....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT