// 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;
}
#include <iostream>
#include <string>
usingnamespace std;
// Even function
void even(string str1)
{
cout << " 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] << " ";
}else
{
cerr << str1[x] << endl;
}
}
cout << endl;
}
// odd function
void odd(string str1)
{
cout << " 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] << " ";
}else
{
cerr << str1[x] << endl;
}
}
cout << endl;
}
int main(){
string s("a random string");
odd(s);
even(s);
return 0;
}
compiles and returns
1 2 3
tmp$ ./a.out 2> tmp.txt
the odd digits are a a o m s i g
the even digits are r n d t r n
#include <iostream>
#include <fstream> //For file access
#include <string>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdlib.h> // string
usingnamespace std;
// Four Void function //
bool validate(string str1); // this will help to return true or false
void reverse(string str1); //this is to figure out the reverse of the number
void even(string str1); // this is to figure out the even number
void odd(string str1); // this is to figure out the odd number
ofstream myfile;
int main()
{
char again;
string input_string;
myfile.open("DataFile.txt");
do
{
do {
// Integer number from user
cout << " Please Enter a Positive integer greater than 0:\n";
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;
// Reverse function, even or odd
reverse(input_string);
odd(input_string);
even(input_string);
// This seperates the next part
cout << " --------------------" << endl;
myfile << " --------------------" << endl;
// this uses to ask user to repeat again
cout << " Do you want to do it again? <y/n> " << endl;
cin >> again;
} while (again == 'Y' || again == 'y');
myfile.close();
return 0;
}
// Checking user input
bool validate(string str1)
{
int num = atoi(str1.c_str());
if (num <= 0) // Validating user input is positive
{
returntrue;
}
returnfalse;
}
// Output digits to screen and txt file
void reverse(string str1)
{
cout << " the number reversed ";
myfile << " the number reversed ";
// Reverse order with a space
// Using the loop
for (int x = str1.length() - 1; x >= 0; x--)
{
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;
}
Please don't start another topic for the same code, it's ultimately a time waster for those who reply. Decide which topic you are going to go with, inform everyone of that choice, direct them there with a link to that topic.
@ericM
That won't really work, a char is already a small int and the ASCII code does not determine if the digit is odd or even. Subtracting the char turns it into a number we can use.
How can I use static_cast in this case?
Even though this suggestion isn't right, you need to put more effort in, read the documentation, look at examples, try it for yourself. We can help, but it doesn't mean we are going to answer every little trivial question.