pgmerLA
asked on
Class constructor and array initialization
Hi,
How can I have my constructor initialize my array "arabicConverted"?
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;
};
#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()
{
}
#include<iostream>
#include<string>
using namespace std;
#include "RomanType.h"
int main()
{
RomanType l;
l.GetArabicNumber();
l.ConvertArabicToRoman();
l.PrintRomanNumeral();
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
memset advisable
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.