Hye, I am a beginner and I just started my C++ course in my university. I'm currently working on my functions' homework. It was an easy task, however, after I use the functions for the forth times, it didnt working anymore. Can someone help me to find my mistakes? thanks a lot in advance.
#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";
}
void display_sum(int sum) {
if (sum > 100)
cout << "The sum of x is more than 100.\n";
else
cout << "The sum of x is less than 100.\n";
}
int sum_of_x(int sum) { // this line didnt working anymore
sum += x;
if (sum < 100)
return 1;
else
return 2;
}
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_i (sum)
}
} while (i != 0);
//check if sum > 100 or < 100
display_sum(num); // i got error here where they said I didnt declare the identifier
> it said that it didnt recognize the functions, but whyy??
Because the previous function has unbalanced { }
In function 'void odd_or_even(int)':
20:27: error: a function-definition is not allowed here before '{' token
28:23: error: a function-definition is not allowed here before '{' token
36:18: error: a function-definition is not allowed here before '{' token
41:1: error: a function-definition is not allowed here before '{' token
76:1: error: expected '}' at end of input
Always read and fix the first error message, because that can easily cause a cascade of nonsense if the compiler fails to guess correctly what you might have meant.