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;
|