Ok mensen.. Dit wordt een slapeloze nacht. Ik probeer hier mbv een randomgetallen generator een telraampje te realiseren in de vorm van een int array. Eerst wil ik de compiler zover krijgen dat hij me laat zien hoe vaak een bepaald getal is geworpen in cijfers.
Ik krijg geen foutmeldingen, maar wel onjuiste waarden en zelfs een skip naar de reset functie. Zou iemand me alsjeblieft kunnen zeggen wat ik niet goed doe?
Ik heb in de lidfuncties references geprobeerd en returns, maar die willen niet werken
...help
// M.P. Holtrop
#include <iostream.h>
#include <conio.h> // _getch()
#include <stdlib.h> // srand()
#include <time.h> // time()
class Frequentietabel {
private:
int i;
public:
void dice( int & number ) { // reference argument voor rn
srand ( time(NULL) ); // initialiseer random seed mbv de tijd
number = rand() % 11; // genereer random nummer mbv de seed
}
void reset( int p[], int max ) { // resetten van de array
for ( i = 0; i < max; i++ ) {
p[ i ] = 0;
}
}
void add_mark( int p[], int num ) { // element array "aanturven"
p[ num ] = p[ num ] +1;
}
void print( int p[], int max ) { // toon huidige lijst
for ( i = 0; i < max; i++ ) {
cout << p[ i ] << endl;
}
}
}; // einde specificatie
int main() {
Frequentietabel table;
char choice;
const int MAX = 10; // vast aantal elementen
int a[MAX], value;
cout << "Dit programma genereert, op jouw aangeven, een willekeurig getal" << endl;
cout << "van de cijfers 0 t/m 10 en turft deze aan in een frequentietabel." << endl << endl;
cout << "Druk op" << endl << endl;
cout << "---------------------------------------" << endl;
cout << "W voor een worp" << endl;
cout << "R voor het resetten van de tabel" << endl;
cout << "S voor het stoppen van het programma" << endl;
cout << "---------------------------------------" << endl << endl;
do {
choice = getch();
if ( choice == 'W' || choice == 'w' )
table.dice( value );
table.add_mark( a, value );
table.print( a, MAX );
if ( choice == 'R' || choice == 'r' )
table.reset( a, MAX );
cout << "De tabel is gereset" << endl << endl;
if ( choice == 'S' || choice == 's' )
cout << "Je hebt op S gedrukt: het programma stopt" << endl;
} while ( choice != 'S' && choice != 's' );
cout << endl << "Druk op Enter";
cin.get();
return 0;
}
Ik krijg geen foutmeldingen, maar wel onjuiste waarden en zelfs een skip naar de reset functie. Zou iemand me alsjeblieft kunnen zeggen wat ik niet goed doe?
Ik heb in de lidfuncties references geprobeerd en returns, maar die willen niet werken
// M.P. Holtrop
#include <iostream.h>
#include <conio.h> // _getch()
#include <stdlib.h> // srand()
#include <time.h> // time()
class Frequentietabel {
private:
int i;
public:
void dice( int & number ) { // reference argument voor rn
srand ( time(NULL) ); // initialiseer random seed mbv de tijd
number = rand() % 11; // genereer random nummer mbv de seed
}
void reset( int p[], int max ) { // resetten van de array
for ( i = 0; i < max; i++ ) {
p[ i ] = 0;
}
}
void add_mark( int p[], int num ) { // element array "aanturven"
p[ num ] = p[ num ] +1;
}
void print( int p[], int max ) { // toon huidige lijst
for ( i = 0; i < max; i++ ) {
cout << p[ i ] << endl;
}
}
}; // einde specificatie
int main() {
Frequentietabel table;
char choice;
const int MAX = 10; // vast aantal elementen
int a[MAX], value;
cout << "Dit programma genereert, op jouw aangeven, een willekeurig getal" << endl;
cout << "van de cijfers 0 t/m 10 en turft deze aan in een frequentietabel." << endl << endl;
cout << "Druk op" << endl << endl;
cout << "---------------------------------------" << endl;
cout << "W voor een worp" << endl;
cout << "R voor het resetten van de tabel" << endl;
cout << "S voor het stoppen van het programma" << endl;
cout << "---------------------------------------" << endl << endl;
do {
choice = getch();
if ( choice == 'W' || choice == 'w' )
table.dice( value );
table.add_mark( a, value );
table.print( a, MAX );
if ( choice == 'R' || choice == 'r' )
table.reset( a, MAX );
cout << "De tabel is gereset" << endl << endl;
if ( choice == 'S' || choice == 's' )
cout << "Je hebt op S gedrukt: het programma stopt" << endl;
} while ( choice != 'S' && choice != 's' );
cout << endl << "Druk op Enter";
cin.get();
return 0;
}