Question: Can somebody help me fix my code? it doens't work Its suppose to .. Asks the user to enter his/her name AND validates it Once
Can somebody help me fix my code? it doens't work Its suppose to ..
Asks the user to enter his/her name AND validates it
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 users 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.
If anyone can help fix mine please reply, This is for C++ #include
#include
#include
using namespace std;
void validateUserName(string inputname) {
int flag = 1;
while (flag == 1) {
cout << "Enter the name your name ";
getline(cin, inputname);
flag = 0;
for (int i = 0; i < inputname.length(); i++) {
if (inputname[0] >= 'a' && inputname[0] <= 'z' ||
inputname[0] >= 'A' && inputname[0] <= 'Z') {
cout << "Good Job ";
system("pause");
return;
}
else
cout << "Letters Only ";
flag = 1;
}
}
}
void validateUsertext(string filetext) {
int flag = 1;
while (flag == 1) {
cout << "Enter Anything you want, Press -1 To stop ";
getline(cin, filetext);
flag = 0;
}
while ((filetext[0] != '-') && (filetext[1] != '1')) {
cout << "Enter Anything you want, Press -1 To stop ";
getline(cin, filetext);
flag = 0;
}
return;
}
void checkEvenDigit(int buffer, int &count_even) {
if (buffer % 2 == 0) //check the modulo of the number if even remainder is zero
count_even++;
}
void checkOddDigit(int buffer, int &count_odd) {
if (buffer % 2 == 1) //check the modulo of the number if odd remainder is one
count_odd++;
}
void createFile(string totalfile) {
ofstream ptr_write; //creating a file pointer
totalfile = totalfile + ".txt"; //simply concatinating .txt with the name
ptr_write.open(totalfile.c_str()); //creating a file with the name specified
ptr_write.close();
}
void writeDataToFile(string totalfile, int input, int size) {
ofstream ptr_file; //creating a file pointer for writing
totalfile = totalfile + ".txt"; //simply concatinating .txt with the name
ptr_file.open(totalfile.c_str(), fstream::app); //opening the file for writing data in append mode
ptr_file << input << endl; //writing date into the file
if (input == -1)
ptr_file.close();
//*note -1 is also writing to make it easier while reading*//
}
void readDataFromFile(string totalfile) {
int input;
ifstream ptr_file; //creating a file pointer for reading
totalfile = totalfile + ".txt"; //simply concatinating .txt with the name
ptr_file.open(totalfile.c_str());
if (ptr_file.is_open()) { //if file can be open or exist
cout << endl << "Displaying from a file" << " " << totalfile << endl;
ptr_file >> input; //getting data from the file
while (input != -1) {
cout << input << ", ";
ptr_file >> input; //getting data from the file
}
cout << endl;
}
else { //file cannot be open
cout << "cannot open file!" << endl;
}
ptr_file.close();
}
void displayResults(string totalfile, int count_even, int count_odd) {
totalfile = totalfile + ".txt"; //simply concatinating .txt with the name
cout << "In the file name " << totalfile << endl;
cout << "The number of even are " << count_even << endl;
cout << "The number of odd are " << count_odd << endl;
}
int main() {
int validate = 0, buffer = 0, count_even = 0, count_odd = 0, size = 0; //declaration of variables
string filetext, filename;
validateUserName(filename);
validateUsertext(filetext);
string totalfile = (filetext + filename);
while (buffer != (-1)) {
size++;
cin >> buffer;
writeDataToFile(totalfile, buffer, size);
checkEvenDigit(buffer, count_even);
checkOddDigit(buffer, count_odd);
}
readDataFromFile(totalfile);
displayResults(totalfile, count_even, count_odd);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
