string array, data member initializer not allowed

Pages: 12
Firs you need to find an algorithm to do it. As I said all numbers are made from of parts of hundreds. So your algorithm first must split given number to this parts. And then analyze this parts separately.

I use variable string result; to store words of numbers, but if you can have only one variable of type int, you can print words in program execution not store them.

P.S. here you will find complete code of program. http://cplusplus.shinigami.lt/ analyze it and maybe then will be more clearly.
I tried compiling your code in visual studio 2010, but it wouldn't, said there were a few errors. What compiler are you using?

But back to my program, I'm having trouble with the part "The class should have a constructor that accepts a non negative integer and uses it to initialize the Numbers object."

I don't know how to pass a variable to the constructor.

Here's my current code so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
using namespace std;


class Numbers
{
private:
		//int member variable
		int number;
public:
		//prototypes
		void print();
		Numbers();
		Numbers(int x);

};

	//static string words
	const static string words[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
		"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "fourty", "fivety",
		"sixty", "seventy", "eighty", "ninety", "hundred", "thousand" };

	//constructor
	Numbers::Numbers(int x)
	{				
		number = x;
	}

	//print function
	void Numbers::print()
	{
		cout << number;
	}

int main()
{
	Numbers object; 
	int userInput;
	cout << "Enter number 0-9999: ";
	cin >> userInput;
	object.Numbers(userInput);
	object.print();


	cout << endl;
	system("PAUSE");

	return 0;
}
Last edited on
I am on linux and use g++ and Geany. It compiles my code perfectly.

And for your code.
Numbers(int x);
is a constructor and you can't use constructor like thus.
object.Numbers(userInput);

You have two choices.
1) Delete line 38 Numbers object; and change line 42 to Numbers object(userInput);
2) Make a method to add user input to object. Like void add(int x);
and use object.add(userInput);

P.S. what errors you get by compiling my code? Is it in constructor?
Ok, took option 1 and it compiles now.

As far as your code, the parenthesis is giving an error, ((string[32]), and the brackets for the array.

1
2
3
4
5
6
7
NumbersToWords::NumbersToWords(int number_in) :
number(number_in),
words_array((string[32]){"zero ", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine ", "ten ",
        "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen ", "twenty ",
        "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety ", "hundred ", "thousand ", "million ", "billion "})
{
}
Hey, I took some pointers from the code and so forth, his class was pretty nice. I made the adjustments needed to get it to work in visual studio, gcc and such aren't as strict.


First add this to either private/public in your class.
static const string words_array[32];

Then, down here, just do some separation and pretty much just copy/paste the words_array I put in here.
1
2
3
4
5
6
Numbers::Numbers(int number_in) :
number(number_in){}

const string Numbers::words_array[32] = {"zero ", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine ", "ten ",
        "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen ", "twenty ",
        "thirty ", "forty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety ", "hundred ","thousand ", "million ", "billion "};



Also in that class Shingami had in here, there was a bug that 100 didn't display anything at all, I added another if statement to fix it.
1
2
3
4
5
6
7
8
9
10
11
    if (number_in == 100)
	{
		int temp = number_in / 100;
        hundreds(temp);
        result += words_array[28];
        hundreds(number_in - temp * 100);
	}
    else if (number_in < 1000)
    {
        hundreds(number_in);
    }


That's pretty much it for now, I had to do the same thing you did, and this was pretty handy other than trying to get it to work with visual studio.
@Zachary Knight
thank you for finding a bug :D , I could not test all numbers so left some bugs.

I fixed it like this

1
2
3
4
5
else if (number_in == 100)
	{
		result += words_array[1];
		result += words_array[28];
	}


in function hundreds(int number_in)

now output is
Enter number from 0 to 1000.000.000: 1100
one thousand one hundred
Topic archived. No new replies allowed.
Pages: 12