#include <iostream>
usingnamespace std;
int main()
{
int n = 0; // initalize n
int number=10; // Init number;
cout << "Please enter a number "; //Write something so it doesn't look like the thing hung up
cin >> n; // get user input
double test = n/2;
while number>9
{
if (test - n = 0)
{
sum=even numbers
}
else {sum=odd numbers}
number=sum
}
if number <10
{
cout number
}
function for even numbers;
return number
function for odd numbers;
return number
Please forgive my stupidity...but I'm not a computer science student and have only attended 3 lectures and feel totally clueless.
I did what you said but the sample run is still wrong...
#include <iostream>
int handle_even(int num)
{
int sum = 0;
do {
sum += num % 10;
} while ( (num /= 10) != 0 );
return sum;
}
int handle_odd(int p)
{
return p/2;
}
int main()
{
int number;
std::cout << "Please enter a number: ";
std::cin >> number;
while (number>9) {
if (number % 2 == 0)
number = handle_even(number);
else
number = handle_odd(number);
std::cout << number << '\n';
}
}
Why so rounabout way? Why so many unused variables in main? Why do you ever need different n and number variables?
Why your functions take arguments by reference? Why do you copy those arguments in separate variable? Why do you have declaration and initialization on separate lines where it could be done on one line?
But actually this is only part of my question...
I have to put this function in a program that provides the user with four options described by respective functions. So I got this option's function done. But how can I put it back into the program when itself contains two functions already?