why I didn't get my desire input?


Hello, I'm a beginner and I need to do a task about using functions. I already made the code as below, however, after it sums up all the number that I input, it just print out as "The sum of x is less than 100", even though I input the numbers as 90, 45 ,... can someone point out my logic mistakes? Thank you in advance !

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

 #include <iostream>

using namespace std;

void display(int x) {
    cout << "Welcome to the odd and even number counting program.\n";
}

void odd_or_even(int x) {
    if (x != 0) {
        //check if input value is odd or even number
        if ((x % 2) != 0) {
            cout << "You have just entered an ODD number.\n";
        }
        else
            cout << "You have just entered an EVEN number.\n";

    }
}

    int sum_of_num(int sum, int num) {
        sum += num;
        if (sum < 100)
            return 1;
        else
            return 2;
    }

    void display_sum(int sum) { // is there any problems with my logic here?
        if (sum > 100)
            cout << "The sum of x is more than 100.\n";
        else 
            cout << "The sum of x is less than 100.\n";

    }

   void thank_you() {
    cout << "Thank you for using this program.\n" ;
}

int main()
{
    int num;
    int sum = 0;
    int welcome = 0;

    display(welcome); //welcome messages
    cout << "This program is divided into 5 functions.\n";
    cout << "First part is to display welcome message using a function,\n " ;
    cout << "Second part is to check whether you have inserted an odd or even number using a function, \n" ;
    cout << "Third part is to check the limit of sum and return an integer to the main program.\n " ;
    cout << "Fourth part is to display the message of sum. \n" ;
    cout << "Last part is to display thank you message.\n " ;
    cout << "========================================== \n" ;

    do {
        cout << "Please, enter number (0 to exit): ";
        cin >> num;
        
        odd_or_even(num);
        sum_of_num(sum, num);
        }
     while (num != 0);
    //check if sum > 100 or < 100
    
   
   

    display_sum(sum);
    thank_you();


    cout << "=================================\n";
    

    


}
Last edited on
Look at http://www.cplusplus.com/doc/tutorial/functions/
It describes that function can take parameter by value or by reference.
1
2
// this function takes both parameters by value
int sum_of_num( int sum, int num );

Since the function simply copies the value of caller's argument, it will not modify the caller's argument.
If the function would use a reference to caller's argument, then the 'sum' in main() would be modified.


PS.
1
2
    // you call that function
    sum_of_num( sum, num );

The function returns an integer value. You don't use that value in any way.
Why does the function return a value? What is the purpose of that return value?
I use the return since the questions said to use the function like :

"Function name - int sum_of_x(int sum)- This function will get an integer
sum from main function and then check whether the summated integers is smaller
than 100 or larger than 100. It will return 1 if the sum value is larger than 100.
Otherwise, it will return 2."
sum_of_num() returns a value, but this returned value is not used by the calling code

 
sum_of_num(sum, num);


you need something like:

 
int is100 = sum_of_num(sum, num);


and then test is100 and output an appropriate message.

Note that in sum_of_num function, sum should be passed by ref as it's value is changed in the function.
Last edited on
wait, what do you mean by test is100?
1
2
3
4
5
6
int is100 = sum_of_num(sum, num);

if (is100 == 1)
   std::cout << "less than 100\n";
else
    std::cout << "100 or more\n";


the return value from sum_of_num is assigned to the variable is100 which is then tested in the if statement as the condition.

This is just an example. test and process as required.
Last edited on
I tried to do my functions something like this, but still, it print out sum of x < 100, even I entered 90, 34, 45,... whyy?? T_T

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int sum_of_num(int sum, int num) {
    sum += num;
    if (sum > 100)
        return 1;
    else
        return 2;
}

    void display_sum(int sum) {
        {
            if (sum == 1)
                cout << "The sum of x is more than 100.\n";
            else
                cout << "The sum of x is less than 100.\n";
        }

    }
sum needs to be passed by ref and not by value as per my previous post

1
2
3
4
5
6
7
8
int sum_of_num(int& sum, int num) {
    sum += num;

    if (sum > 100)
        return 1;
    else
        return 2;
}

Are you sure you're taking the return value of sum_of_num(), and passing that into display_sum()? Since you haven't shown us your modified main() function, there's no way for us to know.

EDIT: Also, you would help yourself and us if you used a sensible indentation style.
Last edited on
Oh, I'm sorry, this is my modified main function

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
#include <iostream>

using namespace std;

void display(int x) {
    cout << "Welcome to the odd and even number counting program.\n";
}

void odd_or_even(int x) {
    if (x != 0) {
        //check if input value is odd or even number
        if ((x % 2) != 0) {
            cout << "You have just entered an ODD number.\n";
        }
        else
            cout << "You have just entered an EVEN number.\n";

    }
}
int sum_of_num(int& sum, int num) {
    sum += num;
    if (sum > 100)
        return 1;
    else
        return 2;
}

    void display_sum(int sum) {
        {
            if (sum == 1)
                cout << "The sum of x is more than 100.\n";
            else
                cout << "The sum of x is less than 100.\n";
        }

    }

    

   void thank_you() {
    cout << "Thank you for using this program.\n" ;
}

int main()
{
    int num = 0;
    int sum = 0;
    int welcome = 0;

    display(welcome); //welcome messages
    cout << "This program is divided into 5 functions.\n";
    cout << "First part is to display welcome message using a function,\n " ;
    cout << "Second part is to check whether you have inserted an odd or even number using a function, \n" ;
    cout << "Third part is to check the limit of sum and return an integer to the main program.\n " ;
    cout << "Fourth part is to display the message of sum. \n" ;
    cout << "Last part is to display thank you message.\n " ;
    cout << "========================================== \n" ;

    

    do {
        cout << "Please, enter number (0 to exit): ";
        cin >> num;
        
        odd_or_even(num);
        
        sum_of_num (sum,num); // do you mean this part?
        display_sum(sum);

       
    } while (num != 0);
    
    thank_you();


    cout << "=================================\n";
   
}
Last edited on
wait, I just realized what do you mean by pass by ref, sorry for my silly mistakes T_T, I just started my programming last week, so there's a lot of thing that I confused. Btw thank you all for helping ^_^
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
#include <iostream>

using namespace std;

void display()
{
	cout << "Welcome to the odd and even number counting program.\n";
}

void odd_or_even(int x)
{
	//check if input value is odd or even number
	if ((x % 2) != 0) {
		cout << "You have just entered an ODD number.\n";
	} else
		cout << "You have just entered an EVEN number.\n";
}

int sum_of_num(int& sum, int num)
{
	sum += num;

	if (sum > 100)
		return 1;
	else
		return 2;
}

void display_sum(int sum)
{
	if (sum == 1)
		cout << "The sum of x is more than 100.\n";
	else
		cout << "The sum of x is less than 100.\n";
}

void thank_you() {
	cout << "Thank you for using this program.\n";
}

int main()
{
	int num = 0;
	int sum = 0;

	display(); //welcome messages

	cout << "This program is divided into 5 functions.\n";
	cout << "First part is to display welcome message using a function,\n ";
	cout << "Second part is to check whether you have inserted an odd or even number using a function, \n";
	cout << "Third part is to check the limit of sum and return an integer to the main program.\n ";
	cout << "Fourth part is to display the message of sum. \n";
	cout << "Last part is to display thank you message.\n ";
	cout << "========================================== \n";

	do {
		cout << "Please, enter number (0 to exit): ";
		cin >> num;

		if (num != 0) {
			odd_or_even(num);

			int is100 = sum_of_num(sum, num);

			display_sum(is100);
			// or just
			//display_sum(sum_of_num(sum, num));
		}
	} while (num != 0);

	thank_you();

	cout << "=================================\n";
}

Last edited on
You don't need to worry about passing by reference for now. You just need to actually use the number that sum_of_num() returns.
Topic archived. No new replies allowed.