the values are not being passed correctly, in my constructor i copy the value of d to decimalNum and the value of b to base. when i run the program the values are 0. what am i doing wrong?:

baseType::baseType(int b, int d)
{
    base = b;
    decimalNum = d;
}

Recommended Answers

All 3 Replies

You're calling the default constructor, not the two argument constructor:

#include <iostream>
#include <string>

using namespace std;

class baseType
{
public:

  void decToBase(int num, int base);
  void print();
  baseType(int b, int d);
  baseType();

private:

  int decimalNum;
  int base;
};

int main()
{
  int d;
  int b;

  cout << "Enter number in decimal and the Base to Convert: ";
  cin >> d >> b;
  baseType myBase(b, d);
  cout << endl;
  myBase.decToBase(d, b);
  myBase.print();

  return 0;
}

void baseType::print()
{
  cout << "Decimal " << decimalNum << " = ";
  //	decToBase(decimalNum, base);
  cout << " in Base " << base << endl;
}

void baseType::decToBase(int num, int base)
{
  if (num > 0)
  {
    string digit="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    decToBase(num / base, base);
    cout << digit[num % base];
  }
}

baseType::baseType(int b, int d)
{
  base = b;
  decimalNum = d;
}

baseType::baseType()
{
  decimalNum = 0;
  base = 0;
}

Why don't you use the parameterized constructor?

int main()
{
	int d;
	int b;

	cout << "Enter number in decimal and the Base to Convert: ";
	cin >> d >> b;
	cout << endl;
	baseType myBase(d,b);
	myBase.decToBase(d, b);
	myBase.print();

	return 0;
}

thank you. tried your suggestion and it worked

Be a part of the DaniWeb community

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