How to insert if into a void function?

Hello guys!!

I'm at the point where i need to insert an 'if' statement to my exercise!


The code works until i reach the point with the if statements
This is my code:


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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>

using namespace std;

void addNumbers(int m, int d, int y, int c)
{
  cout << "The day of the week is: " << (d + m + y + (y/4) + c)%7 << endl;
}


int main()
{

//example: 3rd January 1350 is a sunday
    
    int day;
    int month;
    int year;
    int century;


    //day of the month: 3
    cout << "This program will tell you the day of the inputs:" << endl;
    cout << "What is the day of the month?: "<< endl;
    cin >> day;


    //month: 0
    cout << "What is the number of the month from the months table?: "<< endl;
    cout << "Jan, Oct = 0"<< endl;
    cout << "May = 1"<< endl;
    cout << "Aug = 2"<< endl;
    cout << "Feb, Mar, Nov = 3"<< endl;
    cout << "Jun = 4"<< endl;
    cout << "Sep, Dec = 5"<< endl;
    cout << "Apr, Jul = 6"<< endl;
    cout << "Exceptions: If it is a leap year, Feb = 2 and Jan = 6"<< endl;
    cin >> month;

    //last 2 digits: 50
    cout << "What is the last 2 digits in the year?: "<< endl;
    cin >> year;

    //century: 6
    cout << "What is the century's number in the 100s of years table?: "<< endl;
    cout << "14th century: 6" << endl;
    cout << "15th century: 5" << endl;
    cout << "16th century: 4" << endl;
    cout << "17th century: 3" << endl;
    cout << "18th century: 2" << endl;
    cout << "19th century: 1" << endl;
    cout << "20th century: 0" << endl;
    cin >> century;

    addNumbers(day,month,year,century);
    
     if(addNumbers(day,month,year,century)==0)
    {
        cout << "The day of the week is: saturday";
    }
    if(addNumbers(day,month,year,century)==1)
    {
        cout << "The day of the week is: sunday";
    }
    if(addNumbers(day,month,year,century)==2)
    {
        cout << "The day of the week is: monday";
    }
    if(addNumbers(day,month,year,century)==3)
    {
        cout << "The day of the week is: tuesday";
    }
    if(addNumbers(day,month,year,century)==4)
    {
        cout << "The day of the week is: wednesday";
    }
    if(addNumbers(day,month,year,century)==5)
    {
        cout << "The day of the week is: thursday";
    }
    if(addNumbers(day,month,year,century)==6)
    {
        cout << "The day of the week is: friday";
    }







    return 0;
}
Last edited on
You can't. Void functions don't return anything. Therefore - addNumbers(day,month,year,century)==1 Can't work. It looks like you want an integer function that returns the result.

1
2
3
4
int addNumbers(int m, int d, int y, int c)
{
  return (d + m + y + (y/4) + c)%7;
}
Thank you TarikNeaj!

The code ran without errors and looked like this:
Btw: If the code could be written otherwise/more efficient, please tell 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
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
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>

using namespace std;

int addNumbers(int m, int d, int y, int c)
{
  int result = (d + m + y + (y/4) + c)%7;
   if(result==0)
    {
        cout << "The day of the week is: saturday";
    }
    if(result==1)
    {
        cout << "The day of the week is: sunday";
    }
    if(result==2)
    {
        cout << "The day of the week is: monday";
    }
    if(result==3)
    {
        cout << "The day of the week is: tuesday";
    }
    if(result==4)
    {
        cout << "The day of the week is: wednesday";
    }
    if(result==5)
    {
        cout << "The day of the week is: thursday";
    }
    if(result==6)
    {
        cout << "The day of the week is: friday";
    }

}


int main()
{

//example: 3rd January 1350 is a sunday

    int day;
    int month;
    int year;
    int century;

    // day of the month: 3
    cout << "This program will tell you the day of the inputs:" << endl;
    cout << "What is the day of the month?: ";
    cin >> day;
    cout << endl;


    //month: 0
    cout << "Jan, Oct = 0"<< endl;
    cout << "May = 1"<< endl;
    cout << "Aug = 2"<< endl;
    cout << "Feb, Mar, Nov = 3"<< endl;
    cout << "Jun = 4"<< endl;
    cout << "Sep, Dec = 5"<< endl;
    cout << "Apr, Jul = 6"<< endl;
    cout << "Exceptions: If it is a leap year, Feb = 2 and Jan = 6"<< endl << endl;
    cout << "What is the number of the month from the months table?: ";
    cin >> month;
    cout << endl;

    //last 2 digits: 50
    cout << "What is the last 2 digits in the year?: ";
    cin >> year;
    cout << endl;

    //century: 6
    cout << "14th century: 6" << endl;
    cout << "15th century: 5" << endl;
    cout << "16th century: 4" << endl;
    cout << "17th century: 3" << endl;
    cout << "18th century: 2" << endl;
    cout << "19th century: 1" << endl;
    cout << "20th century: 0" << endl;
    cout << "What is the century's number in the 100s of years table?: ";
    cin >> century;
    cout << endl;

    addNumbers(day,month,year,century);


    return 0;
}


Output: 1 = sunday
Last edited on
Btw: If the code could be written otherwise/more efficient, please tell me.


I suggest using a switch statement. To make this code below look 250% cleaner.
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
   if(result==0)
    {
        cout << "The day of the week is: saturday";
    }
    if(result==1)
    {
        cout << "The day of the week is: sunday";
    }
    if(result==2)
    {
        cout << "The day of the week is: monday";
    }
    if(result==3)
    {
        cout << "The day of the week is: tuesday";
    }
    if(result==4)
    {
        cout << "The day of the week is: wednesday";
    }
    if(result==5)
    {
        cout << "The day of the week is: thursday";
    }
    if(result==6)
    {
        cout << "The day of the week is: friday";
    }
Oh yeah SakurasouBusters i see, definitely smarter way of approaching that problem!
Thanks man!

It will then look like this?
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;

int addNumbers(int m, int d, int y, int c)
{
  int result = (d + m + y + (y/4) + c)%7;

  switch(result)
  {
      case(0): cout << "The day of the week is: saturday";
      break;
      case(1): cout << "The day of the week is: sunday";
      break;
      case(2): cout << "The day of the week is: monday";
      break;
      case(3): cout << "The day of the week is: tuesday";
      break;
      case(4): cout << "The day of the week is: wednesday";
      break;
      case(5): cout << "The day of the week is: thursday";
      break;
      case(6): cout << "The day of the week is: friday";
  }
}

int main()
{

//example: 3rd January 1350 is a sunday

    int day;
    int month;
    int year;
    int century;


//day: 3
    cout << "This program will tell you the day of the inputs:" << endl;
    cout << "What is the day of the month?: ";
    cin >> day;
    cout << endl;

//month: 0
    cout << "Jan, Oct = 0"<< endl;
    cout << "May = 1"<< endl;
    cout << "Aug = 2"<< endl;
    cout << "Feb, Mar, Nov = 3"<< endl;
    cout << "Jun = 4"<< endl;
    cout << "Sep, Dec = 5"<< endl;
    cout << "Apr, Jul = 6"<< endl;
    cout << "Exceptions: If it is a leap year, Feb = 2 and Jan = 6"<< endl << endl;
    cout << "What is the number of the month from the months table?: ";
    cin >> month;
    cout << endl;

//year: 50
    cout << "What is the last 2 digits in the year?: ";
    cin >> year;
    cout << endl;

//century: 6
    cout << "14th century: 6" << endl;
    cout << "15th century: 5" << endl;
    cout << "16th century: 4" << endl;
    cout << "17th century: 3" << endl;
    cout << "18th century: 2" << endl;
    cout << "19th century: 1" << endl;
    cout << "20th century: 0" << endl;
    cout << "What is the century's number in the 100s of years table?: ";
    cin >> century;
    cout << endl;

    addNumbers(day,month,year,century);

return 0;
}
Your function addNumbers says it returns an int.

But it doesn't. It doesn't return anything. The function is malformed and broken.
Topic archived. No new replies allowed.