-1

I am trying to implement random_function(), which outputs the amount of random numbers within a range (user input), and count_function() to count for the value that the user wants to look up in the outputted random numbers. I made the random numbers to be saved in an array called randomlist[] so that it can be used to count for the value from the outputted random numbers. However, I cannot declare an array without its fixed size in the header file and define its size in the count_function.cpp. So I cannot use randomlist[] in the count_function() without redefining it (which is meaningless...).
Is there a way to declare array without defining its size, in the header file?

Here is the source code:

header file:
#pragma once

using namespace std;

class Rand
{
  public:
    // Rand();
    int range,min,max;
    char key;
    void Random_function();
    void Count_function();
    // randomlist[]; something like this, which that is not possible in c++
};
This is randomfunction.cpp:
#include <iostream>
#include "random_function.hpp"


using namespace std;

void Rand::Random_function()
{
    beginning:
    
    cout << "Enter amount of numbers to generate: ";
    cin >> range;
    
    if (range<=0 || cin.fail())
    {
        cout <<"Error! Please enter valid number and try again! "<<endl;
        goto beginning;
    }

    else
    {
     reenter:

        
        cout << "Enter minimum boundary: ";
        cin >> min;
        cout << "Enter maximum boundary: ";
        cin >> max;
        cout << "\n";
        
        srand((unsigned)time(NULL));
    
        if(max<min || cin.fail())
            {
                cout << "\nError! Please enter the valid value and try again! " << endl;
                goto reenter;
            }
        
        else
            {
              int randomlist[range]; //I defined the size of the randomlist here but I want to use this outside of the random_fucntion() as well. 
                
              for(int i=0 ; i < range; i++)
              {
                  
                randomlist[i] = min + (rand() % static_cast<int>(max - min + 1));
              
                cout <<i+1<<". "<< randomlist[i] <<endl;
              
                
            }
    
    }
        
        cout <<"\n"<< "Total random numbers generated: " << range<< endl;
  
    
      cout <<"Do you want to continue? y/n"<<endl;
      cin >> key;
    
    if(key=='y')
    {
            Count_function();
        
            cout <<"Do you want to restart? y/n"<<endl;
            cin >> key;
            if(key=='y')
            {
                goto beginning;
            }
            else
            {
                exit(0);
            }

    }
    
    else
    {
        cout <<"Do you want to restart? y/n"<<endl;
        cin >> key;
        
        if(key=='y')
        {
                goto beginning;
        }
            
        else
        {
            exit(0);
        }
     
    }
  }   
    
}


void Rand::Count_function()
{
    
    int n,count=0;
    
    reenter2:
    
    cout<<"Enter the value to count for: ";
    cin>>n;
    
if(cin.fail())
{
    cout<<"Please enter valid value to count for"<<endl;
    goto reenter2;
}
    
else
{
    
    for(int i=0 ; i <range; i++)
    {
        if(randomlist[i]==n)
        {
            count++;
        }
    }
}
    cout <<"The number of '"<<n<<"'s in the given list is: "<< count <<endl;
}
main:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <random>
#include "random_function.hpp"

using namespace std;

int main()
{
    Rand call;
    call.Random_function();
}
Chris
  • 26,361
  • 5
  • 21
  • 42
noob
  • 3
  • 3
  • 7
    Have a look at std::vector (int randomlist[range] isn't valid C++), also for random number generation use the types from – Pepijn Kramer Feb 04 '22 at 05:26
  • 6
    Don't use `goto`. – heap underrun Feb 04 '22 at 05:27
  • 2
    The short answer, in C++, is that you don't. Unlike C, standard C++ does not support VLAs (an array with a size determined at run time) - and, although some C++ compilers support VLAs as a non-standard extension, there are significant restrictions on their use (such as the fact they cannot "grow" based on how they are used). In C++, the recommended approach is to use `std::vector` (in standard header ``) which is a templated class that manages a dynamically allocated (and resizeable) array. – Peter Feb 04 '22 at 05:46
  • @heapunderrun: I strongly disagree with the general statement that `goto` should not be used. However, I do agree that it should not be used in this case, as there is a better alternative, such as an infinite loop and `continue` and `break` statements. There are situations where using `goto` is appropriate, though. [What is wrong with using goto?](https://stackoverflow.com/q/3517726/12149471) – Andreas Wenzel Feb 04 '22 at 06:06

1 Answers1

0

When you want to use an array, but the size cannot be known at compile-time, the generally accepted approach in C++ is to use a std::vector.

Chris
  • 26,361
  • 5
  • 21
  • 42