// Zeinalipour-Yazti Demetrios // csyiazti // cs10xx - Lab Sec#25-#29 // lab8 /* This program defines : * a) A function that will randomly generate two operands ranging between 1-1000. * This function modifes the operands through reference * b) A function that takes as parameters, the two operands and will output the * question nicely to the screen. Each question should line up with future * outputted questions at the addition operator and the equal sign. * c) A function that will take as a parameter an integer representing some number * of questions and generate that many test questions. * Then the program will allow the user to specify some number of questions and * then will show that many addition questions nicely on the screen. */ #include #include #include using namespace std; /* This function assigns a random number in range (from,to) to the * parameters passed by reference * @return : void */ void assignRand(int &start, int &end, int from, int to) { /* e.g (2,4) ==> range=3 [3 numbers] */ int range = to-from+1; // produce twice a random number from <= random <= to start = (rand() % range) + from; end = (rand() % range) + from; } /* This function prints 1 equation. * @return : void */ void printEquation(int start, int end) { cout << setw(5) << setiosflags(ios::left) << start << " + " << setw(5) << setiosflags(ios::left) << end << " = " << endl; } /* This function prints N equations * @return : void */ void print_N_Equationa(int N) { int start; // the first random int end; // the second random void assignRand(int &start, int &end, int from, int to); void printEquation(int start, int end); // iterate N times for (int i=0; i> val; while (cin.fail()) { cout << "#ERROR - Only integer values are allowed!\n"; cin.clear(); cin.ignore(80, '\n'); cout << "Please enter the number of equations to create: "; cin >> val; } return val; } /* This function asks the user to select the char "y" or "n" * @return : y for yes * n for no */ char doAgain() { /* defining local scope variables*/ char value; cout << "\nDo you want to run the program again (y/n) ?: "; cin >> value; while (cin.fail() || !(value=='y' || value=='n')) { cout << "#Error - only (y/n) are accepted!\n"; cin.clear(); cin.ignore(80, '\n'); cout << "Do you want to run the program again (y/n) ? : "; cin >> value; } cout << endl; return value; } void main() { /* define variables */ int x; /* the equations to create per round */ /* declare functions & procedures*/ int getUserTimes(); /* get a correct int value from user */ char doAgain(); /* returns 'y' or 'n' to the question whether to continue */ cout << "Random Equation Generator by Demetris Zeinalipour - Lab 9 \n\n"; // run the program as many times as the user wants do { // get the input from user in a function-fashion way x = getUserTimes(); // ask the user how many equations to create /* print x Equations */ print_N_Equationa(x); } while (doAgain()=='y'); }