Three Digits.

Hey guys :)

I am working on a C++ program in which the user needs to enter a 3 digit POSITIVE number. If the value entered is neither three digits nor positive, then an error message should be displayed. Once the value has been entered, the program must extract each digit and test to determine if the digit is odd or even. each digit needs to be displayed along with its status.

So for example, if I entered 230,
It should show as
"Your "ones" digit is 0 and it's an Even value
and so forth.

What I have so far is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;
int main()
{
	float integer;
	integer = 0;

	cout << "Please enter an integer value \n" << endl;

	if
		(integer > 99)

		cout << "The integer value you have entered is a three digit value \n" << endl;
	else

		cout << "ERROR : The integer value entered is either negative OR is not a three digit value \n" << endl;
"ones" digit: integer % 10
"tens" digit: (integer % 100) / 10
"hundreds" digit: (integer % 1000) / 100

1
2
3
4
5
6
7
8
if (/*name of digit*/ % 2 == 0)
{
     //say it is even
}
else
{
     //say it is odd
}
#include <iostream>
using namespace std;
int main()
{
float integer, ones, tens, hundreds;
integer = 0;
ones = integer % 10;
tens = (integer % 100) / 10;
hundreds = (integer % 1000) / 100;

cout << "Please enter an integer value \n" << endl;

if
(integer > 99)

cout << "The integer value you have entered is a three digit value \n" << endl;

else

cout << "ERROR : The integer value entered is either negative OR is not a three digit value \n" << endl;

if
(ones % 2 == 0)

cout << "The ones digit is even \n" << endl;
else

cout << "the ones digit is odd \n" << endl;

if
(tens % 2 == 0)

cout << "The tens digit is even \n" << endl;
else
cout << "The tens digit is odd \n" << endl;

if
(hundreds % 2 == 0)

cout << "The hundreds digit is even \n" <<endl;
else
cout << "The hundreds digit is odd \n" << endl;

return 0;

}

Is this what you were aiming for?
Umm yeah, but you have nowhere that it asks for input, and integer will be 0 every time. Also, you'll probably want them to be int values if you're keeping them as whole numbers

try this:

int integer, ones, tens, hundreds;
cout << "Please enter an integer value \n" << endl;
cin >> integer;
ones = integer % 10;
tens = (integer % 100) / 10;
hundreds = (integer % 1000) / 100;

Also, you want to add something in your first if then statement to make sure the user isnt going over 3 digits. Do something like if (integer > 99 && integer < 1000)

Other than that it looks fine
Although when I input this, it gives me an error

 
rror C2296: '%' : illegal, left operand has type 'float'
Last edited on
I ran it myself, and it worked fine. What does your code look like? Also, I'm not sure if its intentional or not, but it still shows whether or not the digits are odd or even regardless of the legality of the input, and your last 3 if statements only tell if they are even or odd, and not what the number actually is.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int ones, tens, hundreds, integer;
integer = 0;
ones = 0;
tens = 0;
hundreds = 0;
ones = integer % 10;
tens = (integer % 100) / 10;
hundreds = (integer % 1000) / 100;

cout << "Please enter an integer value \n" << endl;
cin >> integer;
if
(integer > 99)

cout << "The integer value you have entered is a three digit value \n" << endl;

else

cout << "ERROR : The integer value entered is either negative OR is not a three digit value \n" << endl;

if
(ones % 2 == 0)

cout << "The ones digit is even \n" << endl;
else

cout << "the ones digit is odd \n" << endl;

if
(tens % 2 == 0)

cout << "The tens digit is even \n" << endl;
else
cout << "The tens digit is odd \n" << endl;

if
(hundreds % 2 == 0)

cout << "The hundreds digit is even \n" <<endl;
else
cout << "The hundreds digit is odd \n" << endl;

return 0;

}


This is my code at the moment.

When I input 230,
I am suppose to get that 2 and 0 are even, while 3 is odd. I get that all three are even. Why so?
Just as a by-the-by, since your number is 3-digit, integer%1000 is not required for hundreds value. Just integer/100 will be sufficient...
I am also a bit curious about why the integer % 1000 and all is used? I haven't seen this before and was just wondering about it.
You mean to say you haven't heard of the (%) operand? What it does is evaluates the remainder of the division. For example, 10%7=3, 10%6=4, 10%5=4, 10%4=2 and so on....
Well yeah I've heard of the modulus, just wasn't sure of its application in the ones etc. But I got it. Can you help me figure out why it's giving 3 as an even integer?
You know, Caprico is right, for the hundreds, you dont really need the modulus. In any case, your problem is because by the time you initialize the ones tens and hundreds variables, integer is still equal to 0. Put the initializers after you get input. Also, you dont need to initialize them as 0 if your going to initialize them as something else right afterwards...
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


#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	int ones, tens, hundreds, integer;
	integer = 0;
	ones  = integer % 10;
	tens  = (integer % 100) / 10;
	hundreds  = (integer % 1000) / 100;

	cout << "Please enter an integer value \n" << endl;
	cin >> integer;
	if
		(integer > 99 && integer < 1000)

		cout << "The integer value you have entered is a three digit value \n" << endl;

	else

		cout << "ERROR : The integer value entered is either negative OR is not a three digit value \n" << endl;

	if
		(ones % 2 == 0)

		cout << "The ones digit is even \n" << endl;
	else

		cout << "the ones digit is odd \n" << endl;
	

	if 
	(tens % 2 == 0)

	cout << "The tens digit is even \n" << endl;
	else

	cout << "The tens digit is odd  \n" << endl;

	if 
	(hundreds  % 2 == 0)
	
	cout << "The hundreds digit is even \n" <<endl;

	else
	cout << "The hundreds digit is odd \n" << endl;
	

return 0;

}


This is what I got.
Put

1
2
3
ones  = integer % 10;
	tens  = (integer % 100) / 10;
	hundreds  = (integer % 1000) / 100;


after

1
2
cout << "Please enter an integer value \n" << endl;
	cin >> integer;


So that it looks like


1
2
3
4
5
cout << "Please enter an integer value \n" << endl;
	cin >> integer;
        ones  = integer % 10;
	tens  = (integer % 100) / 10;
	hundreds  = (integer % 1000) / 100;
I'll be damned :P Why does that work? What I mean by the question is that why does moving the ones, tens and hundreds give a different output?
Because c++ reads code like a list, from top to bottom. The way you had it, when you had it do ones = integer % 10, by that time, integer was 0 because you had initialized it as so. It calculated integer % 10, and assigned that value to ones, and ones is now that value, separate from the value of integer. Then you have the user input a number and assign it to integer. But that only changes integer, not ones tens and hundreds, because It wont go back up to see if anything has changed. So when you use ones tens and hundreds later on, your using its value as if integer was 0 instead of what the user inputted. But if you put ones tens and hundreds after cin >> input, it'll calculate the three variables using the new value of integer.
Thanks a million :) That really did help. :)
Topic archived. No new replies allowed.