In addition to the error in the title, it says that A2
and A1
are undeclared identifiers and that Result::calResult:function
does not take 2 arguments, but it does in its implementation. And lastly, it says that left of getValue
must have class/struct/union.
I am new to C++ and am trying to create a program that compares 3 elements of two arrays, I know this is not the best way to do this but I am trying to get my head around objects and classes.
Main.cpp:
#include "stdafx.h"
#include <iostream>
#include "ArrayOfThree.h"
using namespace std;
int main()
{
ArrayOfThree A1, A2;
Result R;
int i = 0;
int input;
for (int i=0;i<2;i++)
{
cin >> input;
A1.set(i, input);
}
for (int i = 0; i < 2; i++)
{
cin >> input;
A2.set(i, input);
}
R.CalResult(A1, A2);
R.outResult();
return 0;
}
ArrayOfThree.h
#include "stdafx.h"
#include <iostream>
#include "Result.h"
using namespace std;
class ArrayOfThree {
private:
int a1, a2, a3;
public:
ArrayOfThree() {
a1 = 0; a2 = 0; a3 = 0;
}
void set(int index,int input) {
if (index == 0)
a1 = input;
else if (index == 1)
a2 = input;
else if (index == 2)
a3 = input;
else
cout << "Index out of bound" << endl;
}
int getValue(int index) {
if (index == 0)
return a1;
else if (index == 1)
return a2;
else if (index == 2)
return a3;
else
cout << "Index out of bound" << endl;
return 0;
}
};
Result.h
#include "stdafx.h"
#include <iostream>
using namespace std;
class Result {
private:
int r1=0, r2=0;
char R;
public:
Result() { r1 = 0; r2 = 0; R = ' '; }
void CalResult(ArrayOfThree A1, ArrayOfThree A2)
{
for (int i = 0; i < 2; i++)
{
if (A1.getValue(i) < A2.getValue(i))
r2++;
else if (A1.getValue(i) > A2.getValue(i))
r1++;
else
r1 = r1;
}
if (r1 < r2)
R = 'B';
else if (r1 > r2)
R = 'A';
else
R = 'T';
}
void outResult()
{
if (R == 'B' || R == 'A')
cout << "The winner is :" << R;
else
cout << "Its a Tie" << endl;
}
};