Functions with Parameters

I'm a beginner, and i've been trying to make a program that outputs someone's age in years and months, and so far i have this. However, i have no idea why it does not work. Can someone help me?
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
#include <iostream>

void bad (int x, int y)
{
    if (x == 1 && y == 1) {
        std::cout << "Ango is " << x << " Year old and " << y << "month old!" << endl;
    }
    if (x > 1 && y > 1) {
        std::cout << "Ango is " << x << " Years old and " << y << "months old!" << endl;
    }
    if (x == 1 && y > 1) {
        std::cout << "Ango is one year old and" << y << "months old!" << endl;
    }
    if (x > 1 && y == 1) {
        std::cout << "Ango is" << x << "years old and one month old!" << endl;
    }
}

int main ()
{
    cin >> x >> endl;
    cin >> y >> endl;
    bad (x, y)
    return 0;
}




Last edited on
cin and endl need std:: like you've done with the cout.

In the main function:

x and y have not been declared as variables
cin does not take endl
bad (x,y) needs closing ;
You are just missing some syntactical things. Including the std namespace, a couple missing semi colons, and misuse of the endl feature.

Try this:
#include <iostream>
using namespace std;

void bad(int x, int y)
{
if (x == 1 && y == 1) {
std::cout << "Ango is " << x << " Year old and " << y << " month old!" << endl;
}
if (x > 1 && y > 1) {
std::cout << "Ango is " << x << " Years old and " << y << " months old!" << endl;
}
if (x == 1 && y > 1) {
std::cout << "Ango is one year old and" << y << " months old!" << endl;
}
if (x > 1 && y == 1) {
std::cout << "Ango is" << x << " years old and one month old!" << endl;
}
}

int main()
{
int x, y;
cin >> x;
cin >> y;
bad(x, y);
system("pause");
return 0;
}
Good Luck on your programming endeavors!
Maybe think about changing the text output to remove the repeated "old"?

Ango is 3 years old and 6 months old

vs.

Ango is 3 years and 6 months old
Topic archived. No new replies allowed.