I'm trying to create a class which finds the sum of multiples for a given base
I have the code written with out a class im just having trouble converting it into a class
my total function isnt retrieving the data from the object maybe?

``````/*
If we list all the natural numbers below 10 that are multiples of
3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
*/

#include<iostream>
using namespace std;

class SumofMultiples
{
int base, multia, multib, count, totalsum;
public:
SumofMultiples (int, int, int);
//~SumofMultiples();
int total ()
{
cout << "Base: " << base << "multiple a: " << multia << "multiple b: " << multib << "count: " << count << "total sum: " << totalsum << endl;
totalsum=0;
count=base/multia;
while (count>0)
{
totalsum+=(multia*count);
count--;
}
count=base/multib;
while (count>0)
{
if(count%multia!=0)
{
totalsum+=(multib*count);
}
count--;
}
return totalsum;
}
};

SumofMultiples::SumofMultiples(int i, int a, int b)
{
i = base;
a = multia;
b = multib;
}

int main ()
{
SumofMultiples test (1000, 3, 5);
int Result = test.total();
cout << "Result: " << Result  << endl;
//cout << "Result: " << test.total()  << endl;
return 0;
}

/*int main ()
{
int base, multiA, multiB, count, totalsum;
base = 999;
multiA= 3;
multiB= 5;
count=base/multiA;
while (count>0)
{
totalsum+=(multiA*count);
count--;
}
count=base/multiB;
while (count>0)
{
if(count%multiA!=0)
{
totalsum+=(multiB*count);
}
count--;
}
cout << "Total is " << totalsum << endl;
}*/
``````

Lines 41 - 43 have the assignments the wrong way round, you are assigning the value of the class members to the initialisation parameters rather than the value of the initialisation parameters to the class variables e.g. `base = i;`

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.