Solved

Class constructor and array initialization

Posted on 2011-09-13
8
550 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 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