Solved

Class constructor and array initialization

Posted on 2011-09-13
8
544 Views
Last Modified: 2012-06-21
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

0
Comment
Question by:pgmerLA
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 36533548
The probably simplest way would be to
#include <stdlib.h> // for 'memset()'

// ...

RomanType::RomanType()
{
  arabicNum=0;
  romanNum="";

  memset(arabicConverted,0,sizeof(arabicConverted));
}

Open in new window

0
 

Assisted Solution

by:ajaytemp
ajaytemp earned 33 total points
ID: 36533788
if (arabicConverted != null)
{
// exception handling shit unknown to me
     try
{
// depending on vendors, exception will be thrown on different case with array access violation

   for (int indexer = 0;  indexer < someConstantLimit;  indexer++)
  {
      if (arabicConverted[indexer] == null);  // unknown on older systems without nx (no execute) bit
//  for older systems unknown
}
}
catch (exception ex)
{
// re throw advisable
}
}

Open in new window

0
 

Expert Comment

by:ajaytemp
ID: 36533796
memset advisable
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 100 total points
ID: 36534607
you should consider using a string same as for romanNum.

then you would add to arabicConverted like

void RomanType::ConvertArabicToRoman()
{
  arabicConverted = ""; // make empty
  while(arabicNum/1000>0)
  {
       arabicConverted += 'M';
       arabicNum -=1000;
  

Open in new window

}

a c array cannot simply initialised in the initialisier list. you would need a loop (or memset for none-class type only).

for (size_t i = 0; i < sizeof(arabicConverted/sizeof(arabicConverted[0])); ++i)
     arabicConverted[i] = '\0';

Open in new window


note, as arabicConverted[0] is char type its sizeof is 1. nevertheless it is good practice to divide the sizeof the array by the sizeof of its first element when you have to determine the number of elements, cause you later might change the char to wchar_t and the loop still would work correctly.

Sara
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 34 total points
ID: 36534996
Hi pgmerLA,

if you want to initialize it to '\0' (or any other character) you should use memset as mentioned above.

If you want to initialize it with a given set of values you can implement a function which uses memcpy to init your array, i.e.:
void RomanType::InitArabicConverted()
{
  static char data[50] = { 0, 1, 2, 3 ... };
  memcpy( arabicConverted, data, 50 * sizeof( char ) );
}

Open in new window

and call this within your constructor.

ZOPPO
0
 
LVL 28

Expert Comment

by:pepr
ID: 36535519
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.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 36535559
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
0
 
LVL 28

Assisted Solution

by:pepr
pepr earned 33 total points
ID: 36535565
For the case you will be searching for Roman to Arabic numeral and back, there is rather interesting case study in Mark Pilgrim's "Dive Into Python 3" -- http://diveintopython3.org/regular-expressions.html#romannumerals.  It is in Python, but there also are some ideas, and it can definitely be converted to C++.  There is one more chapter related to the case that introduces Unit testing (http://diveintopython3.org/unit-testing.html).  It is not that brief in C++; however, you can basically do the same with the cppunit (http://sourceforge.net/projects/cppunit/).  If you do not know the Unit testing, you may think it is a bit overkill for you.  When learning it, you will definitely appreciate it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 66
Would like to move button in a function 3 71
Socket Programming (Unix) 8 118
Header of docx file 17 97
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now