Results 1 to 10 of 10

Thread: Coding help

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Depression Moon's Avatar
    Join Date
    Aug 2007
    Location
    Warrior Falls
    Posts
    6,050
    Articles
    45
    Blog Entries
    2
    Contributions
    • Former Editor

    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 <stdio.h>
    
    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 <iostream><iostream>
    #include <ctime> <ctime> // For time function
    #include<cstdlib> <cstdlib> // 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<number2)>
        {
            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.
    </number2)></cstdlib></ctime></iostream></stdio.h>
    Last edited by Depression Moon; 06-19-2009 at 10:01 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •