Question
7.12 Final Submission: Traversing a string and analyzing its characters (file IO) This is the last of the three steps of Project-1 You are writing
7.12 Final Submission: Traversing a string and analyzing its characters (file IO)
This is the last of the three steps of Project-1
You are writing a variable name validator. Given a file containing words, you will need to apply all of the three rules below to see if each given word to determine if its a valid C++ variable name or not.
Rules:
- A variable name must begin with a letter of the alphabet. It cannot begin with a digit.
- A variable name must not begin with an Uppercase letter since that convention is usually used for identifying constants.
- No special characters are allowed.
Note - By no means, is this a complete list for naming variables in C++. In the interest of time, I have picked the above three for you to implement in this project.
Ex: If the input file is:
data2.txt
the output is:
The variable name - This - is invalid. The variable name - file - is valid. The variable name - contains - is valid. The variable name - 15 - is invalid. The variable name - words - is valid. The variable name - It - is invalid. The variable name - has - is valid. The variable name - lots - is valid. The variable name - of - is valid. The variable name - letters - is valid. The variable name - and - is valid. The variable name - very - is valid. The variable name - few - is valid. The variable name - special - is valid. The variable name - characters! - is invalid.
reate a new project and call it Project-1. Store it in an appropriate location (one possibility is by mounting your *blue* account and saving it there). - Click New Project on the [Welcome Screen](https://www.jetbrains.com/help/clion/welcome-screen.html). - In the New Project dialog that opens, select the target type of your project (executable), and the language to be used (C++ 14). - You are now ready to write your code on main.cpp - Copy your code for this problem from CheckPoint B and ensure you can compile and run it!
The variable name - Fallis@wesomein$onomaCounty! - is invalid.
Use these instructions for Checkpoint B:
5.34 Checkpoint B: Traversing a string and analyzing its characters (functions)
Given a line of text as input, output the number of Vowels, Consonants, Digits and Special characters.
Definitions:
- A consonant is any alphabetic character that's not a vowel
- A special character is any character that is neither alphabetic nor a digit.
Ex: If the input is:
Fallis@wesomein$onomaCounty!
the output is:
Vowels: 11 Consonants: 14 Digits: 0 Special characters: 3
Step 1. Getting started with refactoring
Refactoring code means to restructure it to make it easier to read, modify, and potentially extend. At this point, your main tool for refactoring is adjusting the way you break a task into functions.
In this part of the project, you're going to replace a lot of the code in main() with calls to functions that you will define yourself. These instructions will walk you through this first function in a little more detail, and then you will build the others only from specifications.
The first piece of refactoring you'll do is to move the lines of code that print the 4-line report at the end of your program into their own function, which we'll call printReport(). Before you go any further, decide what the function's signature -- that is, the part of its definition that includes the return types and parameter list -- should look like. Write it down or type it somewhere. When you think you have it, highlight the whitened text below to see the text hidden in it. void printReport(int v, int c, int d, int s);.
Your parameter names and their order can differ from mine, but the types should match.
Next, move around code in your program so that this function actually prints the report, and main() just calls it. You should put the function's prototype (that is, the contents of the black box or their equivalent) at the beginning of your program and the actual definition after main(). Here's pseudocode:
// Include necessary libraries // Prototype for writeReport goes here: ___ printReport( /* params */ ); int main() { // most of your code printReport( /* whatever you pass it */); // call the new function return 0; } ___ printReport( /* params */ ) { // Print the counts of vowels, consonants, digits, and special characters // If you do this right, the output shouldn't change at all }
Troubleshooting If you have trouble getting above to work, troubleshoot using the following questions:
- Does your function have a prototype that appears above main() in your code?
- Is your function actually defined anywhere? This requires the information in the prototype, followed by curly braces that contain the actual code for the function.
- Is the prototype identical to the first part of the function definition (the part before the curly braces)?
- Does the prototype end with a semicolon?
- Within each of your functions (you have 2 so far), are you being consistent with local variable names? Your vowel counter (for example) does not need to have the same name in main() that it does in printReport, but you should be consistent within each one. When you're refactoring, you will often have to adjust variable names when they move from one function to another.
- Are you making the mistake of declaring local variables that shadow your function parameters? In general, whenever you use a variable, can you point to where it was declared and confirm that it's in the right scope?
- Are you passing arguments to your function in the right order? Remember, it doesn't matter what their names are: the first argument you use will get passed as the first parameter, and so on.
- Are you actually calling your function and, where appropriate, actually using its return value?
Step 2. More refactoring
Here are the other functions you should define and call. For each one, come up with what you think the prototype should look like before highlighting my greyed-out sample prototype.
getUserInput: This function should print the prompt, get the user's input, and return that input. Prototype: string getUserInput();.
When you have defined this function, be sure to call it at the appropriate place in main, and be sure to use the return value (if applicable).
isVowel: This function should return true if a given character is a vowel and false otherwise. It shouldn't assume that the character is uppercase/lowercase. Prototype: bool isVowel(char c);.
In your main() function, you should see a call to each of these functions:
- getUserInput
- isVowel
- printReport
Step 1: Your program will now take its input from this file instead of from the user. Here's what you want to do:
- Remove the call to getUserInput(). You can also remove its definition and prototype if you like.
- Within main, follow the instructions in the I/O cheat sheet (available on Canvas) to read into your string variable from your new file words.txt, rather than from the console input.
- 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. You may refer to lecture_Feb19 available on Canvas to find the syntax for opening and reading a file.
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: Create 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!
To test your code on CLion, place the input files in the directory cmake-build-debug under the project directory Project-1. The input files, data1.txt and data2.txt, can be downloaded from below. Please note that you can make a total of 10 submission attempt.
Step 1: Your program will now take its input from this file instead of from the user. Here's what you want to do:
- Remove the call to getUserInput(). You can also remove its definition and prototype if you like.
- Within main, follow the instructions in the I/O cheat sheet (available on Canvas) to read into your string variable from your new file words.txt, rather than from the console input.
- 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. You may refer to lecture_Feb19 available on Canvas to find the syntax for opening and reading a file.
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: Create 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!
To test your code on CLion, place the input files in the directory cmake-build-debug under the project directory Project-1. The input files, data1.txt and data2.txt, can be downloaded from below. Please note that you can make a total of 10 submission attempt.
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!
main.cpp:
#include
using namespace std;
int main ()
{
/*Type your code here.*/
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started