So I coded this trying to only allow integers to be inputted. If characters are inputted it still works. Really confused on how to decline the characters and only accept integers(whole numbers). Also trying to decline the user if character was inputted to see a message like "Please input a number".
#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);
void even(string str1);
void odd(string str1);
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;
}
Hey figured it out thanks! Also I am now trying to figure out how to make it show if there are no even or odd number to show the display as.
"There are no even digits" and "There are no odd digits".
I am using else, but it won't work.
What am I doing wrong here?
// 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)
{
cout << str1[x] << " ";
myfile << str1[x] << " ";
{
else ((str1[x] - '0' % 2 != 0))
cout << "there is no even number";
}
}
}
cout << endl;
myfile << endl;
}
@JLBorges
Hey thanks, but there are things I don't like about it. I want the output to be spaced out. And also need to show there are no odd or even. When there are none.
Output:
12345
The Original Number 1234
The number reversed 4 3 2 1
The even number 2 4
The odd number 1 3
y/Y to continue, any else to exit.
I was trying to get you think a bit about what you were doing - it was aimed at you coming up with the notion of some sort of count variable. JLBorges has these in the code, it's up to you to think about how to do your extra requirements.