This is the problem 1. Asks the user to enter a positive integer greater than 0
ID: 662708 • Letter: T
Question
This is the problem
1. Asks the user to enter a positive integer greater than 0
2. Validates that the entry is a positive integer
3. Outputs the digits in reverse order with a space separating the digits
4. Outputs the even digits not in reverse order with a space separating the digits (consider zero to be even)
5. Outputs the odd digits not in reverse order with a space separating the digits
6. Allows user is to repeat/continue the program as many times as he/she wants
7. Keeps a record in a txt file named outDataFile.txt with the history of all numbers entered and the associated results, in the following format:
the original number is 1023
the number reversed 3 2 0 1
the even digits are 0 2
the odd digits are 1 3
-----------------
the original number is 102030
the number reversed 0 3 0 2 0 1
the even digits are 0 2 0 0
the odd digits are 1 3
---------------------------------------------------------------------------------------------------------------------------------
and those are my answer but cannot excute the program
can someone help me to fix it please
#include <iostream>
#include <fstream> // use fstream for file access
#include <string> // string
using namespace std;
// Four Void function //
bool validate(string str1); //return true or false
void reverse (string str1, ofstream dumfile);
void even (string str1, ofstream dumfile);
void odd (string str1, ofstream dumfile);
int main()
{
ofstream myfile;
myfile.open("DataFile.txt");
char again;
string input_string;
do
{
do {
// get the integer number from user //
cout << " Please Enter a Positive integer greater than 0: ";
cin >> input_string;
cout << endl;
} while(validate(input_string)); // ask again
cout << " the original number is: " << input_string << endl;
myfile << " the original number is: " << input_string << endl;
// do the reverse function, even and odd fnction //
reverse(input_string, myfile);
odd(input_string, myfile);
even(input_string, myfile);
// separate next part with this part
cout<< " --------------------" << endl;
myfile << " --------------------" << endl;
// ask user to reprat //
cout << " Do you want to do it again? <y/n> " << endl;
cin >> again;
}while(again == 'Y' || again == 'y');
myfile.close();
return 0;
}
// function to check user's input
bool validate(string str1)
{
// *****************************
// *check the idea from C++.com*
// *****************************
string::size_type sz; //alias of size_t
int num = stoi(str1,&sz);
if(num < 0) // this stop Validates that the entry is a postive integer
{
return true;
}
return false;
}
// function that output digits to screen and txt file
void reverse(string str1, ofstream dumfile)
{
cout << " the number reversed ";
dumfile << " the number reversed ";
// next step reverse order with a space //
// use for loop //
for (int x = str1.length()- 1; x >=0; x--) // memory store position till 0 //
{
cout << str1[x] << " "; //the space
dumfile << str1[x] << " "; // the space
}
cout <<endl;
dumfile <<endl;
}
// Even function
void even (string str1, ofstream dumfile)
{
cout << " the even digits are ";
dumfile << " the even digits are ";
for (int x=0; x< str1.length(); x++) //same with last function so
{ if((str1[x]-'0')%2 == 0) // i just do copy and paste
{
cout << str1[x] << " ";
dumfile << str1[x] << " ";
}
}
cout << endl;
dumfile << endl;
}
// odd function
void odd (string str1, ofstream dumfile)
{
cout << " the odd digits are ";
dumfile << " the odd digits are ";
// same with last function {copy and paste}
for (int x = 0; x < str1.length(); x++)
{
if ((str1[x]-'0')%2 !=0)
{
cout << str1[x] << " ";
dumfile << str1[x] << " ";
}
}
cout << endl;
dumfile << endl;
}
Explanation / Answer
Hi
Plese find below thw solution. It is copiling and running fine now. Tested.
#include <iostream>
#include <fstream> // use fstream for file access
#include <string>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdlib.h> // string
using namespace std;
// Four Void function //
bool validate(string str1); //return true or false
void reverse (string str1);
void even (string str1);
void odd (string str1);
ofstream myfile;
int main()
{
char again;
string input_string;
myfile.open("DataFile.txt");
do
{
do {
// get the integer number from user //
cout << " Please Enter a Positive integer greater than 0: ";
cin >> input_string;
cout << endl;
} while(validate(input_string)); // ask again
cout << " the original number is: " << input_string << endl;
myfile << " the original number is: " << input_string << endl;
// do the reverse function, even and odd fnction //
reverse(input_string);
odd(input_string);
even(input_string);
// separate next part with this part
cout<< " --------------------" << endl;
myfile << " --------------------" << endl;
// ask user to reprat //
cout << " Do you want to do it again? <y/n> " << endl;
cin >> again;
}while(again == 'Y' || again == 'y');
myfile.close();
return 0;
}
// function to check user's input
bool validate(string str1)
{
int num = atoi(str1.c_str());
if(num < 0) // this stop Validates that the entry is a postive integer
{
return true;
}
return false;
}
// function that output digits to screen and txt file
void reverse(string str1)
{
cout << " the number reversed ";
myfile << " the number reversed ";
// next step reverse order with a space //
// use for loop //
for (int x = str1.length()- 1; x >=0; x--) // memory store position till 0 //
{
cout << str1[x] << " "; //the space
myfile << str1[x] << " "; // the space
}
cout <<endl;
myfile <<endl;
}
// Even function
void even (string str1)
{
cout << " the even digits are ";
myfile << " the even digits are ";
for (int x=0; x< str1.length(); x++) //same with last function so
{ if((str1[x]-'0')%2 == 0) // i just do copy and paste
{
cout << str1[x] << " ";
myfile << str1[x] << " ";
}
}
cout << endl;
myfile << endl;
}
// odd function
void odd (string str1)
{
cout << " the odd digits are ";
myfile << " the odd digits are ";
// same with last function {copy and paste}
for (int x = 0; x < str1.length(); x++)
{
if ((str1[x]-'0')%2 !=0)
{
cout << str1[x] << " ";
myfile << str1[x] << " ";
}
}
cout << endl;
myfile << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.