Soully
Soully

Reputation: 849

String array in C++ not working properly?

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.

Upvotes: 2

Views: 1886

Answers (2)

Zan Lynx
Zan Lynx

Reputation: 54325

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.

Upvotes: 1

Konrad Rudolph
Konrad Rudolph

Reputation: 545588

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.

Upvotes: 5

Related Questions