Solved

Class constructor and array initialization

Posted on 2011-09-13
8
541 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 32

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

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 32

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

762 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

22 Experts available now in Live!

Get 1:1 Help Now