All pastes #719589 Raw Edit

Text-based dices

public cpp v1 · immutable
#719589 ·published 2007-09-29 12:51 UTC
rendered paste body
#include <iostream>#include <iomanip>#include <stdlib.h>#include <time.h>#include <string>#include <sstream>using namespace std;const string numbers = "0123456789";int string_to_int(string s) {	int n;	istringstream iss(s);	iss >> n;	return n;}int main() {	string roll;	string::size_type count_end;	string::size_type die_end;	string::size_type d;    	int count;	int die;	int sum;	int value;		srand(time(0));    	while(true) {			cout << "Select die: ";		cin >> roll;		count_end = roll.find_first_not_of(numbers, 0);		if (count_end == string::npos || count_end == 0) {			continue;		}		d = roll.find_first_not_of('d', count_end + 1);		if (d == string::npos || d != count_end + 1) {			continue;		}		die_end = roll.find_first_not_of(numbers, d + 1);		if (die_end != string::npos) {			continue;		}		count = string_to_int(roll.substr(0, count_end));		die = string_to_int(roll.substr(d, roll.length() - d)); 		if (count <= 0 || die <= 0) {			continue;		}							cout << "You roll: ";		sum = 0;		for (int i = 0; i < count; i++) {			value = rand() % die + 1;			sum += value;			cout << value << " "; 		}				cout << "(sum " << sum << ")" << endl << endl;			}	return 0;}