problem with loop
Mar 18, 2012 at 7:32am UTC
hi everyone,
i'm writing a demo program to teach myself how to use arrays of classes, but i'm having problems moving onto the next element of the array. the problem occurs after the function "again" is called, if i select 'y' to enter more information, it outputs all the prompts very quickly without a chance to input, then quits.
here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <iostream>
using namespace std;
class magic
{
private :
char cardName[31];
char cardType[20];
char cardInstruct[200];
int cardCost;
int cardPower;
int cardTough;
public :
void cardEntry();
};
bool again();
int main()
{
int i = 0;
magic aCard[10];
cout << "Welcome to the Magic Database." << endl;
do
{
aCard[i].cardEntry();
++i;
}while (again());
return 0;
}
bool again()
{
char choice;
cout << "\n\nWould you like to enter another? Y/N" << endl;
cin >> choice;
if (tolower(choice) == 'y' )
return true ;
return false ;
}
void magic::cardEntry()
{
cout << "\n\nPlease enter the name of the card: " << endl;
cin.get(cardName, 31, '\n' );
cin.ignore(100, '\n' );
cout << "\n\nPlease enter the card type: " << endl;
cin.get(cardType, 20, '\n' );
cin.ignore(100, '\n' );
cout << "\n\nPlease enter the body of the card: " << endl;
cin.get(cardInstruct, 200, '\n' );
cin.ignore(100, '\n' );
cout << "\n\nPlease enter CCM: " << endl;
cin >> cardCost;
cin.ignore(100, '\n' );
if (tolower(cardType[0]) == 'c' )
{
cout << "\n\nPlease enter the creature's power: " << endl;
cin >> cardPower;
cin.ignore(100, '\n' );
cout << "\n\n\nPlease enter the creature's toughness: " << endl;
cin >> cardTough;
cin.ignore(100, '\n' );
}
}
Topic archived. No new replies allowed.