C++ Write a program which: 1. Asks the user to enter his/her name AND validates
ID: 3863133 • Letter: C
Question
C++
Write a program which:
1. Asks the user to enter his/her name AND validates it
2. Once the name is validated, then the program prompts the user to enter anything (s)he wants until user types -1.
3. The program must create a text file with the user’s name AND store EVERYTHING the user types in it.
4. The program must then read the contents of the file and count the number of even digits and the number of odd digits
5. The program should display the number of even digits if there are any; otherwise it should indicate that there are no even digits.
6. The program should display the number of odd digits if there are any; otherwise it should indicate that there are no odd digits.
7. The program MUST have the following functions:
void validateUserName(which parameters? pass by reference or by value?);//validate user name
void validateUserInput(which parameters? pass by reference or by value?);//validate user input
void checkEvenDigit(which parameters? pass by reference or by value?);//check for the presence of even digits
void checkOddDigit(which parameters? pass by reference or by value?);//check for the presence of odd digits
void createFile(which parameters? pass by reference or by value?);//create userFile
void writeDataToFile(which parameters? pass by reference or by value?);//write to the file
void readDataFromFile(which parameters? pass by reference or by value?);//read from the file
void displayResults(which parameters? pass by reference or by value?);//display results
8. The main() function should consist mostly of local variable declarations and a series of function calls. One of the objectives of the quiz is to get you to “modularize” your programs using functions—hence main() must necessarily be very short!
GENERAL RESTRICTIONS FOR ALL ASSIGNMENTS
No global variables
No infinite loops, examples include:
for(;;;)
while(1)
while(true)
do{//code}while(1);
No break statements to exit loops
Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
void validateUserName(string name, bool& errorFlag);//validate user name
void validateUserInput(string &input);//validate user input
void checkEvenDigit(string filename, int &count);//check for the presence of even digits
void checkOddDigit(string filename, int& count);//check for the presence of odd digits
void createFile(string filename);//create userFile
void writeDataToFile(string filename, string input);//write to the file
void readDataFromFile(string filename, string& input);//read from the file
void displayResults(int evenCount , int oddCount);//display results
int main()
{
string filename ,input ;
ifstream file;
bool errorFlag = false;
int evenCount =0, oddCount = 0;
cout<< "Enter your name : ";
cin>> filename;
validateUserName(filename,errorFlag);
if(errorFlag){
return -1;
}
validateUserInput(input);
filename = filename + ".txt";
createFile(filename);
writeDataToFile(filename,input);
checkEvenDigit(filename,evenCount);
checkOddDigit(filename,oddCount);
displayResults(evenCount,oddCount);
return 0;
}
void displayResults(int evenCount , int oddCount)
{
if(evenCount){
cout<< "Number of even digits : <" << evenCount <<">"<<endl;
}
else
cout<< "No Even digits found" <<endl;
if(oddCount){
cout<< "Number of odd digits : <" << oddCount <<">"<<endl;
}
else
cout<< "No Odd digits found" <<endl;
}
void checkEvenDigit(string filename, int &count)
{
string input;
int i =0, digit ;
readDataFromFile(filename,input);
while(i < input.length())
{
if( isdigit(input[i])){
digit = input[i] - 48;
if(digit % 2 == 0){
count++;
}
}
i++;
}
}
void checkOddDigit(string filename, int &count)
{
string input;
int i =0, digit ;
readDataFromFile(filename,input);
while(i < input.length())
{
if( isdigit(input[i])){
digit = input[i] - 48;
if(digit % 2 != 0){
count++;
}
}
i++;
}
}
void readDataFromFile(string filename, string &input){
ifstream file(filename.c_str());
string val;
input = "";
while(file >> val){
input = input + val;
}
file.close();
}
void writeDataToFile(string filename, string input)
{
ofstream file(filename.c_str());
file << input;
file.close();
}
void createFile(string filename)
{
ofstream file;
file.open(filename.c_str());
file.close();
}
void validateUserName(string name, bool& errorFlag){
int i=0;
while( i < name.length()){
if(!isalpha(name[i])){
errorFlag = true;
cout<< "Invalid name : " <<name <<endl;
return;
}
i++;
}
}
void validateUserInput(string& input){
string val = "";
char ch = ' ';
cout<< "Please enter the inputs for the file : " <<endl;
cin>>val;
while(val.compare("-1")){
ch = getchar();
input = input + val + ch;
cin >> val;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.