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 !
#include <iostream>
usingnamespace 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;
elsereturn 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";
}
// 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."
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;
elsereturn 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";
}
}
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.
#include <iostream>
usingnamespace 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;
elsereturn 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";
}
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 ^_^
#include <iostream>
usingnamespace 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;
elsereturn 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";
}