views:

140

answers:

2

I'm working on a program for class that takes in a number from 0 to 9999, and spits out the word value (ie 13 would be spit out as "thirteen", etc) And I'm having a pain with the array for some reason.

Here is the class so far:

#include<iostream>
#include<string>

using namespace std;

class Numbers
{
    private:

        int number;

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

    public:
        Numbers(int newNumber)
        {
            setNumber(newNumber);
        }

        void setNumber(int newNumber)
        {
            if(newNumber < 0 || newNumber > 9999)
            {
                cout << "Number cannot be negative or larger than 9999, defaulted to zero." << endl;
                number = 0;
            }
            else
            {
                number = newNumber;
            }
        }

        int getNumber()
        {
            return number;
        }
};

My issue is up at the two string arrays, I'm getting these errors back from my compiler:

1>f:\college work\csis 297\assignment 4\chapter 11-1\chapter 11-1\11-1.cpp(19) : error C2059: syntax error : '{'
1>f:\college work\csis 297\assignment 4\chapter 11-1\chapter 11-1\11-1.cpp(19) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>f:\college work\csis 297\assignment 4\chapter 11-1\chapter 11-1\11-1.cpp(22) : error C2059: syntax error : '{'
1>f:\college work\csis 297\assignment 4\chapter 11-1\chapter 11-1\11-1.cpp(22) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>f:\college work\csis 297\assignment 4\chapter 11-1\chapter 11-1\11-1.cpp(24) : error C2864: 'Numbers::hundred' : only static const integral data members can be initialized within a class
1>f:\college work\csis 297\assignment 4\chapter 11-1\chapter 11-1\11-1.cpp(25) : error C2864: 'Numbers::thousand' : only static const integral data members can be initialized within a class

I'm sure if I spend more time on the single variable strings I can figure those out, but I've spent the better part of an hour looking up what I'm doing wrong on the two arrays, any advice or information would be appreciated.

Thanks in advance.

PS: No, there is no main, haven't gotten there yet, my program has a blank main right now as I'm simply trying to get the class to compile error free for now.

EDIT for clarification:

The first two errors are on the first array, the second two errors on the second array, and the last two errors are on the non-array strings.

+5  A: 

The error message says it all:

only static const integral data members can be initialized within a class

You cannot do what you want, you have to separate declaration and initialization, and move the initialization either to the constructor, or use a static const, and put the initialization outside the class. This is preferred, since the values don’t actually change, or depend on an instance:

class Numbers
{
    private:
        static const string lessThan20[];
        // …
};

string const Numbers::lessThan20[ ] = {"zero", "one", "two", "three", "four", "five", 
            "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", 
            "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

And analogously for the other array.

Konrad Rudolph
Slightly confused...you say initialize inside the constructor? I've never done that before nor am I familiar on how that works, would you explain please?
Jeff
Other note, that error is only on the two non-array strings, the first four errors are for the two arrays (two errors per array)
Jeff
After tinkering around looks like it reads it outside the class. Kind of strange, if you wouldn't mind sending my a message on how this works I'd appreciate it, thanks again for the assist!
Jeff
@Jeff: Data has to be defined outside a class because class declarations go into header files. The data should be in a source file, not a header file. The data should end up in one and only one "object file." If it was in the header, it could end up copied into every object file, wasting a lot of space. It is the same with functions. All functions defined in a class are automatically "inline" and you can end up with several copies of their object code in a large project.
Zan Lynx
+1  A: 

I would make your arrays be arrays of const char*. Then use std::string to build your result strings. The C++ string class accepts char* for almost every operation.

A string literal like "this is a string" is a const char*. There is no real need to make it into a string.

And oh yes, as the other answer says, you must define the values of your arrays outside of the class definition.

Zan Lynx