Error Message?

Why do I get an error message for an expected "}" at the end of my source when there is one there?

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  // Star Signs - What's your telling?

#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    int nDay;
    int nMonth;
    cout << "Enter Day of Birth: ";
    cin >> nDay;
    cout << "Enter Month of Birth: ";
    cin >> nMonth;

    if (nMonth == 12) {
        if (nDay >= 22)
        {
            cout << "You are an Aquarius."
            << endl;
        }
        else if (nDay <= 21)
        {
            cout << "You are a Sagittarius."
            << endl;
        }
        if (nMonth == 11) {
            if (nDay >= 22)
        {
            cout << "You are Sagittarius."
            << endl;
        }
        else if (nDay <=21)
        {
            cout << "You are a Scorpio."
            << endl;
        }
        if (nMonth == 10){
            if (nDay >= 23)
        {
            cout << "You are a Scorpio."
            << endl;
        }
        else if (nDay <= 22)
        {
            cout << "You are a Libra."
            << endl;
        }
        if (nMonth == '09') {
            if (nDay >= 23)
        {
            cout << "You are a libra."
            << endl;
        }
        else if (nDay <= 22)
        {
            cout << "You are a Virgo."
            << endl;
        }
        if (nMonth == '08'){
            if (nDay >= 23)
        {
            cout << "You are a Virgo."
            << endl;
        }
        else if (nDay <= 22)
        {
            cout << "You are a Leo."
            << endl;
        }
        if (nMonth == '07'){
            if (nDay >= 23)
        {
            cout << "You are a Leo."
            << endl;
        }
        else if (nDay <= 22)
        {
            cout << "You are a Cancer."
            << endl;
        }
        if (nMonth == '06'){
            if (nDay >= 21)
        {
            cout << "You are a Cancer"
            << endl;
        }
        else if (nDay <= 20)
        {
            cout << "You are a Gemini."
            << endl;
        }

    if (nMonth == '05'){
        if (nDay >= 21)
    {
        cout << "You are a Gemini."
        << endl;
    }
    else if (nDay <= 20)
    {
        cout << "You are a Taurus."
        << endl;
    }
    if (nMonth == '04'){
        if (nDay >= 20)
        {
            cout << "You are a Taurus."
            << endl;
        }
        else if (nDay <= 19)
        {
            cout << "You are an Aries."
            << endl;
        }
        if (nMonth == '03'){
            if (nDay >= 21)
            {
                cout << "You are an Aries."
                << endl;
            }
            else if (nDay <= 20)
            {
                cout << "You are a Pisces"
                << endl;
            }
            if (nMonth == '02'){
                    if (nDay >= 19)
                    {
                        cout << "You are a Pisces."
                        << endl;
                    }
                    else if (nDay <= 18)
                    {
                        cout << "You are an Aquarius."
                        << endl;
                    }
                    if (nMonth == '01'){
                            if (nDay >= 19)
                            {
                                cout << "You are a Capricorn."
                                << endl;
                            }

    system ("PAUSE") ;
    return 0;

            }
I think you are missing some closing curly brackets }. You have 36 { but only 24 }. For example, look at line 18. You process if the day is >=22, you process if day is <=21, and then you go to next month, without saying to the compiler that you have finished with month 12.

Also, a logic question for you: why do you use else if instead of just else? So if day>=22 do something, else if day<=21 do something else. Is there a way when if day is not >=22 to be anything else than <=21?
I was just missing closing curly brackets on at the end of my else if commands. I could have just used else as well, didn't quite think about that. But the program is working now, thank you!
Last edited on
Hi,

Just a small tip to help you avoid this problem:

Either configure your IDE (if using one) to automatically do closing braces, parentheses, brackets etc, or;

Get into the habit of typing the opening brace, immediately followed by the closing one, then go back and fill in what goes in between the two.

Try to avoid mixing different styles of indenting and brace placement, for example you have this:

118
119
120
121
122
123
if (nMonth == '03'){
            if (nDay >= 21)
            {
                cout << "You are an Aries."
                << endl;
            }


To be consistent, one might have this:

118
119
120
121
122
123
124
125
126
127
128
129
130
      if (nMonth == '03')
      {
            if (nDay >= 21)
            {
                cout << "You are an Aries."
                << endl;
            }

      // other nested if statements

     // other closing brace at the end of first if statement, in the same 
     // column as the one on line 119
     }


Choose which style you like, and stick to it. The main thing is to ensure that it is visually obvious whether it is right or wrong.

Also, about the logic:

But the program is working now, thank you!


Unfortunately that doesn't always mean it is as good as it could be. Did you test values for each month?

You have a lot of tests for which month it is, but there are a lot that are nested inside an existing test for a month. For example the tests in lines 29 - 94 will always be false.

If I was doing it, there would be some error checking on the values entered for month and day - months 1 to 12 , and days in the correct range (no Feb 30 etc).

Then the logic is simple: use a switch statement with a default case to determine the month, then an if else on the value of day to determine the star sign. You could use the ternary ? : operator to do that on one line of code.

Hope all is well, and seasons greetings o<:+)
Last edited on
Topic archived. No new replies allowed.