/* File: subtractionTutorLoop.cpp Modified example from ch. 4. Uses nested while loops: The outer loop is an eof-controlled while loop that prompts the user for the number of questions to ask. The inner loop conducts a single quiz. */ #include #include // for time() #include // for the srand(), rand() using namespace std; int main(){ int correctCount, // number of correct answers count = 0, // Count the number of questions noOfQues, // number of questions on quiz answer; // user's answer for a quiz question long startTime, endTime, // quiz timer variables elapsedTime; //prompt and read number of quiz questions cout << "enter number of questions for quiz (c-z to exit): "; cin >> noOfQues; //driver loop while(cin){ //initialze quiz counters count = 0; correctCount = 0; long startTime = time(0); cout << endl; //execute one quiz & display stats while (count < noOfQues){ // 1. Generate two random single-digit integers srand(time(0)); int number1 = rand() % 10; int number2 = rand() % 10; // 2. If number1 < number2, swap number1 with number2 if (number1 < number2){ int temp = number1; number1 = number2; number2 = temp; } // 3. Prompt the student to answer “what is number1 – number2?” cout << "What is " << number1 << " - " << number2 << "? "; cin >> answer; // 4. Grade the answer and display the result if (number1 - number2 == answer) { cout << "You are correct!\n"; correctCount++; } else cout << "Your answer is wrong.\n" << number1 << " - " << number2 << " should be " << (number1 - number2) << endl; // Increase the count count++; } //display quiz stats endTime = time(0); elapsedTime = endTime - startTime; cout << "Correct count is " << correctCount << "\nTest time is " << elapsedTime << " seconds\n" << endl; //prompt and read number of quiz questions cout << "enter number of questions for quiz (c-z to exit): "; cin >> noOfQues; } //exit program return 0; }