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>
usingnamespace 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;
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)
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.
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...
#include <iostream>
#include <cmath>
usingnamespace 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;
}
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.