Need help correcting faults by 25th Feb

Hello, i need some help with something i am making in c++. I am a beginner and c++ is an obligatory part of my school class. My teacher made a console application which adds 2 numbers together using a class. The code he gave us is here.

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
// Standard includes...
#include <iostream>

// When we include a file, we essentially copy
// it's contents from the header and paste them here.
// This just tells the compiler that we want to use the stuff in this class.
#include "Adder.h"

// You kow what this does...
using namespace std;

// Entry point...
int main( void ) {
	// Here we make an instance of an object.
	// We make a variable of the "Adder" datatype and call it "adder".
	// And because we call the constructor when we call the object, we have to give parameters (if any).
	// This is an instance of an object, but we can still refer to it as a variable.
	Adder adder( 45, 90 );
	// Here we make an integer variable which equals the value returned from the "Add()" function of the adder class.
	int number2 = adder.Add();
	// Here we just print it out.
	cout << number2 << endl;

	// Wait for the user to press enter...
	cin >> number2;
	// And quit...
	return 0;
}


Now he wants us to be able to let the user choose whether they want to add, subtract, divide and multiply. He also insists that we make classes for each of the 4. I have made classes of the 4 and have added code for the other 3 functions, however a few error messages come up. The code above will work fine but when i try to add the multiple choice option and the other 3 options, the application will not work. As i said before i am a complete beginner to this and the faults in my code are probably trivial to people on this forum so sorry if my mistakes are very obvious, but if someone could help me understand where i am going wrong and help me correct it by the 25th i would greatly appreciate it. I'll post the code below with the choices. I am using visual studio to write this code and below are the faults that come up

error C2360: initialization of 'number2' is skipped by 'case' label
error C2360: initialization of 'adder' is skipped by 'case' label
error C2374: 'number2' : redefinition; multiple initialization
error C2088: '<<' : illegal for class
error C2088: '>>' : illegal for class
error C2360: initialization of 'number2' is skipped by 'case' label
error C2360: initialization of 'subtracter' is skipped by 'case' label
error C2360: initialization of 'adder' is skipped by 'case' label
error C2374: 'number2' : redefinition; multiple initialization
error C2088: '<<' : illegal for class
error C2088: '>>' : illegal for class
error C2360: initialization of 'number2' is skipped by 'case' label
error C2360: initialization of 'multiplier' is skipped by 'case' label
error C2360: initialization of 'subtracter' is skipped by 'case' label
error C2360: initialization of 'adder' is skipped by 'case' label
error C2374: 'number2' : redefinition; multiple initialization
error C2088: '<<' : illegal for class
error C2088: '>>' : illegal for class
IntelliSense: transfer of control bypasses initialization of:
variable "adder" (declared at line 27)




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
/// Standard includes...
#include <iostream>

// When we include a file, we essentially copy
// it's contents from the header and paste them here.
// This just tells the compiler that we want to use the stuff in this class.
#include "Adder.h"
#include "Subtracter.h"
#include "Multiplier.h"
#include "Divider.h"

// You kow what this does...
using namespace std;

// Entry point...
int main(void)

{   int choice;
	cout << "1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n";
	cin >> choice;
	switch (choice){
	case 1:
		// Here we make an instance of an object.
		// We make a variable of the "Adder" datatype and call it "adder".
		// And because we call the constructor when we call the object, we have to give parameters (if any).
		// This is an instance of an object, but we can still refer to it as a variable.
		Adder adder(2, 2);
		// Here we make an integer variable which equals the value returned from the "Add()" function of the adder class.
		int number2 = adder.Add();
		// Here we just print it out.
		cout << number2 << endl;

		// Wait for the user to press enter...
		cin >> number2;
	
		break;
		
	case 2:
			Subtracter subtracter(2, 2);
			int number2 = subtracter.Subtract();
			cout << number2 << endl;
			cin >> number2;

			break;

	case 3:
		    Multiplier multiplier(2, 2);
		    int number2 = multiplier.Multiply();
		    cout << number2 << endl;
		    cin >> number2;

			break;

	case 4:
		   Divider divider(2, 2);
		   int number2 = divider.Divide();
		   cout << number2 << endl;
		   cin >> number2;

		   break;

	}
	// And quit...
	return 0;

Last edited on
Could you please post your code between code tags. Its under format.
http://www.cplusplus.com/articles/jEywvCM9/
Sorry, is that better?
Much better.

Try putting brackets around your case labels, that should fix the " initialization of 'number2' is skipped by 'case' label" errors. like this -

1
2
3
4
5
6
7
8
9
10
11
 
case 2:
		{
                 	Subtracter subtracter(2, 2);
			int number2 = subtracter.Subtract();
			cout << number2 << endl;
			cin >> number2;

			break;
                 }

Do that for all 4 cases.

Then post the errors that you get if any.
Last edited on
Thank you so much, that has fixed everything. Feels like such a rookie mistake as the solution was literally just add a few brackets XD. I'll be sure to ask here if i have anymore problems. Thanks once again :)
Thank you so much, that has fixed everything. Feels like such a rookie mistake as the solution was literally just add a few brackets XD. I'll be sure to ask here if i have anymore problems. Thanks once again :)

ยด
Dont worry about it. I myself actually dont know why the problem accurs. But only that if the code is like this - int number2 = subtracter.Subtract(); It wont work without brackets. So basically, if a variable is declared and initialized to something in the same line of code, it wont work. I believe if you would have done it like this -

1
2
int number2;
number = subtracter.Subtract();


It would have worked fine.
Hi,

If you have errors with VS, just google the Error number - C2360 say - that will tell why you have a problem.

With this particular problem, it might be better to have each case call a function, then all the initialisation happens inside the function, and the scope problems go away. This also has the advantage of making the code easier to read & understand.

12
13
// You kow what this does...
using namespace std;


If you knew what it does ...... then you wouldn't do it !! Google to see why line 13 is bad, along the way find out what namespaces really do, then relay your findings to your teacher.

Always provide a default: case for your switch, use it to catch any bad input.

Edit:

Provide a case which will allow the user to choose to quit.

Good Luck !!
Last edited on
@morchard624 - Since you are using "int number2;" in all cases, Its better to create it outside of the switch statement.

@TheIdeasMan - Thank you. I've heard alot of people say namespaces are bad practice, but never really cares. Using namespace mades things a bit easier but mostly because for now, our teacher is letting us use it. Shouldn't be hard to start using std:: from now on though :)
Thanks for the advice both of you, I'll be sure to try this for future tasks :)
Topic archived. No new replies allowed.