-1

for example,

    #include <iostream>
    #include <string>
    int main ()
{
    int count;
    std::cout << "How many things do you hear?";
    std::cin >> count;
    heard std::string[count];
    for (int i = 0; i < count; i++)
    {
    std::cout << "Enter what you hear: ";
    std::cin << heard[i];
    if (**//heard[i] letter count > 100**) //How could I make this happen
    {
    cout << "Too many letters try again: ";
    }
}

P.S. I know it's a really stupid example, but thank you in advance!

  • 1
    If you want to read more then one string, then use std::vector and push_back. In the end you can just ask the vector how many strings it contains. For a std::string you can count the number of letters by calling its size() method. https://en.cppreference.com/w/cpp/string/basic_string/size – Pepijn Kramer Oct 20 '22 at 12:09
  • 1
    `heard std::string[count];` is not standard C++ [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Use `std::vector` for dynamically sized arrays – 463035818_is_not_an_ai Oct 20 '22 at 12:11
  • total number of letters in all strings? just add them up `sum += heard[i].size();` – 463035818_is_not_an_ai Oct 20 '22 at 12:11
  • 1
    Not only is this not standard C++, @463035818_is_not_a_number, it is incomprehensible gobbledygook. The shown code is not real code, but obviously fake code. – Sam Varshavchik Oct 20 '22 at 12:25
  • This would indeed give multiple error messages before we get to the line with the question. E.g. `std::cin << heard[i];` is the wrong way around, the data goes from std::cin to `heard[i]` : `std::cin >> heard[i];` – MSalters Oct 20 '22 at 12:47

1 Answers1

-1

Looks like you are trying to make an array with a variable "count" at runtime, which isn't possible. You should consider using std::vectors. See this answer:

https://stackoverflow.com/a/8271791/20080198

gabriel__
  • 20
  • 4
  • If an answer is nothing more than a link to another answer, you should vote to close as a duplicate. Your text is also not technically correct, as at least gcc and clang support VLAs as an extension, making their use completely possible. VLAs are not standard C++ and should not be used; note the subtle differences. You also don't address their actual question, which itself needs clarification. Do they just want to count characters, or really just letters like they say? – sweenish Oct 20 '22 at 12:56