1

I have created a class called "letter":

class Letter
{
    char letter{};
    bool guessed{ false };

    public:
        void setLetter(std::string);
};

and I would like to create an array of class "Letter". The size of the array should be the size of a user inputted string, and if possible, each letter of the string should be stored in 1 element of the letter object. I have tried the following:

std::string word;
Letter test[] = word;
"initialization with {...} expected for aggregate object"
std::string word;
Letter test[word.length()]; // and then iterate through each element of the string
"expression must have a constant value"

EDIT: Solved

std::string word;
std::vector<Letter> test;
for (int i = 0; i < word.length(); i++)
{
    test[i].setLetter(word[i]);
}
7
  • 6
    Prefer std::vector when you need a dynamically sized array. Commented Aug 13, 2020 at 16:22
  • could you use a vector? Commented Aug 13, 2020 at 16:22
  • @GenoC Definitely, I'm just more accustomed to arrays Commented Aug 13, 2020 at 16:23
  • setLetter(std::string) should probably be setLetter(char); Commented Aug 13, 2020 at 16:35
  • The letter member is private. Presumably setLetter is responsible for setting it. But it takes a whole std::string instead of just a letter. It isn't clear how it is intended to work, I can't imagine what it is supposed to do if the input is not exactly 1 letter. Did you mean to use void setLetter(char) instead? Commented Aug 13, 2020 at 16:35

3 Answers 3

3

In modern C++, the tool you should first look to when thinking of a dynamic array is std::vector. Here, you would use a std::vector<Letter>. In this case you could write std::vector<Letter> test(word.length()); to make container that holds word.length() default constructed instances of the class Letter.

std::vector behaves mostly like an array. You can access elements the same way, by using operator[]. For example test[1] access the 2nd element whether test is a Letter[] array or an std::vector<Letter>. It has additional functions which you should familiarize yourself which are listed here.

In cases where the array's size is not dynamic (when it is known at compile time) it is still preferable to use std::array. C style arrays have few use cases in modern C++ and should generally be avoided in favor of newer, safer alternatives.

Sign up to request clarification or add additional context in comments.

1 Comment

"In modern C++, the tool you should first look to when thinking of a dynamic array is std::vector" ... with "modern", you mean past -1994? ;-)
0

As already pointed out, i also would use a vector for a dynamic size. The hard task, in my opinion is to convert the string into an array of Letters. The Letter class should have a constructor wich accepts a char and stores it. Than you could do something like this:

std::vector<Letters> myVector;
for( char current : string )
{
    myVector.emplace_back( current );
}

Comments

0

You could modify your program to use std::vector which would be more preferable since it is much more flexible and easier to use. You could do something like this -

class Letter
{
    char letter{};
    bool guessed{ false };

    public:
        void setLetter(char c){
            letter= c;
        };
        char getLetter(){
            return letter;
        };
};

int main()
{
    int user_input;
    char tmp;
    std::cin>>user_input;
    std::vector<Letter> test(user_input);  // this will create an array which can hold user_input number of objects of given type - Letter
    std::cout<<"enter letters :\n";
    for(int i=0;i<user_input;i++){
        std::cin>>tmp;
        test[i].setLetter(tmp);            // read input letter into each element(which is object of Letter)
    }
    for(int i=0;i<user_input;i++){
        std::cout<<test[i].getLetter();    // output the read letter
    }

    return 0;
} 

INPUT :

4
abcd

OUTPUT :

enter letters :
abcd

  1. Here, you use a vector of Letter in which I am just reading input into each element of the vector( where each element will be an object of Letter class) and then I am just outputting the read value. You could modify the above program to suit your needs.

  2. Another change that I made is in the parameter which setLetter takes in. Since it is setting the letter variable of the object of class Letter, so it should take a character as a parameter and then set the character as the value of the letter. I have just added getLetter which just returns the letter for printing.

  3. If you don't want to use vector, you could go with an array with declaration syntax like -

     Letter* test = new Letter[user_input];
    

    But, in C++, vectors are the preferred way to go and arrays should be avoided since vector are similar to an array with added functionalities and flexibility.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.