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

Visual studios C++ Restrictions : 1. No global variables 2. No labels or go to s

ID: 3875326 • Letter: V

Question

Visual studios C++

Restrictions :

1. No global variables

2. No labels or go to statements

3. No infinite loops

for(;;)

while(1)

while(true)

do{//code}while(1);

4. Break statements cannot be used to exit loops

5. No vectors

6. Try hard not to use strings. Avoid if you can.

Design a program that

Asks the user to enter his/her name AND validates it

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

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

( sidenote : how do you store information from conditional if else statements? )

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!

Explanation / Answer

#include <iostream>

using namespace std;

void validateUserName(){
int flag = 1;
string name;
// While loop with condition
while(flag == 1){
cout << "Enter the name: ";
getline(cin, name);
flag = 0;
// Finite for loop which does not contains any break statements
// Iterates for all the characters in the string
for(int i = 0; i < name.length(); i++){
if(name[i] == ' '){
cout << "Given name contains space. ";
flag = 1;
}
else if(name[i] >= '0' && name[i] <= '9'){
cout << "Given name contains numbers. ";
flag = 1;
}
}
}
cout << "You entered valid name which is " << name;
}
int main()
{
validateUserName();
return 0;
}