1

I'm making a program which need to take user input in command line. Unfortunately my program does not want to take user input and do nothing and misses "cin".

#include <iostream>
#include <cstdlib>
#define n 1000000

using namespace std;

class drzewko
{
public:
    int tab[n];
    int size;
    drzewko();
    void dodaj_pracownika(int number=1);
    void wyswietl(int poczatek);
    void sort();
};


int main(int argc, char *argv[])
{
    ios::sync_with_stdio(true);
    int bajtalary, liczba_pracownikow, pensja, numer_pracownika = 0;
    cin >> liczba_pracownikow;
    drzewko P;

    for (int i=1; i < liczba_pracownikow; i++)
    {
        cin >> pensja >> bajtalary;
        if (pensja == liczba_pracownikow)
        {
            P.dodaj_pracownika(numer_pracownika = 1);
        }
        else if (pensja != liczba_pracownikow)
            {
                P.dodaj_pracownika(numer_pracownika++);
            }
    }

    P.wyswietl(1);

    return EXIT_SUCCESS;
}

drzewko::drzewko()
{
    size = 0;
}

void drzewko::dodaj_pracownika(int number)
{
    tab[size + 1] = number;
    int s = size + 1;
    while (s != 1)
    {
        if (tab[s / 2] < tab[s])
        {
            swap(tab[s / 2], tab[s]);
            s /= 2;
        }
        else
            break;
    }
    size++;
}

void drzewko::wyswietl(int poczatek){
    if(poczatek <= size) {
        cout << poczatek << " : " <<  tab[poczatek] << " \n";
        if(poczatek*2 <= size) wyswietl(poczatek*2);
        if(poczatek*2+1 <= size) wyswietl(poczatek*2+1);
    }
}

There is screenshot what happens in command line:

enter image description here

user438383
  • 5,716
  • 8
  • 28
  • 43
Buunyk
  • 11
  • 3
  • 1
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Eljay Apr 27 '22 at 10:52
  • 1
    A million ints is likely 4MB, quite a lot to allocate on stack. Could be that your program is crashing because of (nomen omen) stack overflow. – Yksisarvinen Apr 27 '22 at 10:53
  • 2
    Because `n` is very large `sizeof(drzewko)` is around 4 megabytes. Probably too large to fit on the stack. You could make it a global variable to bypass that problem or use a `std::vector`. – Retired Ninja Apr 27 '22 at 10:54
  • Thanks to all of you. It helped :D – Buunyk Apr 27 '22 at 12:58

0 Answers0