-1

After calling function the amount_of_people, the variable n remains unchanged. I verified this by outputting the variable after the function call. Do I need a pointer n to function as argument?

int main(){
srand(time(NULL));
bool Appworks = true;
size_t n;
    do {
        amount_of_people(n); // Entering amount of people HERE! STUCKED HERE.
        if (n >= 1) {
            DataBase *first = new DataBase[n]; // Creating dynamic structure-array
            inputData(first, n);

            output(first, n); // Output of entered data

            freeUp_memory(first); // Clearing dynamic-alocated memory engaged by early-created pointer


        }
        else cout << "Error! Wrong amount of people!" << endl;

    } while (Appworks);
system("PAUSE");
return 0;
}

Function declaring:

unsigned amount_of_people(int n) {
    cout << "Enter how many people u want to enter" << endl;
    cin >> n;
    return n;
}

I would appreciate any help and explanation(!) Thanks for your attention.

TheGame142
  • 15
  • 2
  • Thank you for posting a question. Please include a main() function and provide a [Compilable, Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve). This helps us help you. If you can make your problem as simple as possible while still creating the failure, it will help us isolate the issues that will make your code work correctly. – Gardener Mar 21 '19 at 00:36
  • @Gardener have done! – Michael Bazyshyn Mar 21 '19 at 00:39
  • Not being funny but this should be pretty well covered by https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list! Which are you using to learn C++? – Lightness Races in Orbit Mar 21 '19 at 01:48

2 Answers2

1
amount_of_people(n)

does not use the value returned from amount_of_people. n is of no use here because according to the function declaration

unsigned amount_of_people(int n);

n is passed by value. When a parameter is passed by value, the function operates on a copy of the source variable. Changing the copy has no effect on the original. May I suggest instead,

std::size_t amount_of_people() // parameter gone, return type changed to match n in caller
{
    std::size_t n; // parameter moved to here and type changed to match return type
    std::cout << "Enter how many people u want to enter" << std::endl;
    std::cin >> n;
    return n;
}

This is then used like

const std::size_t n = amount_of_people();

Side note: Rather than

DataBase *first = new DataBase[n];

strongly consider ensuring that DataBase correctly observes the Rule of Three, Five, or Zero and using

std::vector<DataBase> databases;

rather than a raw allocation. It knows it's size and looks after all of the memory management for you. Documentation for std::vector.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Not sure why you changed it to `size_t` but if you insist on that then it should be `std::size_t`. Also the `n` in `main` should be `const`. – Lightness Races in Orbit Mar 21 '19 at 01:47
  • @LightnessRacesinOrbit Asker's `main` defines `n` as `size_t` Then it was passed to an `int` and returned as `unsigned`. Figured I'd sort that all out and go with the initial declaration. The odds of `inputData` and `output` taking `n` as a non-`const` reference are slight, so I'll borg that suggestion as well. – user4581301 Mar 21 '19 at 02:55
  • Ah, so they did. Then that makes sense :) Yep, it's better to use `const` when you can so that you do not accidentally modify variables that do not need to be modified - helps prevent bugs! It should have been the default in the language :P – Lightness Races in Orbit Mar 21 '19 at 11:06
0

RE "Do I need a pointer to function as argument", either a pointer or a reference.

unsigned amount_of_people(int n) as you wrote it takes an integer n by value, assigns it using cin >> n, and then returns it. Either change your function to

void amount_of_people(unsigned int& n) {
    std::cout << "Enter how many people u want to enter" << endl;
    std::cin >> n;
}

and call it:

amount_of_people(n);

which takes n by reference, or write

unsigned int amount_of_people() {
    unsigned int n;
    std::cout << "Enter how many people u want to enter" << endl;
    std::cin >> n;
    return n;
}

and call it:

n = amount_of_people();

Both styles have uses; the first is, I think, more common in cases where the function has side effects, and so it "outputs" its results into the ref-passed parameters ("out parameters"), while you opt to return from the function a variable indicating whether an error occurred during its execution. The second style is a little more common for pure functions, where the result is always computed successfully based on the inputs with no possibility of error.

Also, make up your mind whether you want the variable to be a size_t or an int.

jcarpenter2
  • 5,312
  • 4
  • 22
  • 49