Link to home
Start Free TrialLog in
Avatar of pgmerLA
pgmerLA

asked on

Class constructor and array initialization

Hi,
How can I have my constructor initialize my array "arabicConverted"?


class RomanType
{
public:
RomanType();
void GetArabicNumber();
void GetRomanNumeral();
void ConvertArabicToRoman();
void ConvertRomanToArabic();
void PrintRomanNumeral();
void PrintArabicNumeral();
~RomanType();

private:

int arabicNum;
char arabicConverted[50];
string romanNum;
};

Open in new window

#include<iostream>
#include <string>
using namespace std;
#include "RomanType.h"

RomanType::RomanType()
{arabicNum=0;
romanNum="";
}
void RomanType::GetArabicNumber()
{cout<<"Type an arabic number and press enter: ";
cin>>arabicNum;
}

void RomanType::GetRomanNumeral()
{cout<<"Type a roman numeral and press enter: ";
getline(cin,romanNum);
}

void RomanType::ConvertArabicToRoman()
{int i=0;
while(arabicNum/1000>0)
	{arabicConverted[i]='M';
	arabicNum -=1000;
	i++;
	}
while(arabicNum/500>0)
	{arabicConverted[i]='D';
	arabicNum -=500;
	i++;
	}
while(arabicNum/100>0)
	{arabicConverted[i]='C';
	arabicNum -=100;
	i++;
	}
while(arabicNum/50>0)
	{arabicConverted[i]='L';
	arabicNum -=50;
	i++;
	}
while(arabicNum/10>0)
	{arabicConverted[i]='X';
	arabicNum -=10;
	i++;
	}
while(arabicNum/5>0)
	{arabicConverted[i]='V';
	arabicNum -=5;
	i++;
	}
while(arabicNum/1>0)
	{arabicConverted[i]='I';
	arabicNum -=1;
	i++;
	}
}

void RomanType::ConvertRomanToArabic()
{
}

void RomanType::PrintRomanNumeral()
{cout<<"The roman numeral conversion is: "<<endl;
cout<<arabicConverted<<endl;
}

void RomanType::PrintArabicNumeral()
{
}
RomanType::~RomanType()
{
}

Open in new window

#include<iostream>

#include<string>
using namespace std;
#include "RomanType.h"

int main()
{

RomanType l;
l.GetArabicNumber();
l.ConvertArabicToRoman();
l.PrintRomanNumeral();
return 0;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
memset advisable
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pepr
pepr

The classical C array can often be replaced by the C++ vector, which is a dynamic array that can be cleared (thus having the size equal to zero.  See http://www.cplusplus.com/reference/stl/vector/ for example.
to add to pepr's comment:

if you change

        char arabicConverted[50];
to
 
        vector<char> arabicConverted

it also can be initialised in the initialer list:

RomanType::RomanType()
: arabicNum(0),
  romanNum(0),
  arabicConverted(50, '\0') {}

Sara
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial