Trying to make it display for the output

My program right now doesn't display there are no even or there are no odds.
I tried add else and it wouldn't work. What do i have to do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// 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;
}
I cannot reproduce the error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
using namespace 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


It looks it returns stuff.
Sorry this is the whole code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <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); // 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
	{
		return true;
	}
	return false;
}

// 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;
}
From your comments it looks like you are trying to decide the oddity of every digit, so why don't you use a static_cast to treat your chars as ints?
How can I use static_cast in this case?
@wckedsck

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.
@TheIdeasMan but where is he starting another topic? *confused*

What's wrong with this logic ((str1[x] - '0') % 2 == 0)? Seems to be fine to me..
Topic archived. No new replies allowed.