Thread: Crashing and Wroung data

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    5

    Unhappy Crashing and Wroung data

    So what is happening is that I run this code and when I run the divide function it will sometimes crash or say that I entered a wrong answer when it’s correct. I know I need to check option while loop but I'm not too concerned with that at the moment. Please check the divide function and tell me what I need to revise.


    Code:
    bool check_flat(int x, int y) //checks that two numbers divide evenly to one another
    {
        int int_check = x/y;
        double check_x = x;
        double check_y = y;
        double double_check = check_x/check_y;
    
        if (int_check < double_check)
            return (false);
        else
            return (true);
    }
    
    
    double dividing (int x, int y) //dividing fuction (this is giving me problems)
    {
        bool flat = false;
        int first_number;
        int second_number;
        int greater_number;
        int lesser_number;
        while (flat == false) //when I mean flat I mean the two numbers divide evenly to one another, no decimals
        {
            first_number = rand() % (x - y + 1);
            second_number = rand() % (x - y + 1);
    
            if (first_number > second_number) //checks to find the bigger of the two numbers and then set one to greater_number and one to lesser_number
            {
                greater_number = first_number;
                lesser_number = second_number;
            }
    
            else
            {
                greater_number = second_number;
                lesser_number = first_number;
            }
    
            flat = check_flat(greater_number, lesser_number); //calls check_flat function
        }
    
    
    int main()
    {
        time_t seconds;
        time(&seconds);
        srand((unsigned int) seconds);
    
        int max;
        int min;
        int option;
        int difficulty;
    
        cout << "Math practice";
        cout << "What would you like to practice? \n"
             << "1. Adding \n"
             << "2. Subtracting \n"
             << "3. Multiplying \n"
             << "4. Dividing \n";
        cin >> option; //type of math problems
    
        cout << "Difficulty? \n"
            << "1. Very Easy (1-5) \n"
            << "2. Easy (1-10) \n"
            << "3. Medium (5-15) \n"
            << "4. Difficult (1-25) \n"
            << "5. Very Difficult (1-50) \n"
            << "6. Extremely Difficult (1-100) \n";
    
        cin >> difficulty; //range of numbers
    
        while ((difficulty < 1)||(difficulty > 6)) //checking difficulty
        {
            cout << "Enter a difficulty between 1 through 6 \n"
                << "1. Very Easy (1-5) \n"
                << "2. Easy (1-10) \n"
                << "3. Medium (5-15) \n"
                << "4. Difficult (1-25) \n"
                << "5. Very Difficult (1-50) \n"
                << "6. Extremely Difficult (1-100) \n";
    
            cin >> difficulty;
        }
    
        switch(difficulty) //sets max and min randoms to be generated
        {
             case 1: min = 1; max = 5; break;
             case 2: min = 1; max = 10; break;
             case 3: min = 5; max = 15; break;
             case 4: min = 1; max = 25; break;
             case 5: min = 1; max = 50; break;
             case 6: min = 1; max = 100; break;
        }
    
        for(int i = 10; i >= 1; i--) //generates 10 problems at a time
        {
            switch (option)
            {
               case 1: adding(min, max);break; //calls adding fuctions
               case 2: subtracting(min, max);break; //calls subtracting fuctions
               case 3: multiplying(min, max);break; //calls multiplying fuction
               case 4: dividing(min, max);break; //calls dividing fuction
            }
        }
    
        system("PAUSE"); //pauses system
    }
    So what is happening is that I run this code and when I run the divide function it will sometimes crash or say that I entered a wrong answer when it’s correct. I know I need to check option while loop but I'm not too concerned with that at the moment. Please check the divide function and tell me what I need to revise.

    Thank you,

    ShadowIII
    Last edited by ShadowIII; 11-28-2004 at 08:36 PM.

  2. #2
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    Sorry about posting all my code a first. I didn't read all of the rules. I apologize. I hope this is slim enough.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    first_number = rand() % (x - y + 1);
    second_number = rand() % (x - y + 1);
    x-y+1 will always be negative since x was min and y was max in main() and min - max < - 1 in every case available in switch(difficulty). I believe using negative numbers with % results in undefined behaviour which would account for the pattern of your problem quite nicely. Try making it y - x + 1, instead.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by ShadowIII
    So what is happening is that I run this code and when I run the divide function it will sometimes crash or say that I entered a wrong answer when it’s correct. I know I need to check option while loop but I'm not too concerned with that at the moment. Please check the divide function and tell me what I need to revise.


    So what is happening is that I run this code and when I run the divide function it will sometimes crash or say that I entered a wrong answer when it’s correct. I know I need to check option while loop but I'm not too concerned with that at the moment. Please check the divide function and tell me what I need to revise.

    Thank you,

    ShadowIII
    the % operator returns values of 0 to something.

    Put a printout statement that shows the numbers that you are using:

    Code:
     
            first_number = rand() % (x - y + 1);
            second_number = rand() % (x - y + 1);
            cout << "first_number = " << first_number << ", second_number = " 
                 << second_number << endl;
    check_flat() divides one number by the other. If one of them is zero, ....

    Regards,

    Dave
    Last edited by Dave Evans; 11-29-2004 at 09:41 AM.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    5
    Yay it works thank you.

    You were right about the dividing by zero. so I changed it to this.

    Code:
            first_number = rand() % (y - x) + 1;//no more zeros occure
            second_number = rand() % (y - x) + 1;
    The reason I was getting wroung answers even when they were right.

    Code:
        if (answer == (first_number / second_number)) //Sometimes the second number was great than the first
            cout << "Correct \n";
        else
            cout << "Wroung \n";
    I changed it to this

    Code:
        if (answer == (greater_number / lesser_number))
            cout << "Correct \n";
        else
            cout << "Wroung \n";
    Thank you very much!

Popular pages Recent additions subscribe to a feed