How do I repeat a question to the user? I don't want to ask an identical question I want to repeat the same one.
I have written my code so it will ask a question, wait for a responce, determine if that response is correct or incorrect. If the response is correct the code will move on. If the response is incorrect the code should repeat the question until the user gets it right.
Up until this point I thought I had it figured out but I will eventurally be asking randomly generated questions and if the user gets one of those wrong I will need to repeat that particular question until they get if correct.
line 81 is the problem area.
Bonus: Do you think that lines 97, 104, 111, and 118 are necessary?
//
// main.cpp
// Weekly Assignment #2
//
// Created by Joshua Smith on 9/18/12.
// Copyright (c) 2012 Joshua Smith. All rights reserved.
//
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ctime>
#include <cmath>
#include <fstream>
usingnamespace std;
double correctAnswer = 0;
double incorrectAnswer = 0;
float percentCorrect;
int level;
double x;
double y;
int mathType;
int addition =1;
int subtraction = 2;
int multiplication = 3;
int division = 4;
int all = 5;
int main(int argc, constchar * argv[])
{
//upon recieving a desired level program shall take the student to funciton 1 or function 2
cout << "What difficulty level would you like to try?\n" ;
cout << "Enter 1 for easy problems or 2 for harder problems.\n";
cin >> level;
cout << "Ok, you've chosen level " << level << " lets get started.\n" << endl;
cout << "What type of math problem would you like to work on?\n Press 1 for Addition Problems\n Press 2 for Subtraction problems\n Press 3 for Multiplication problems\n Press 4 for Division problems only\n Press 5 for a random mixture of all types" << endl;;
cin >> mathType;
for(int counter = 1; counter<=5; counter++)
{
srand( (unsigned)time(0) );//randomize the randomizer with time as the seed
if (level == 1) // if user wants easy questions
{
x = rand() % 9 + 1;
y = rand() % 9 + 1;
}
elseif (level == 2) // if user wants harder questions
{
x = rand() % 99 + 1;
y = rand() % 99 + 1;
}
elseif (level == 3){
x = rand() % 999 + 1;
y = rand() % 999 + 1;
}
double answer;//student's answer input
int sum = x + y;
int difference = x - y;
int product = x * y;
int quotient = x / y;
if( mathType == 1){
char cstring[] = " plus ";
cout << "What is " << x << cstring << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
cin >> answer;//users answer to be checked and compared with computer's product
correctAnswer = sum;
}
elseif (mathType == 2){
char cstring[] = " minus ";
cout << "What is " << x << " cstring " << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
cin >> answer;//users answer to be checked and compared with computer's product
correctAnswer = difference;
}
elseif (mathType == 3){
char cstring[] = " times ";
cout << "What is " << x << " cstring " << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
cin >> answer;//users answer to be checked and compared with computer's product
correctAnswer = product;
}
elseif (mathType == 4){
char cstring[] = " divided by ";
cout << "What is " << x << " cstring by " << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
cin >> answer;//users answer to be checked and compared with computer's product
correctAnswer = quotient;
}
elseif (mathType == 5){
char cstring[] = " plus ";
cout << "What is " << x << " cstring " << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
cin >> answer;//users answer to be checked and compared with computer's product
}
if (answer == correctAnswer)//checking to see correctness of user's answer
{
++correctAnswer;
int goodStatement = rand() % 3 +1;
switch (goodStatement)
{
case 1:
puts("Very good!");
break;
case 2:
puts("Excellent");
break;
case 3:
puts("Nice work!");
break;
case 4:
puts("Keep up the good work");
break;
default:
puts("Default good");
break;
}
}
elsewhile (answer!= correctAnswer)
{//while loop shall run with the same x and y values being presented. The program will not move forward until a correct answer is entered by user.
++incorrectAnswer;
int badStatement = rand() % 3 +1;
switch (badStatement)
{
case 1:
puts("No. Please try again");
break;
case 2:
puts("Wrong. Try once more");
break;
case 3:
puts("Don't give up");
break;
case 4:
puts("No. Keep trying");
break;
default:
puts("This is the default no");
break;
}
//This should repeat the question that was answered incorrectly.
//change operands to a string that reads out the mathmatical operation
cout << "What is " << x << cstring << y << "?" << endl;
cin >> answer;
if (answer == correctAnswer)//checking to see correctness of user's answer
{
++correctAnswer;
int goodStatement = rand() % 3 +1;
switch (goodStatement)
{
case 1:
puts("Very good!");
break;
case 2:
puts("Excellent");
break;
case 3:
puts("Nice work!");
break;
case 4:
puts("Keep up the good work");
break;
default:
puts("Default good");
break;
}
}
}
}
//This is messed up. The math is wrong and needs to be fixed.
cout << "You got " << correctAnswer <<" of them correct. Great work!" << endl;
cout << "You missed " << incorrectAnswer << "." << endl;
percentCorrect = (correctAnswer / correctAnswer + incorrectAnswer)*100;
cout << percentCorrect << (setprecision(2)) << "%" << endl;
if (percentCorrect < .75) {
cout << "Please ask you teacher for extra help." << endl;
} else {
cout << "Congratulations, you are ready to go the next level!" << endl;
}
}