Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Class constructor and array initialization

Posted on 2011-09-13
8
Medium Priority
?
589 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 1200 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 132 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 400 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 136 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 35

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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.

885 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