6

I'm just going over classes but I get this error which stops me blank. I've looked through other threads but no clue what's wrong with my code? I've commented the code which is the issue.

class myClass { //Class which might cause an issue? Don't see whats wrong    with it.
public:
myClass(string nm) {
    setName(nm);
}
void setName(string x) {
    name = x;
}
string getName() {
    return name;
}

private:
    string name;
};

int main() {
cout << "Task 1, Task 2, Task 3, Task 4 ?" << endl;
int answer;
cin >> answer;
switch (answer)
{
case 1://Practising Classes
    CRectangle area;
    cout << "Enter two numbers \n";
    cin >> area.x;
    cin >> area.y;
    cout << "Area is: "<< area.findArea() << endl;
    break;
case 2://Practising Classes
    AddNumbers myObj1;
    myObj1.getNumbers();
    cout  << myObj1.addNumbers() << endl;
case 3: //Practising Classes
    birthdays b1;
    cout << "Welcome to Birthdays! \n";
    bool bool1 = false;
    do {
    cout << "Do you want to enter some data (1) or retrieve some? \n";
    int answer;
    cin >> answer;
    switch (answer)
    {
    case 1:
        b1.setdata();
        break;
    case 2:
        b1.getdata();
    }
    } while (bool1 == false);
case 4: // This causes the error. // Testing out Constructors
    myClass object("David");
    myClass object2("Amy");
    cout << object.getName();
}
system("PAUSE");
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
IVIaximumPower
  • 131
  • 1
  • 3
  • 9
  • Hmm, dupe of http://stackoverflow.com/q/5136295/560648? It's listed right there in the "related questions" pane. – Lightness Races in Orbit Feb 17 '17 at 12:00
  • Please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). A good question about build errors should contain a text copy-paste of the actual errors, in full, complete, with possible informational notes, and not edited in any way. And besides a comment in the code pointing out where the error is (that you did it is good, not all beginners here do it) also indicate the location in the question body text too. – Some programmer dude Feb 17 '17 at 12:00
  • @Someprogrammerdude Sorry i'll make sure to do it next time! And too Lightness i'm new to C++ and didnt understand what that meant. I get confused quite easily but im doing my best! – IVIaximumPower Feb 17 '17 at 12:03
  • @IVIaximumPower If I would compile this code in my VC++ I would suspect to get errors (or warnings) "Control by-passes declaration of..." or something like this. If you declare variables inside `case`s of a `switch` put braces (`{` and `}`) around the code. And, probably, you forgot the `break`s at the end of the last three `case` branches... – Scheff's Cat Feb 17 '17 at 12:06

1 Answers1

8

Those case "statements" are actually labels, like goto. They do not begin a new scope. When a condition is found, execution "jumps" to the relevant case label and continues from there.

The language's rules insist that you cannot "jump" over initialisations, as allowing this in a consistent and predictable way would require more complex standard wording.

Put your cases in a scope of their own to "insulate" the declarations and prevent them from "leaking" into the next case, which is what the compiler is concerned about.

For example:

case 1: { //Practising Classes
   CRectangle area;
   cout << "Enter two numbers \n";
   cin >> area.x;
   cin >> area.y;
   cout << "Area is: "<< area.findArea() << endl;
   break;
}

I've added the { and the }. These are not part of the syntax of switch/case, but just standalone scope blocks, much like the inner ones here:

int main()
{
   int x = 42;
   {
      int x = 999;  // a different x!
   }
}
Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Thanks! It's fixed now <3 i was stuck on this for a while. Something im learning C++ on "SoloLearn" did tell me this. Thanks! – IVIaximumPower Feb 17 '17 at 12:00