Solved

Class constructor and array initialization

Posted on 2011-09-13
8
561 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

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 29

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 34

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 29

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 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