string array, data member initializer not allowed

Pages: 12
I'm getting a few errors, however I'm not sure what's going on.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
1>Build started 4/6/2012 1:55:15 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\Mallous_e5c1.unsuccessfulbuild".
1>ClCompile:
1>  Mallous_e5c1.cpp
1>c:\users\demetri\documents\visual studio 2010\projects\mallous_e5c1\mallous_e5c1\mallous_e5c1.cpp(19): error C2059: syntax error : '{'
1>c:\users\demetri\documents\visual studio 2010\projects\mallous_e5c1\mallous_e5c1\mallous_e5c1.cpp(19): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>c:\users\demetri\documents\visual studio 2010\projects\mallous_e5c1\mallous_e5c1\mallous_e5c1.cpp(21): error C2864: 'Numbers::hundred' : only static const integral data members can be initialized within a class
1>c:\users\demetri\documents\visual studio 2010\projects\mallous_e5c1\mallous_e5c1\mallous_e5c1.cpp(22): error C2864: 'Numbers::thousand' : only static const integral data members can be initialized within a class
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.04
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



Here's my code:

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
/*
This is a class that can translate whole dollar amounts in the range 0 through 9999 
into an English description of the number.
***********
Designed by: Demetri Mallous
*/

#include <iostream>
#include <string>
using namespace std;

//class Numbers
class Numbers
{
   //single int member variable
   int number;

   //static string members
   string lessThan20[20] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
      "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
   string hundred = "hundred";
   string thousand = "thousand";
};

// main function
int main()
{
   int inputNum;
   cout << "Enter number: ";
   cin >> inputNum;
   cout << "\n";
   
   system("PAUSE");

   return 0;
}
You can't do this
string hundred = "hundred";

you can give variable value only in constructor, not in class. In class you can give value only to static int variables.
Ok, I created a constructor, and it compiles fine. What I'm trying to do is convert an int to English description of the number, for example 713 would be seven hundred thirteen. Stuck on this one.

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
51
52
/*
This is a class that can translate whole dollar amounts in the range 0 through 9999 
into an English description of the number.
***********
Designed by: Demetri Mallous
*/

#include <iostream>
#include <string>
using namespace std;

//class Numbers
class Numbers
{
private:
		//single int member variable
		int number;

public:
		//constructor
		Numbers()
		{
			//static string members
			string lessThan20[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
				"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
			string hundred = "hundred";
			string thousand = "thousand";
		}

		//print function
		void print(int inputNum)
		{
			cout << inputNum;
		}

};

// main function
int main()
{
	Numbers object; 
	
	int inputNum;
	cout << "Enter number: ";
	cin >> inputNum;
	object.print(inputNum);
	cout << "\n";
	
	system("PAUSE");

	return 0;
}
Last edited on
What I'm thinking is

have a function that returns number of digits

then write some if statements depending on number of digits, like
1
2
if (inputNum == 0)
cout << lessThan20[0]



My question is, what function can return number of digits? And I can't access anything in the constructor, says it's undefined.

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
51
52
53
54
55
56
57
58
59
/*
This is a class that can translate whole dollar amounts in the range 0 through 9999 
into an English description of the number.
***********
Designed by: Demetri Mallous
*/

#include <iostream>
#include <string>
using namespace std;

//class Numbers
class Numbers
{
private:
		//single int member variable
		int number;

public:
		//constructor
		Numbers()
		{
			//static string members
			string lessThan20[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
				"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
			string hundred = "hundred";
			string thousand = "thousand";
		}


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

		void setNum(int inputNum)
		{
			if (inputNum = 0)
				cout << lessThan20[0];
		}
};


// main function
int main()
{
	Numbers object; 
	
	int inputNum;
	cout << "Enter number: ";
	cin >> inputNum;
	object.print(inputNum);
	cout << "\n";
	
	system("PAUSE");

	return 0;
}
closed account (zb0S216C)
demetri90 wrote:
1
2
3
4
5
6
7
Numbers()
{
    //static string members
    string lessThan20[] = {/*...*/};
    string hundred = "hundred";
    string thousand = "thousand";
}

A few things:

1) These objects are not part of the class. They are local objects that will fail to exist when the constructor terminates. Regardless, they are never used, even if they were members. So effectively, these objects are useless, and have no significant use in your code.

2) The objects are not static - implicitly or explicitly.

Edit: Solution:

To make the local objects part of your class, you'll need to do this:

1
2
3
4
5
6
7
8
class Something
{
    public:
        Something() : A(0), B(0) { } //A

    private:
        int A, B;
};

In this code, on the line marked by A, the code between the colon and the opening brace is called the initialiser list. It's used to give all members an initial value before the constructor begins.

Wazzak
Last edited on
How about setting it up like this?

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
This is a class that can translate whole dollar amounts in the range 0 through 9999 
into an English description of the number.
***********
Designed by: Demetri Mallous
*/

#include <iostream>
#include <string>
using namespace std;

//class Numbers
class Numbers
{
private:


public:		
	
		//single int member variable
		int number;

		//constructor
		Numbers()
		{

		}


		//print function, converts value from numOfDigits to string, then prints
		void print()
		{			
			//static string members
			static string lessThan20[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
				"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
			static string hundred = "hundred";
			static string thousand = "thousand";

		}


		//Determines how many digits there are in a number and returns the value
		int numOfDigits(int number)
		{
			return number;
		}
};


// main function
int main()
{
	Numbers object; 

	cout << "Enter number: ";
	cin >> object.number;

	object.numOfDigits(object.number);
	object.print();

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

	return 0;
}
closed account (zb0S216C)
That would work. Though, the objects would only be accessible when Numbers::print() has been called, and only accessible to Numbers::print(). I would suggest that you declare all of those objects constant if your intentions are only to read them.

Wazzak
Is there a function native to C++ that returns number of digits in an int?
closed account (zb0S216C)
Not as far as I know of. Sometimes, the CSL doesn't have what you're looking, so you're forced to implement your own. From the top of my head, I would suggest something like this:

1
2
3
4
5
6
7
8
9
10
11
12
std::string Number("123");

int Number_of_digits()
{
    if(!Number.length()) return(-1);

    int Digit_count(0);
    for(unsigned int I(0); I < Number.length(); ++I)
         if(isdigit(Number.at(I))) ++Digit_count;

    return(Digit_count);
}


Wazzak
Last edited on
Plugging that code into my own, I'm getting a few errors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
1>Build started 4/7/2012 3:43:52 PM.
1>InitializeBuildStatus:
1>  Creating "Debug\Mallous_e5c1.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  Mallous_e5c1.cpp
1>c:\users\demetri\documents\visual studio 2010\projects\mallous_e5c1\mallous_e5c1\mallous_e5c1.cpp(46): error C2228: left of '.length' must have class/struct/union
1>          type is 'int'
1>c:\users\demetri\documents\visual studio 2010\projects\mallous_e5c1\mallous_e5c1\mallous_e5c1.cpp(50): error C2228: left of '.length' must have class/struct/union
1>          type is 'int'
1>c:\users\demetri\documents\visual studio 2010\projects\mallous_e5c1\mallous_e5c1\mallous_e5c1.cpp(51): error C2109: subscript requires array or pointer type
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.91
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
This is a class that can translate whole dollar amounts in the range 0 through 9999 
into an English description of the number.
***********
Designed by: Demetri Mallous
*/

#include <iostream>
#include <string>
#include <math.h>
using namespace std;

//class Numbers
class Numbers
{
private:


public:		
	
		//single int member variable
		int number;

		//constructor
		Numbers()
		{

		}


		//print function, converts value from numOfDigits to string, then prints
		void print()
		{			
			//static string members
			const static string lessThan20[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
				"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
			const static string hundred = "hundred";
			const static string thousand = "thousand";

		}


		//Determines how many digits there are in a number and returns the value
		int numOfDigits()
		{
			    if(!number.length()) return(-1);

				int Digit_count(0);

				for(unsigned int I(0); I < number.length(); ++I)
					if(isdigit(number[I])) ++Digit_count;

			return(Digit_count);
		}
};


// main function
int main()
{
	Numbers object; 

	cout << "Enter number: ";
	cin >> object.number;

	object.numOfDigits();
	object.print();

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

	return 0;
}
closed account (zb0S216C)
The compiler assumes that ::Numbers::number is either a class, union, or struct. An int is none of these. The code I posted relied on the pre-defined std::string object, Number. You'll have to change the type of ::Numbers::number to std::string for the function to work.

Wazzak
I got it working, however there should be a better way for printing the code than all these if statements, must be some kind of loop I can use.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
This is a class that can translate whole dollar amounts in the range 0 through 9999 
into an English description of the number.
***********
Designed by: Demetri Mallous
*/

#include <iostream>
#include <string>
using namespace std;

//class Numbers
class Numbers
{
private:


public:		
	
		//single member variable
		string number;

		//print function, gets return value from numOfDigits, then prints accordingly
		void print(string number)
		{			
			//static string members
			const static string lessThan20[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
				"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
			const static string hundred = "hundred";
			const static string thousand = "thousand";
			
			if (numOfDigits() == 1)
			{
				if (number == "0")
					cout << lessThan20[0];
				if (number == "1")
					cout << lessThan20[1];
				if (number == "2")
					cout << lessThan20[2];
				if (number == "3")
					cout << lessThan20[3];
				if (number == "4")
					cout << lessThan20[4];
				if (number == "5")
					cout << lessThan20[5];
				if (number == "6")
					cout << lessThan20[6];
				if (number == "7")
					cout << lessThan20[7];
				if (number == "8")
					cout << lessThan20[8];
				if (number == "9")
					cout << lessThan20[9];
			}

		}

		//Determines how many digits there are in a number and returns the value
		int numOfDigits()
		{
			    if(!number.length()) return(-1);

				int Digit_count(0);

				for(unsigned int I(0); I < number.length(); ++I)
					if(isdigit(number[I])) ++Digit_count;

			return(Digit_count);
		}
};


// main function
int main()
{
	Numbers object; 

	cout << "Enter number: ";
	cin >> object.number;


	object.print(object.number);

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

	return 0;
}
closed account (zb0S216C)
There is a way: std::atoi(). This function converts a null-terminated string into an integer. This can be applied in this way:

1
2
3
std::string number("2");
const static std::string lessThan20[] = { "zero", "one", "two", "three", "four", "five"};
std::cout << lessThan20[std::atoi(number.c_str())] << '\n';


Wazzak
Awesome it works, now to just figure out how to print twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, and then concatenate the rest
Still kind of stumped on how I should print the twenty, thirty, forty, etc. I guess the long way is to continue the array all the way up to 99, but doesn't sound right, and too much work. Any ideas?
Number in numbers: 325612425
Number in words: three hundred twenty five million six hundred twelve thousand four hundred twenty five

Do you see repetition?
1
2
3
three hundred twenty five million
six hundred twelve thousand
four hundred twenty five


This means you need to make a function to write hundred
three hundred twenty five
six hundred twelve
four hundred twenty
And use it three times.

Well I will give you my class hider :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class NumbersToWords
{
	public:
		NumbersToWords(int);
		~NumbersToWords() {}
		string words(int number_in = -1);
		
	private:
		int number;
		string result;
		const string words_array[32];
		void hundreds(int);
	
};
Not exactly sure what repetition I'm supposed to be looking for. But this is what I've came up with so far. Now it's just trying to concatenate the hundred and thousand.

The way I have it now doesn't work, in the if statements, it's trying to find the element in the array, but it doesn't exist. I guess the way it works now, is that it goes by the array in order, for example 5 will print the 5th element in the array which is five. I'm not sure how I should fix this issue.

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
This is a class that can translate whole dollar amounts in the range 0 through 9999 
into an English description of the number.
***********
Designed by: Demetri Mallous
*/

#include <iostream>
#include <string>
using namespace std;

//class Numbers
class Numbers
{
public:		
	
		//single member variable
		string number;

		//print function, gets return value from numOfDigits, then prints accordingly
		void print()
		{			
			//static string members
			const static string lessThan20[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
				"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy"
				"eighty", "ninety" };
			const static string hundred = "hundred";
			const static string thousand = "thousand";
			if (numOfDigits() == 2 || numOfDigits() == 1)
			{
				cout << lessThan20[atoi(number.c_str())] << '\n';
			}
			if (number == "30")
				cout << lessThan20[21];
			if (number == "40")
				cout << lessThan20[22];
			if (number == "50")
				cout << lessThan20[23];
			if (number == "60")
				cout << lessThan20[24];
			if (number == "70")
				cout << lessThan20[25];
			if (number == "80")
				cout << lessThan20[26];
			if (number == "90")
				cout << lessThan20[27];

			if (numOfDigits() == 3)
			{
				cout << lessThan20[atoi(number.c_str())] << hundred << '\n';
			}

			if (numOfDigits() == 4)
			{
				cout << lessThan20[atoi(number.c_str())] << thousand << '\n';
			}
			

		}

		//Determines how many digits there are in a number and returns the value
		int numOfDigits()
		{
			    if(!number.length()) return(-1);

				int Digit_count(0);

				for(unsigned int I(0); I < number.length(); ++I)
					if(isdigit(number[I])) ++Digit_count;

			return(Digit_count);
		}
};


// main function
int main()
{
	Numbers object; 

	cout << "Enter number: ";
	cin >> object.number;


	object.print();

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

	return 0;
}
:X

Did you looked in to my hider file?

Your program is completely wrong. If your variable user inputs is of type string, you will have a lot, a lot of coding to finish it.

repetitions are this 325612425 --> 325.612.425

three hundred twenty five million six hundred twelve thousand four hundred twenty five -->
1
2
3
three hundred twenty five million
six hundred twelve thousand
four hundred twenty five

or
three hundred twenty five

six hundred twelve

four hundred twenty five

It is all hundreds. So you need to split number to hundreds parts and analyze them in function hundreds. For that you need variable number to be of type int.

So in my example are two functions.
1) words(int number_in = -1);
this function splits number to hundred parts and gives this parts to second function. Splitting is maid by dividing number of 1, 1000 or 1000000.

2) void hundreds(int);
this function analyzes a number given to it and prints (adds to variable result) numbers in words. To it numbers are given between 0 and 999.

This functions analyzes numbers by using recursion.
Last edited on
Alright, my program is crap. Sounds like I need to start over and approach this differently. This is for an assignment that's due on the 20th so I should have plenty of time. Just wish my instructor wouldn't take two days to answer his e-mails. Here is the challenge from the book:

Design a class Numbers that can be used to translate whole dollar amounts in the range 0 through 9999 into an English description of the number. For example, the number 713 would be translated into the string seven hundred thirteen, and 8203 would be translated into eight thousand two hundred three.

The class should have a single integer member variable

int number;

and a collection of static string members that specify how to translate key dollar amounts into the desired format. For example, you might use static string such as

1
2
3
4
			string lessThan20[] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", 
				"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
			 string hundred = "hundred";
			 thousand = "thousand";


The class should have a constructor that accepts a non negative integer and uses it to initialize the Numbers object. It should have a member function print() that prints the English description of the Numbers object. Demonstrate the class by writing a main program that asks the user to enter a number in the proper range and then prints outs its English description.
Pages: 12