In: Computer Science
Step 1: Your program will now take its input from this file instead of from the user. Here's what you want to do:
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:
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!
/***************************************************/
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;
}
}
/***************************************************/
/***************************************************/
/***************************************************/