Thread: Trouble with my Computer Assistance Instruction Program

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    2

    Trouble with my Computer Assistance Instruction Program

    My output for my assignment is supposed to be something like this:
    What is 10+5=? 7

    Incorrect: 10+5=15

    What is 9-4=? 5

    Correct!

    (etc)

    Your total score is ?

    I have to ask a student for the answer to 10 random arithmetic problems consisting of addition and subtraction using integers from 1 to 10 only. Pick two random integers from 1 to 10 and assign to num1 and num2 and pick a random integer 0 and 1 for the the operation (OP). If OP is 0 it is addition, 1 is subtraction. If it is a subtraction you must make sure that the first number Num1 is bigger than the second number Num2. If it is not then swap the numbers by calling a function Swap that you will define. Each correct problem is awarded 10 points. Display the total score at the end.

    My program isn't doing that, I'm not sure what I'm doing wrong in my code, but here is what I wrote:

    Code:
    //Computer Assisted Instruction Program
    #include<iostream>
    #include<ctime>    
    #include<cstdlib> 
    using namespace std;
    void Swap(int num1, int num2,int temp); 
    int main()
    {
        int num1,num2,correctAns,OP,youAns,score=0;
        for(int Problem=1;Problem<=10;Problem++)
        {
             num1=1+rand()%10; //get first random number
             num2=1+rand()%10; //get second random number
             correctAns=0;
             OP=rand()%2;
        }
           if(OP==0) //Addition
           {
              cout<<"What is"<<num1<<"+"<<num2<<"=";
              cin>>youAns;
              correctAns=num1+num2;
           }
              if(youAns==correctAns)
              {
                  cout<<"Correct!"<<endl;
                  score+=10; //Add 10 pts to score 
              }
              else
                cout<<"Incorrect"<<num1<<"+"<<num2<<"="<<correctAns<<endl;
              
              if(OP==1) ///Subtraction)
              {
                cout<<"What is"<<num1<<"-"<<num2<<"=";
                cin>>youAns;
                correctAns=num1-num2;
              }
              
              if(youAns==correctAns)
              {
                  cout<<"Correct!"<<endl;
                  score+=10; //Add 10 pts to score
              }
              else
               cout<<"Incorrect"<<num1<<"-"<<num2<<"="<<correctAns<<endl;
                 
     }
     
     void Swap(int num1, int num2, int temp)
    {
        for(int Problem=1;Problem<=10;Problem++)
        {
            if(num1<num2)
              {
                  int temp;
                  temp=num1;
                  num1=num2;
                  num2=temp;
              }
        }
    }


  2. #2
    Guest
    Guest
    I haven't looked at it in detail, but is it possible that your for-loop closes too early?
    Code:
        for(int Problem=1;Problem<=10;Problem++)
        {
             num1=1+rand()%10; //get first random number
             num2=1+rand()%10; //get second random number
             correctAns=0;
             OP=rand()%2;
        } // <<<<<<<<<<<<<<<<<<<<<<<<<<<< THE FOR-LOOP ENDS HERE
           if(OP==0) //Addition
           {
              cout<<"What is"<<num1<<"+"<<num2<<"=";
              cin>>youAns;
              correctAns=num1+num2;
           }
    Although it comes down to personal taste, I would suggest you use spaces between operands and operators.

    Basic for-loops also usually take the form of counting upwards from 0 and use the < comparator, which gives the same number of iterations as your 1 and <= approach.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    2
    Yea, I just fixed that, I just realized that I'm missing using the Swap function in my program, I need something like this:
    Code:
    if(num1 < num2)
    Swap(num1,num2);
    correctAns=num1-num2; // because of the above num1 will always be > num2
    But I don't know where to place it in my program.

  4. #4
    Guest
    Guest
    How about placing the swap function inside the subtraction branch before computing correctAns?

    You swap function is seriously flawed by the way. You take in a temp argument from the outside (why?) and you run the code inside 10 times (a swap of two variables is a single "operation").

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not to mention any changes to the variables inside the function will not propagate outside as the inputs are just copies and not references.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Computer assisted Instruction undeclared identifier error
    By programBeginner in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2011, 02:31 PM
  2. Replies: 15
    Last Post: 11-27-2009, 07:09 AM
  3. INT n instruction in C Program
    By raghu2383 in forum C Programming
    Replies: 5
    Last Post: 08-24-2008, 03:08 AM
  4. Illegal instruction problem with my program
    By bigtruckguy3500 in forum C Programming
    Replies: 3
    Last Post: 04-16-2007, 11:29 PM
  5. Replies: 1
    Last Post: 07-12-2002, 09:45 PM

Tags for this Thread