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

Dani AI

Generated

Brief diagnosis and practical checks (building on and ): the most common reason members end up as zero is that your two-argument constructor was never the one actually run. That can happen if you create the object without arguments, if you accidentally declare a function instead of an object (a classic C++ gotcha), or if you pass the arguments in the wrong order relative to what you intended. spotted the first issue and pointed toward using the parameterized constructor.

Quick, low-effort ways to verify and fix it:

  • Put a tiny debug print or breakpoint inside each constructor so you can see which constructor runs at runtime.
  • Enable compiler warnings (for GCC/Clang use -Wall -Wextra) — they often reveal accidental declarations or unused values.
  • Confirm the call sites pass arguments in the intended order (input order vs constructor parameter order).
  • Prefer initializing members with an initializer list to ensure direct initialization (and to avoid extra assignments). For example:
baseType::baseType(int b, int d)
  : base(b), decimalNum(d)
{
  std::cerr << "ctor b=" << base << " d=" << decimalNum << '\n';
}

Extra notes and cautions:

  • Initialization always follows the order of member declarations inside the class, not the order in the initializer list; keep the initializer list in the same order as declaration to avoid surprises.
  • If you do not want default-constructed objects, either remove the default constructor or make misuse harder by deleting it.
  • Use distinct member naming (mbase or base) to avoid accidentally assigning parameters to themselves.

Given ’s follow-up, invoking the correct constructor resolved the problem.

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.