Thread: Coding help
View Single Post
Depression Moon
Hypnotic
Depression Moon's Avatar
Location: Lavender Town
#7
Default

my troubles with the software have gone, but there's an issue here that i'm having with cases. I'm supposed to be adding a question to the code below that allows the user to choose if they want to do an addition subtraction, mulitiplication, or division problem.

I got some help from someone else that says that I need something like this in here. I'm wondering where exactly this should be put in the code and where I should replace some text with the math option of my choice.


Code:
#include 

int main(void)
{
  int choice = 2;                 


  if((choice>10) || (choice <1)){
    choice = 11;                 
  }   

  switch(choice)
  {
    case 7:
      printf("\nCongratulations!");
      break;                    

    case 2:
      printf("\nA");
      break;                      

    case 8:
      printf("\nB");
      break;     

    case 11:
      printf("\nC");

    default:
      printf("\nSorry, you lose.\n");
      break;             
  }
Code:
#include 
#include   // For time function
#include  // For rand and srand functions
using namespace std;

int main()
{
char continueLoop = 'Y';
while (continueLoop == 'Y')
{
// Execute body once
long startTime = time(0);
int correctCount = 0; // Count the number of correct answers
int incorrectCount = 0;
int count = 0; // Count the number of questions

while(count < 4)
{
//generate random numbers
    srand(time(0));
    int number1 = rand() % 10;
    int number2 = rand() % 10;

//if number 1 < number 2, swap
    if(number1
    {
        int temp = number1;
        number1 = number2;
        number2 = temp;
    }

//ask question "what is number1 - number2?"
    cout << "What is " << number1 << " - " << number2 << "?";
    int answer;
    cin >> answer;

//see if answer is correct

    if (number1-number2 == answer)
    {
        cout << "You are correct!\n";
        correctCount++;
    }
    else
    {
        cout << "Your answer is wrong.\n" << number1 << " - " <<
        number2 << " should be " << (number1 - number2) << endl;
incorrectCount++;
    }

// Increase the count    
count++;
//ask question "what is number1 - number2?"
    cout << "What is " << number1 << " + " << number2 << "?";
    cin >> answer;

//see if answer is correct

    if (number1+number2 == answer)
    {
        cout << "You are correct!\n";
        correctCount++;
    }
    else
    {
        cout << "Your answer is wrong.\n" << number1 << " + " <<
        number2 << " should be " << (number1 + number2) << endl;
incorrectCount++;
    }

// Increase the count    
count++;
//ask question "what is number1 * number2?"
    cout << "What is " << number1 << " * " << number2 << "?";
    cin >> answer;

//see if answer is correct

    if (number1*number2 == answer)
    {
        cout << "You are correct!\n";
        correctCount++;
    }
    else
    {
        cout << "Your answer is wrong.\n" << number1 << " * " <<
        number2 << " should be " << (number1 * number2) << endl;
incorrectCount++;
    }

// Increase the count    
count++;
//ask question "what is number1 * number2?"
    cout << "What is " << number1 << " / " << number2 << "?";
    cin >> answer;
    if (number1 == 0)
    number1 = 1;
    if (number2 == 0)
        number2 = 1;
//see if answer is correct

    if (number1/number2 == answer)
    {
        cout << "You are correct!\n";
        correctCount++;
    }
    else
    {
        cout << "Your answer is wrong.\n" << number1 << " / " <<
        number2 << " should be " << (number1 / number2) << endl;
        incorrectCount++;
    }

// Increase the count    
count++;
}

long endTime = time(0);
long testTime = endTime - startTime;
double correctPercent = (correctCount * 100) / (correctCount + incorrectCount);
cout << "correct count is " << correctCount << "\n";
cout << "incorrect count is " << incorrectCount << "correctPercent is " << correctPercent << "\nTest time is " << testTime << " seconds\n";
// Prompt the user for confirmation
cout << "Enter Y to continue and N to quit: ";
cin >> continueLoop;
}
system("pause");
return 0;
}

Edit: Take note that i did post the < iostream > etc part. For some reason this site doesn't show them.

Last edited by Depression Moon; 06-19-2009 at 11:01 PM..
Old 06-19-2009, 10:47 PM
Reply With Quote
Depression Moon is offline