Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++ , program needs properly indented) Write a program which performs the follo

ID: 3854152 • Letter: #

Question

(C++ , program needs properly indented)

Write a program which performs the following tasks in the order described below:

Asks the user to enter his/her name AND validates it (name should be single name)

Once the name is validated, then the program prompts the user to enter anything (s)he wants until user types -1.

The program must create a text file with the user’s name AND store EVERYTHING the user types in it.

The program must then read the contents of the file and count the number of even digits and the number of odd digits

The program should display the number of even digits if there are any; otherwise it should indicate that there are no even digits.

The program should display the number of odd digits if there are any; otherwise it should indicate that there are no odd digits.

It is VERY important that the program also include the following functions:

void validateUserName(which parameters? pass by reference or by value?);//validate user name

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

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 QUIZZES, MIDTERM AND FINAL EXAM

No global variables

No labels or go-to statements

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);//validate user name
void validateUserInput(string input);//validate user input
void createFile(string fileName,string input);
void checkEvenNumber(string );
void checkOddNumber(string );

void createFile(string fileName,string input){
  
//file.open(fileName.c_str());
ofstream OFileObject; // Create Object of Ofstream
   OFileObject.open (fileName.c_str()); // Opening a File or creating if not present
   OFileObject << input; // Writing data to file
   cout<<"Data has been written to file";
   OFileObject.close();
}

void validateUserName(string name){
  
if(name.length()==0){
  
cout<<" Name cannot be null value";
exit(0);
}
for(int i=0;i<name.length();i++){
  
if(!isalpha(name[i])){
cout<<" Invalid input for name!! ";
exit(0);
}
}
}

void validateUserInput(string input){
  
if(input.length()==0){
  
cout<<" User input cannot be null value";
exit(0);
}
for(int i=1;i<input.length();i++){
  
if(input[i-1]=='-'&&input[i]=='1'){
  
exit(0);
}
}
}

void checkEvenNumber(string word){
  
int l = 0;
  
for (int i = 0; i < word.size(); i++)
{
if(isdigit(word[i])){
  
if(word[i]%2==0){
  
l++;
}
}
}
if(l>0)
cout<<" The number of even numbers are "<<l;
else
cout<<" There are no even numbers";
}

void checkOddNumber(string word){
  
int l = 0;
  
for (int i = 0; i < word.size(); i++)
{
if(isdigit(word[i])){
  
if(word[i]%2!=0){
  
l++;
}
}
}
if(l>0)
cout<<" The number of odd numbers are "<<l;
else
cout<<" There are no odd numbers";
}


int main(){
  
string name, input, filename,words;
ifstream inputfile;
  
cout<<" Enter your name : ";
cin>>name;
  
validateUserName(name);
  
string ext = ".txt";
filename = name.append(ext);
  
cout<<" Enter -1 to quit, anything else to continue.. ";
cin>>input;
  
validateUserInput(input);//validate user input
createFile(filename,input);
  
checkEvenNumber(input);
  
checkOddNumber(input);
  
//Open the input file.
inputfile.open(filename.c_str());
  
cout<<" File Contents ";
//If the file succesfully opened, process it.
if (inputfile)
{
while (inputfile >> words){
  
cout<<words;
}
}
return 0;
}