case problem.

the first bold line gives me an error
error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>]

the second bold line gives
jump to case label

any ideas?

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
#include <iostream>
#include <iomanip>
using namespace std;


class cube
{
    public:
        double side;
        double volume()
        {
            return (side*side*side);
        }
        cube(){}
        ~cube(){}
};

class sphere
{
    public:
        double radius;
        double volume()
        {
            return ((4*3.141593*radius)/3);
        }
        sphere(){}
        ~sphere(){}
};

int main()
{
    int Menu_choice;
    cout << "Please select what solid you would like the volume of\n";
    cout << " For a cube, press 1\n";
    cout << "For a sphere press 2\n";
    cout << "To end the program, press x\n";
    cin >> Menu_choice;
    switch(Menu_choice)
    {
        case 1:
        cube c1;
        cout << "Please enter the length of the side of the cube\n";
        cin >> c1.side;
        cout << "The volume of the cube is :" << c1.volume << endl;
        break;
        case 2: 
        sphere s1;
        cout << "Please enter the radius of the sphere\n";
        cin >> s1.radius;
        cout << "The volume of the sphere is: " << s1.volume << endl;
        break;
        case 120:
        case 88:
        break;
        case default:
        cout << "Please enter a valid choice.\n";
    }
    return 0;
}
The error is in line 44 (and 50), not 42. volume is a function, but you aren't calling it.
You can't jump over variable initializations, like the error message says. If Menu_choice was 2 or something else, you'd jump over the initialization of c1, which isn't allowed. So you have to make sure c1 is destroyed before case 2 starts by moving it into a subblock:

1
2
3
4
5
6
7
8
9
      case 1:
      {
        cube c1;
        cout << "Please enter the length of the side of the cube\n";
        cin >> c1.side;
        cout << "The volume of the cube is :" << c1.volume() << endl;
        break;
      }
      case 2: 
Last edited on
hi
your program has 2 problem.
1.dont forget the Parenthesis () after volume method.
1
2
3
case default:
        cout << "Please enter a valid choice.\n";
    }

what is "case default"? is it new method for switch-case?
just make it "default".
this source code compile fine:
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
#include <iostream>
#include <iomanip>
using namespace std;


class cube
{
    public:
        double side;
        double volume1(void)
        {
            return (side*side*side);
        }
        cube(){}
        ~cube(){}
};

class sphere
{
    public:
        double radius;
        double volume1(void)
        {
            return ((4*3.141593*radius)/3);
        }
        sphere(){}
        ~sphere(){}
};

int main()
{
    int Menu_choice;
    cout << "Please select what solid you would like the volume of\n";
    cout << " For a cube, press 1\n";
    cout << "For a sphere press 2\n";
    cout << "To end the program, press x\n";
    cin >> Menu_choice;
    switch(Menu_choice)
    {
        case 1:
             {
        cube c1;
        cout << "Please enter the length of the side of the cube\n";
        cin >> c1.side;
       cout << "The volume of the cube is :" << c1.volume1() << endl;
        }
        break;
        case 2:
             { 
        sphere s1;
        cout << "Please enter the radius of the sphere\n";
        cin >> s1.radius;
        cout <<"The volume of the sphere is: "<< s1.volume1()<<endl;
    
        }
        break;
        case 120:
        case 88:
        break;
        default:
        cout << "Please enter a valid choice.\n";
    }
    return 0;
}
now it is giving me a primary expression before default....
everything else is working though, seemingly.
Thanks Athar

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
    switch(Menu_choice)
    {
        case 1:
        {
        cube c1;
        cout << "Please enter the length of the side of the cube\n";
        cin >> c1.side;
        cout << "The volume of the cube is :" << c1.volume() << endl;
        break;
        }
        case 2:
        {
        sphere s1;
        cout << "Please enter the radius of the sphere\n";
        cin >> s1.radius;
        cout << "The volume of the sphere is: " << s1.volume() << endl;
        break;
        }
        case 120:
        {
            break;
        }
        case 88:
        {
            break;
        }
        case default:
        cout << "Please enter a valid choice.\n";
    }
    return 0;
}
You've already been told:

what is "case default"? is it new method for switch-case?
just make it "default".
Last edited on
yeah, sorry, that reply got added right as i posted....
thanks anyway!
Topic archived. No new replies allowed.