0

I need to create an array with the size that a random number generator creates. Any help would be appreciated!

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(NULL));
    int parameter;
    char newArray[] = {};
    parameter = rand() % 10;
    newArray[parameter] = {};

    for (int i = 0; i < parameter; i++)
    {
        cout << "Enter a character: ";
        cin >> newArray[i];
    }
    for (int i = 0; i < parameter; i++)
    {
        cout << newArray[i];
        return (0);
    }
}
3
  • 4
    In C++, an array of dynamically determined size is spelled std::vector Commented Apr 26, 2020 at 0:44
  • 1
    std::vector and .push_back() would be the way to go. Commented Apr 26, 2020 at 0:45
  • Or std::string Commented Apr 26, 2020 at 0:51

3 Answers 3

1

You can use a std::string

#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>

int main() {
    std::srand(std::time(nullptr));
    int parameter = std::rand() % 10;
    std::string newArray(parameter, '\0');

    for (auto &c : newArray) {
        std::cout << "Enter a character: "; std::cin >> c;
    }
    std::cout << newArray;

    return 0;
}

or a std::vector

#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <ctime>

int main() {
    std::srand(std::time(nullptr));
    int parameter = std::rand() % 10;
    std::vector<char> newArray(parameter);

    for (auto &c : newArray) {
        std::cout << "Enter a character: "; std::cin >> c;
    }
    for (auto c : newArray) {
        std::cout << c;
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think he can also just declare newArray after the random number and keep it as a char newArray[parameter] = new char[parameter] and keep the code the same.
@OmidCompSCI Do you mean a variable-length array char newArray[parameter]; or dynamic memory allocation char *newArray = new char[parameter]? Your example char newArray[parameter] = new char[parameter] causes a compile error. Many compilers don't support VLAs and dynamic memory allocation is a complex topic in C++. You have to remember to clean up. Strings and vectors are the most simple ways to achieve this.
1

The intuitive thing to do is to use std::string, after all, we want an array of characters:

#include <iostream>
#include <ctime>

int main()
{
    srand(time(NULL));
    int parameter;
    char c;

    std::string newArray; 

    parameter = rand() % 10 + 1; // you'd want to avoid 0 

    while(parameter > 0)
    {
        std::cout << "Enter a character: ";
        std::cin >> c;
        //ignore extra characters
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        newArray.push_back(c);
        parameter--;
    }
    std::cout << newArray;
}

Comments

1
#include <iostream>
#include <memory>

int main()
{
  std::srand(time(nullptr));
  int count = (rand() % 10) + 1;
  std::unique_ptr<char[]> newArray(new char[count + 1]{});

  std::cout << "Count: " << count << std::endl;

  for (int i=0; i<count; i++) {
    std::cout << "Enter character " << i << ": "; 
    std::cin >> newArray[i];
  }

  std::cout << newArray.get() << std::endl;

  return 0;
}

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.