Dit probleem is opgelost. ik heb de code zo herschreven:
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
const unsigned int MAXIMUM_CARDS ( 100 );
struct single_card
{
string question;
string answer;
};
class vocabulary_cards
{
private:
single_card stack_of_cards [ MAXIMUM_CARDS ];
unsigned int total_elements_used;
public:
vocabulary_cards ( const single_card list [ ],
unsigned int list_size );
single_card & get_card ( );
void right ( );
void wrong ( );
bool done ( );
void dump_data ( );
};
vocabulary_cards::vocabulary_cards ( const single_card list [ ],
unsigned int list_size )
{
assert ( list_size > 0 || list_size < MAXIMUM_CARDS );
unsigned int index;
for ( index = 0; index < list_size; ++index )
stack_of_cards [ index ] = list [ index ];
total_elements_used = list_size;
}
single_card & vocabulary_cards::get_card ( )
{
return ( stack_of_cards [ 0 ] );
}
void vocabulary_cards::right ( )
{
unsigned int index;
for ( index = 0; index < total_elements_used - 1; ++index )
stack_of_cards [ index ] = stack_of_cards [ index + 1 ];
--total_elements_used;
}
void vocabulary_cards::wrong ( )
{
unsigned int index;
single_card element_to_backup;
element_to_backup = stack_of_cards [ 0 ];
for ( index = 0; index < total_elements_used - 1; ++index )
stack_of_cards [ index ] = stack_of_cards [ index + 1 ];
stack_of_cards [ total_elements_used - 1 ].question = element_to_backup.question;
stack_of_cards [ total_elements_used - 1 ].answer = element_to_backup.answer;
}
bool vocabulary_cards::done ( )
{
if ( total_elements_used == 0 )
return true;
else
return false;
}
void vocabulary_cards::dump_data ( )
{
unsigned int index;
cout << "Data dump of an instance of class \"vocabulary_cards\" \n"
<< "\tType\t\tName\t\t\t\tValue \n";
for ( index = 0; index < total_elements_used; ++index )
cout << "\tC++ string\tstack_of_cards [ " << index << " ].question\t"
<< stack_of_cards [ index ].question << '\n'
<< "\tC++ string\tstack_of_cards [ " << index << " ].answer\t"
<< stack_of_cards [ index ].answer << '\n';
cout << '\n';
}
int main ( )
{
single_card questions_answers [ 5 ] = {
{ "A car", "Une voiture" },
{ "A cat", "Un chat" },
{ "An airplane", "Une avion" },
{ "A dog", "Un chien" },
{ "A glass of water", "Un verre d'eau" }
};
vocabulary_cards test ( questions_answers, 5 );
single_card current_card;
string user_input;
cout << "Welcome to the vocabulary learning program! \n\n"
<< "Answer with the French word of the given word: \n";
while ( true )
{
current_card = test.get_card ( );
cout << current_card.question << '\n';
A
getline ( cin, user_input );
if ( ! test.done ( ) )
{
if ( current_card.answer.compare ( user_input ) != 0 )
test.wrong ( );
else
test.right ( );
}
else
break;
}
cin.get ( );
return ( 0 );
}
Nu werkt het wel. Het knelpunt is dus gewoon dat je niet de grootte van een referentie kunt krijgen met de sizeof functie.