Hello, I'm a newbie at programming and this example is taken from the book <<C++ Without Fear>>.
The problem is that the programs runs continuously, as though the "number of cards to draw" is infinite, even when I entered a finite number.
Code:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;
int rand0ton1(int n);
void draw_a_card();
char *suits[4] = {"hearts","diamonds","spades","clubs"};
char *ranks[13] = {"ace","two","three","four","five","six","seven","eight","nine","ten","jack","queen","king"};
int main() {
int n, i;
srand(time(NULL));
while (1) {
cout << "Enter number of cards to draw (0 to quit): ";
cin >> n;
if (n==0)
break;
for (i=1;1<=n;i++)
draw_a_card();
}
return 0;
}
void draw_a_card() {
int r;
int s;
r = rand0ton1(13);
s = rand0ton1(4);
cout << ranks[r] << "of" << suits[s] << endl;
}
int rand0ton1(int n) {
return rand()%n;
}