C++ Program not compiling

Hello all,

I am having issues with a specific task with writing a C++ Program. Any help on what is needed would be greatly appreciated. The feedback that I received is as followed:

"The code does not compile due to syntax problems. The curly brackets don't seem to be matched up correctly. It would have been better to leave out the menu. The curly brackets go around the whole switch...case block, not individual cases. Remember that you should not have a semicolon after a catch().

You shouldn't need to use references in the catch clauses.

The correct way to introduce the custom exceptions is to include the headers for the exceptions in the Shape base class, not in main.cpp.

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
#include <iostream>
#include <string>

 // The header files are included to use the Class in main
#include "shape.h"
#include "circle.h"
#include "square.h"

#include "zeroexception.h"  // The header is required as exception(Zero value) is raised while calculating area
#include "negativeexception.h" // The header is required as exception (negative value) is raised while calculating area

using namespace std;

int main()
{

    int menuOption;
    int shape_id = 0;

    do
    {   // Starting of while loop, do-while loop is used so that the menu is shown at least once unless user select exit
        shape_id++;
        cout << endl << "=========CALCULATE AREA================" << endl;
        cout<< endl
        << "Select an Object" << endl
        << " 1: Circle" << endl
        << " 2: Square" << endl
        << " 3: Triangle" << endl
        << " 0: Exit" << endl
        << " Enter Your Choice: ";
        cin  >> menuOption;


        std::string unit = "Meter";

        try
        {
            switch(menuOption)
            {
            case 1:


                    cout<< shape_id << ": Enter radius (in "<<unit<<") : " ;
                    float radius;
                    cin >> radius;

                    /* Demonstration of instantiation Circle class */
                    Circle c1(radius,shape_id, unit);

                    /* Demonstration of calling method */
                    float area1 = c1.getArea();

                    c1.display();


                break;

            case 2:




                    cout<< shape_id << ": Enter length of one side : ";
                    float length ;
                    cin >> length;

                    /* Demonstration of instantiation Square class and storing the reference to base class object */
                    Shape *s1 = new Square(length,shape_id, unit);

                    /* Demonstration of calling virtual method, using base object reference of Square class */
                    float area2 = s1->getArea();

                    s1->display();
                                                                                                                                                                                                                                                                                                                                                                             }


                break;
            case 3:



                    cout<< shape_id << ": Enter base : ";
                    float base,height ;
                    cin >> base;
                    cout<< shape_id << ": Enter height : ";
                    cin >> height;




            }
        }
        catch(ZeroException& exp); /* Example of catching Custom exceptions (1) */
        {
            cout<<"!! ZeroException caught:  " << exp.what() << endl;
        }
        catch(NegativeException& exp) /* Example of catching Custom exceptions (2) */
        {
            cout<<"!! NegativeException caught:  " << exp.what() << endl;
        }
        catch(exception& exp)  /* Example of catching standard exceptions */
        {
            cout<<"!! Standard Exception caught:  " << exp.what() << endl;
        }

    } while(menuOption!=0);


    cout << endl<< endl << "============== THANK YOU ===================" << endl<< endl;

    return 0;
}
}
Hi,
even we cant compile this code due to missing header files

that being said I underlined the following errors which are relevant to syntax error (and not no file found errors [i suppose those are called linker errors])

lines in bold are the error your teacher pointed out

[code] In function 'int main()':
42:21: error: 'Circle' was not declared in this scope
45:35: error: 'c1' was not declared in this scope
52:18: error: jump to case label [-fpermissive] 45:27: error: crosses initialization of 'float area1'
62:21: error: 'Shape' was not declared in this scope
62:28: error: 's1' was not declared in this scope
62:37: error: expected type-specifier before 'Square'
45:27: warning: unused variable 'area1' [-Wunused-variable]
65:27: warning: unused variable 'area2' [-Wunused-variable]
72:13: error: case label '3' not within a switch statement 86:9: error: expected 'catch' before '}' token
86:9: error: expected '(' before '}' token
86:9: error: expected type-specifier before '}' token
86:9: error: expected ')' before '}' token
86:9: error: expected '{' before '}' token
87:14: error: expected 'while' before '(' token

87:15: error: 'ZeroException' was not declared in this scope
87:30: error: 'exp' was not declared in this scope
91:9: error: expected primary-expression before 'catch'
95:9: error: expected primary-expression before 'catch'

At global scope:
[i]100:7: error: expected unqualified-id before 'while'
103:5: error: 'cout' does not name a type
105:5: error: expected unqualified-id before 'return'
106:1: error: expected declaration before '}' token[/i][/code]

try to debug as much as you can, if you still got problems ask us, we'll be glad to help :)
Last edited on
This is what happens when you write huge amounts of code and compile after you're done.

You should've kept compiling as you wrote code. Testing if the new code you added works each time.
Last edited on
I am still not sure where to start on this. I have taken a needed Programming Course for graduation and my friend who is a part time programmer helped me. We were not able to correct this, and he is out of town to assist me again. If someone can point me in the right direction for what the instructor asked me to do that would be great.

Thanks Guys!
Line number 74
Column number 366
there is a stray closing brace }

... yes, there really is a column 366 - keep scrolling to the right until you find it.

Delete that closing brace - it matches (though it shouldn't) the opening brace at line 39.

While you're at it, delete the whole of line 74, the rest is just blank spaces.

There's another stray closing brace right at the end. After return 0; there should be just a single }
Thank you, Chervil,

I was unable to see column 366, but removed the space and braces you recommended. Are you able to assist with what the instructor asked me to do? I appreciate your help on this!
Well, there are two parts to this. First, you need to get the code to compile.
Then you need to modify it as requested by your instructor.

Have you got the code to compile yet?
One comment regarding the use of switch-case statements. Often there is no need for the use of braces { } within each case block. However, when local variables are used, then the scope of that variable must be contained within its own open/close braces otherwise the code will not compile. Example. In the second case the local variable y is used, without the braces it would still be visible from the subsequent parts of the switch-case but would not have been properly initialised.
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
#include <iostream>

using namespace std;
    
int main() 
{

    int x = 2;
    
    // Simple switch-case    
    switch (x) 
    {
        case 1:
            cout << "x is 1\n";
            break;
            
        case 2:
            cout << "x is 2\n";
            break;
            
        default:
            cout << "value of x unknown\n";
    }


    // Same switch-case with local variable
    switch (x) 
    {
        case 1:
            cout << "x is 1\n";
            break;
            
        case 2:
            {
                int y = x * x;
                cout << "x is 2\n";
                cout << "y is " << y << '\n';
            }
            break;
            
        default:
            cout << "value of x unknown\n";
    }

}


As an experiment, comment out the lines 34 and 38, these are the compiler error messages
In function 'int main()':
41	9	[Error] jump to case label [-fpermissive]
35	21	[Error] crosses initialization of 'int y'


It's hard to go further without having the various header files available:
1
2
3
4
5
"shape.h"
"circle.h"
"square.h"
"zeroexception.h"
"negativeexception.h"


Last edited on
OP: if you do decide to share the missing files that Chervil highlighted you might consider posting them at, for e.g., http://pastie.org/, and just giving us the links. It's very difficult to read super-long posts. Thanks
I would be happy to send all the files we created to see if I can get this to run. Its giving me the hardest time, and the instructor makes it seem like they are simple changes. I will work on trying to find a way to get the files over.
I am still unable to make this work. The instructor makes it seem like the changes needed are minor, and I'm not finding that. Also if someone wants to try to assist me and walk me through it, I would be happy to pay them for their time, I know this is a challenging one.
Last edited on
mirhodes,

I have made a plausible guess at what is probably in those headers and replaced them with some classes sufficient to debug your code. (But I don't know much about exception classes, I'm afraid.) You should go back to using your own headers when you have noted the comments.

The code below contains the corrections already suggested above; in particular:
- everything underlined by @shadder;
- braces within the switch-case blocks if you introduce new local variables there (very grateful to @chervil here: I didn't realise this)

You still need to do something with triangles.

Suggestion for the future: recompile and retest at frequent intervals whilst developing code. If you get lots of errors coming at once then it is very difficult to deal with them.
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
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <string>
#include <exception>             // <===== Possibly need this header
using namespace std;


// The header files are included to use the Class in main
// #include "shape.h"
// #include "circle.h"
// #include "square.h"

// #include "zeroexception.h"  // The header is required as exception(Zero value) is raised while calculating area
// #include "negativeexception.h" // The header is required as exception (negative value) is raised while calculating area

// THESE ARE FUDGES OF POSSIBLE HEADER CLASSES - JUST TO GET YOUR CODE TO COMPILE ********************
const float PI = 3.14159;

class Shape
{
protected:
   int id;
   string unit;
   string Stype;
public:
   Shape() {}
   ~Shape() {}
   int getId() { return id; }
   string getUnit() { return unit; }
   virtual float getArea() {}
   virtual void display() {}
};

class Circle : public Shape
{
   float radius;
public:
   Circle( float r, int i, string un ) { radius = r; id = i; unit = un; Stype = "Circle"; }
   ~Circle() {}
   float getRadius() { return radius; }
   float getArea() { return PI * radius * radius; }
   void display() { cout << Stype << ",  id=" << id << "   radius=" << radius << "   area=" << getArea() << endl; }
};

class Square : public Shape
{
   float side;
public:
   Square( float s, int i, string un ) { side = s; id = i; unit = un; Stype = "Square"; }
   ~Square() {}
   float getSide() { return side; }
   float getArea() { return side * side; }
   void display() { cout << Stype << ",  id=" << id << "   side=" << side << "   area=" << getArea() << endl; }
};

class Triangle : public Shape
{
   float base;
   float height;
public:
   Triangle( float b, float h, int i, string un ) { base = b; height = h; id = i; unit = un; Stype = "Triangle"; }
   ~Triangle() {}
   float getBase() { return base; }
   float getHeight() { return height; }
   float getArea() { return 0.5 * base * height; }
   void display() { cout << Stype << ",  id=" << id << "   base=" << base << "   height=" << height << "   area=" << getArea() << endl; }
};

class ZeroException: public exception { public: const char* what() const throw() { return "Zero exception happened"; } };
class NegativeException: public exception { public: const char* what() const throw() { return "Negative exception happened"; } };

// END OF FUDGES **********************************************************************************



int main()
{

    int menuOption;
    int shape_id = 0;

    do
    {   // Starting of while loop, do-while loop is used so that the menu is shown at least once unless user select exit
        shape_id++;
        cout << endl << "=========CALCULATE AREA================" << endl;
        cout<< endl
        << "Select an Object" << endl
        << " 1: Circle" << endl
        << " 2: Square" << endl
        << " 3: Triangle" << endl
        << " 0: Exit" << endl
        << " Enter Your Choice: ";
        cin  >> menuOption;


        string unit = "Meter";

        try
        {
            switch(menuOption)
            {
               case 1:
               {                         // <<===== added brace because of local variables c1 and area1 in this scope
                    cout<< shape_id << ": Enter radius (in "<<unit<<") : " ;
                    float radius;
                    cin >> radius;

                    /* Demonstration of instantiation Circle class */
                    Circle c1(radius,shape_id, unit);

                    /* Demonstration of calling method */
                    float area1 = c1.getArea();

                    c1.display();
                    break;
               }                         // <====== added closing brace
               case 2:
               {                         // <<===== added brace because of local variables s1 and area 2 in this scope
                    cout<< shape_id << ": Enter length of one side : ";
                    float length ;
                    cin >> length;

                    /* Demonstration of instantiation Square class and storing the reference to base class object */
                    Shape *s1 = new Square(length,shape_id, unit);

                    /* Demonstration of calling virtual method, using base object reference of Square class */
                    float area2 = s1->getArea();

                    s1->display();
                    break;
               }                         // <====== added closing brace
               case 3:
               {                         // <<===== added brace because of local variables (eventually!) in this scope
                    cout<< shape_id << ": Enter base : ";
                    float base,height ;
                    cin >> base;
                    cout<< shape_id << ": Enter height : ";
                    cin >> height;
                    cout << "You haven't decided what to do with a triangle yet!" << endl;
                                         // <<===== add whatever you want for a triangle here
               }                         // <====== added closing brace
            }
        }

        catch(ZeroException& exp)         // <===== removed extra semicolon
        {
            cout<<"!! ZeroException caught:  " << exp.what() << endl;
        }
        catch(NegativeException& exp)
        {
            cout<<"!! NegativeException caught:  " << exp.what() << endl;
        }
        catch(exception& exp)
        {
            cout<<"!! Standard Exception caught:  " << exp.what() << endl;
        }

    } while(menuOption!=0);


    cout << endl<< endl << "============== THANK YOU ===================" << endl<< endl;

    return 0;
}
                                             //<===== Removed stray trailing brace 
I believe that your @lastchance is working for me! I am going to try to make some adjustments and see if this gets approved. I appreciate you all looking into this!
Topic archived. No new replies allowed.