Why isn't m function working?

My question is..

Program will have user enter an integer between 1 and 100, anything else will cause the loop and program to quit. This integer is first tested to see if it is an odd or even number; here an appropriate message is displayed, displayed only in the main function. Then the sum of all the numbers up to and including the entered number is calculated and displayed. So if the user enters the number 4, the sum of numbers from 1 to 4 would yield 10. This program needs to call two separate functions, one to determine if number is odd or even, and the other to calculate the sum of the numbers. Both functions will have one value or input parameter and both should return an “int” back, which is the answer the main will deal with.

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
#include <iostream>
#include <iomanip>
using namespace std;

void EvenOdd (int Integer);
void SumDec (int Integer, int Sum);

int main()
{
    int Integer, Sum = 0;
    
    do
    {
        cout << "Enter an integer that is between 1 and 100 --> ";
            cin >> Integer;
if(Integer >= 1 && Integer <= 100)
    EvenOdd(Integer);
    SumDec (Integer, Sum);
        
    }
    while (Integer >= 1 && Integer <= 100);
}
void EvenOdd (int Integer)
{
    if((Integer % 2) != 0)
        cout << Integer << " is odd.";
    if ((Integer % 2) == 0)
        cout << Integer << " is even.";
}
void SumDec (int Integer, int Sum)
{
    Sum = 0;
    while ( Integer > 0)
    {
        Sum = Integer + Sum;
        --Integer;
    }
    cout << "The sum of all the numbers up to and including " << Integer
    << " is " << Sum << endl;
}


i am not getting any errors found.
You forgot to tell what the problem is.
is this how you enter functions?

1
2
3
if(Integer >= 1 && Integer <= 100)
    EvenOdd(Integer);
    SumDec (Integer, Sum);


i am unable to get to the even odd function and the sumdec function.
i enter an integer and the program stores the value but nothing more
if() only works on the next statement, if you want more than one statement you need to wrap them into a compound statement with braces like this...
1
2
3
4
5
if(Integer >= 1 && Integer <= 100)
{
    EvenOdd(Integer);
    SumDec (Integer, Sum);
}
thread 1: breakpoint 2.1


is what i am getting on

EvenOdd(Integer);

my output has only

Enter an integer that is between 1 and 100 --> 4
(lldb) 
Last edited on
your code works for me, i get this output.
1
2
Enter an integer that is between 1 and 100 --> 5
5 is odd.The sum of all the numbers up to and including 0 is 15


the problem with "up to and including 0..." is that SumDec() keeps decrementing Integer so by the time it prints the value is zero.

I don't see your breakpoint problem at all. copy and paste the error into this thread please so we can see it.

also you can get rid of the second parameter to SumDec() because 1) its useless, 2) your spec says only 1 parameter. like this...
1
2
3
void SumDec (int Integer)
{
    int Sum = 0;
i am using Xcode and am not sure why my program is not running all the way through. i think i should add another int so that integer is unchanged and make the new int change with the dec.
Last edited on
I now have this and unsure why it is not running. It says "No Issues"

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
#include <iostream>
#include <iomanip>
using namespace std;

void EvenOdd (int Integer);
void SumDec (int Integer);

int main()
{
    int Integer;
    do
    {
        cout << "Enter an integer that is between 1 and 100 --> ";
            cin >> Integer;
        
        if(Integer >= 1 && Integer <= 100)
        {
            EvenOdd(Integer);
            SumDec (Integer);
        }
    }
    while (Integer >= 1 && Integer <= 100);
}
void EvenOdd (int Integer)
{
    if((Integer % 2) != 0)
        cout << Integer << " is odd.\n";
    if ((Integer % 2) == 0)
        cout << Integer << " is even.\n";
}
void SumDec (int Integer)
{
    int NewInteger, Sum;
    NewInteger = Integer;
    Sum = 0;
    while ( Integer > 0)
    {
        Sum = NewInteger + Sum;
        NewInteger = --Integer;
    }
    cout << "The sum of all the numbers up to and including " << Integer
    << " is " << Sum << endl;
}
Last edited on
closed account (18hRX9L8)
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
#include <iostream>

void EvenOdd (int Integer) {
    if ((Integer % 2) != 0) {
        std::cout << Integer << " is odd.\n";
    } else {
        std::cout << Integer << " is even.\n";
    }
}

void SumDec(int Integer) {
    int Sum = 0, OldInteger = Integer;
    while (Integer > 0) {
        Sum += Integer;
        Integer--;
    }
    std::cout << "The sum of all the numbers up to and including " << OldInteger << " is " << Sum << std::endl;
}

int main(void) {
    int Integer;
    while (true) {
        std::cout << "Enter an integer that is between 1 and 100 --> ";
        std::cin >> Integer;
        std::cin.ignore();

        if (Integer >= 1 && Integer <= 100) {
            EvenOdd(Integer);
            SumDec(Integer);
            return 0;
        }
        std::cout << "Try again.\n";
    }
}


This works .
Last edited on
Topic archived. No new replies allowed.