Thread: help me simplify my switch statement

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    308

    help me simplify my switch statement

    i think i can make it cleaner but i dont know how;

    Code:
    void test(int &value_1, int &value_2, int x)
    {
        switch(x)
        {
    
    
        case 0  :
            cout << "\nif weight out between 1 and 5\n";
    
            cout << "\nEnter a number here for test 1; 0 = false, 1 = true: ";
            cin >> value_1;
            break;
        case 1  :
            cout << "\nif added weight out between 1 and 0\n";
    
            cout << "\nEnter a number here for test 1; 0 = false, 1 = true: ";
            cin >> value_1;
            if(value_1 == 0)
            {
                cout << "\nfix?\n";
    
                cout << "\nEnter a number here for test 2; 0 = false, 1 = true: ";
                cin >> value_2;
            }
    
            break;
        case 2  :
            cout << "\nif weight is continued to goal weight starting from previous last weight";
    
            cout << "\nEnter a number here for test 1; 0 = false, 1 = true: ";
            cin >> value_1;
            if(value_1 == 0)
            {
                cout << "\nfix?\n";
    
                cout << "\nEnter a number here for test 2; 0 = false, 1 = true: ";
                cin >> value_2;
            }
    
            break;
        case 3  :
            cout << "\nif weight in between weight out and 1";
    
            cout << "\nEnter a number here for test 1; 0 = false, 1 = true: ";
            cin >> value_1;
            break;
        case 4  :
            cout << "\nif added weight out between 1 and 0";
    
            cout << "\nEnter a number here for test 1; 0 = false, 1 = true: ";
            cin >> value_1;
            if(value_1 == 0)
            {
                cout << "\nfix?\n";
    
                cout << "\nEnter a number here for test 2; 0 = false, 1 = true: ";
                cin >> value_2;
            }
            break;
        case 5  :
            cout << "\nif weight is continued to goal weight starting from previous last weight";
    
            cout << "\nEnter a number here for test 1; 0 = false, 1 = true: ";
            cin >> value_1;
            if(value_1 == 0)
            {
                cout << "\nfix?\n";
    
                cout << "\nEnter a number here for test 2; 0 = false, 1 = true: ";
                cin >> value_2;
            }
            break;
            }
    
    
    }
    edit i have classes for each case already made, so i will make a function in each class with the lines in each case then just post the function name in the switch statement. so i fixed my own problem.
    Last edited by jeremy duncan; 06-01-2019 at 03:19 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would move this into a function:
    Code:
            cout << "\nEnter a number here for test 1; 0 = false, 1 = true: ";
            cin >> value_1;
    Then I would write another function that calls that function along with:
    Code:
            if(value_1 == 0)
            {
                cout << "\nfix?\n";
     
                cout << "\nEnter a number here for test 2; 0 = false, 1 = true: ";
                cin >> value_2;
            }
    With these two functions you would be able to eliminate a great deal of your current code duplication.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    heres my code so far, notice the comments in the switch case is the code is still need to make.

    Code:
    #include <iostream>
    #include "one.h"
    #include "two.h"
    #include "three.h"
    #include "four.h"
    #include "five.h"
    #include "six.h"
    
    using namespace std;
    
    void test(int &value_1, int &value_2, int x)
    {
        int value_3[6] = { 0, 0, 0, 0, 0, 0 };
        int value_4[6] = { 0, 0, 0, 0, 0, 0 };
    
        // if weight out between 1 and 5
        one obj1 {value_3[0]};
    
        // if added weight out between 1 and 0
        two obj2 {obj1.get_data_value(),
        value_3[1],
        value_4[1]};
    
        // if weight is continued to goal weight starting from previous last weight
        three obj3 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        value_3[2],
        value_4[2]};
    
        // if weight in between weight out and 1
        four obj4 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        value_3[3]};
    
        // if added weight out between 1 and 0
        five obj5 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        obj4.get_data_value(),
        value_3[4],
        value_4[4]};
    
        // if weight is continued to goal weight starting from previous last weight
        six obj6 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        obj4.get_data_value(),
        obj5.get_data_value(),
        obj5.get_second_data_value(),
        value_3[5],
        value_4[5]};
    
        switch(x)
        {
    
        case 0  :
            obj1.switch_zero(value_1);
    
            break;
        case 1  :
            obj2.switch_zero(value_1, value_2);
            if(value_2 == 1)
            {
                // loop from case 1 to case 4
                // if loop stops somewhere between case 1 and case 4 continue loop to case 4
                // while recording that the loop stopped at case 2 or 3
            }
            break;
        case 2  :
            obj3.switch_zero(value_1, value_2);
            if(value_1 == 1)
            {
                // stay in case position to finish incrementing loop while also continuing on to next case
                // then go to previous case and test if value_2 == 1 for n number of repeated loops in case 2.
                // if previous case value_2 == 1 for n number of repeated loops = high percentage
                // then case 1 is aware of case 2.
            }
            break;
        case 3  :
            obj4.switch_zero(value_1);
            if(value_1 == 0)
            {
                // closes previous case which stops case 2 from completing its loop.
                // if case 1 is going to case 4
                // if case 2  testing case 1 had a high percentage, then prevent case 1 from going to case 4
            }
            break;
        case 4  :
            obj5.switch_zero(value_1, value_2);
            if(value_2 == 1)
            {
                // loop from case 4 to case 1
                // if loop stops somewhere between case 4 and case 1 continue loop to case 2
                // while recording that the loop stopped at case 5 or 1
            }
            break;
        case 5  :
            obj6.switch_zero(value_1, value_2);
            if(value_1 == 1)
            {
                // stay in case position to finish incrementing loop while also continuing on to next case
                // then go to previous case and test if value_2 == 1 for n number of repeated loops in case 2.
                // if previous case value_2 == 1 for n number of repeated loops = high percentage
                // then case 4 is aware of case 5.
                // if case 4 aware of case 5, then case 5 true = true, even if its true becomes false.
                // else if case 5 true becomes false, go to case 0 true
            }
            break;
            }
    }
    
    int main() {
    
        int value_1[6] = { 0, 0, 0, 0, 0, 0 };
        int value_2[6] = { 0, 0, 0, 0, 0, 0 };
    
        // if weight out between 1 and 5
        one obj1 {value_1[0]};
    
        // if added weight out between 1 and 0
        two obj2 {obj1.get_data_value(),
        value_1[1],
        value_2[1]};
    
        // if weight is continued to goal weight starting from previous last weight
        three obj3 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        value_1[2],
        value_2[2]};
    
        // if weight in between weight out and 1
        four obj4 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        value_1[3]};
    
        // if added weight out between 1 and 0
        five obj5 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        obj4.get_data_value(),
        value_1[4],
        value_2[4]};
    
        // if weight is continued to goal weight starting from previous last weight
        six obj6 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        obj4.get_data_value(),
        obj5.get_data_value(),
        obj5.get_second_data_value(),
        value_1[5],
        value_2[5]};
    
    
    
        for(int x = 0; x < 6; ++x)
        {
            test(value_1[x], value_2[x], x);
    
            if( (x == 0) && (value_1[0] == 1) )
            {
                // if weight out between 1 and 5
    
            }
            else if( (x == 1) && ( (value_1[1] == 1) || (value_2[1] == 1) ) && (value_1[0] == 1)  ) //
            {
                // if added weight out between 1 and 0
    
            }
            else if( (x == 2) && ( (value_1[1] == 1) || (value_2[1] == 1) ) )
            {
                // if weight is continued to goal weight starting from previous last weight
    
            }
            else if( (x == 3) && (value_1[3] == 1)  )
            {
                // if weight in between weight out and 1
    
            }
            else if( (x == 4) && ( (value_1[4] == 1) || (value_2[4] == 1) ) && (value_1[3] == 1) )
            {
                // if added weight out between 1 and 0
    
            }
            else if( (x == 5) && ( (value_1[4] == 1) || (value_2[4] == 1) ) )
            {
                // if weight is continued to goal weight starting from previous last weight
    
            }
            else
                {
                    cout << "\nexit program\n\n";
                    break;
                }
        }
    
    
    
    
        //three obj4 {obj3};
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    here's my finished code. i need to cross check it before putting the if statements in the switch statement into their classes to clean up the switch statement.

    i have to read the code now and correct any logical errors i might have made by just looking at the code;

    Code:
    #include <iostream>
    #include "one.h"
    #include "two.h"
    #include "three.h"
    #include "four.h"
    #include "five.h"
    #include "six.h"
    
    using namespace std;
    
    void test(int &value_1,
              int &value_2,
              int x,
              int &loop_closed,
              int &check_case_1_value_2,
              int &check_case_4_value_2,
              int &case_that_closed_loop,
              int &case_0_true,
              int &loop_from_case_1_to_case_4,
              int &loop_from_case_4_to_case_1,
              int &case_2_loop_not_finished,
              int &case_5_loop_not_finished,
              int page,
              int &old_case_2_page,
              int &old_case_5_page,
              int &save_page_case_2,
              int &save_page_case_5,
              int &while_true,
              int &loop_from_case_2_to_case_4)
    {
        int value_3[6] = { 0, 0, 0, 0, 0, 0 };
        int value_4[6] = { 0, 0, 0, 0, 0, 0 };
    
        // if weight out between 1 and 5
        one obj1 {value_3[0]};
    
        // if added weight out between 1 and 0
        two obj2 {obj1.get_data_value(),
        value_3[1],
        value_4[1]};
    
        // if weight is continued to goal weight starting from previous last weight
        three obj3 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        value_3[2],
        value_4[2]};
    
        // if weight in between weight out and 1
        four obj4 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        value_3[3]};
    
        // if added weight out between 1 and 0
        five obj5 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        obj4.get_data_value(),
        value_3[4],
        value_4[4]};
    
        // if weight is continued to goal weight starting from previous last weight
        six obj6 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        obj4.get_data_value(),
        obj5.get_data_value(),
        obj5.get_second_data_value(),
        value_3[5],
        value_4[5]};
    
        switch(x)
        {
    
        case 0  :
            obj1.switch_zero(value_1);
            if(case_0_true == 1)
            {
                value_1 = 1;
            }
            else if(value_1 == 0)
            {
                loop_closed = 1;
                case_that_closed_loop = 0;
                while_true = 0;
            }
    
            break;
    
        case 1  :
            obj2.switch_zero(value_1, value_2);
            if(value_2 == 1)
            {
                // if loop stops somewhere between case 1 and case 4 continue loop to case 4
    
                // loop from case 1 to case 4
                loop_from_case_1_to_case_4 = 1;
    
                //loop returns to this case on different page
    
                if(case_2_loop_not_finished == 1)
                {
                    check_case_1_value_2 += 1;
                }
            }
            else if(value_2 == 0)
            {
                loop_closed = 1;
                case_that_closed_loop = 1;
                while_true = 0;
            }
    
            break;
    
        case 2  :
            obj3.switch_zero(value_1, value_2);
            if(value_1 == 1)
            {
                // stay in case position to finish incrementing loop while also continuing on to next case
                // then go to previous case and test if value_2 == 1 for n number of repeated loops in case 2.
                // if previous case value_2 == 1 for n number of repeated loops = high percentage
                // then case 1 is aware of case 2.
    
                cout << "are you finished this loop? 1 = yes, 0 = no: ";
                cin >> case_2_loop_not_finished;
    
                if(case_2_loop_not_finished ==  0) // finished
                {
                    old_case_2_page = page; // page is refreshed
                    save_page_case_2 = 0;
                }
                else if( (case_2_loop_not_finished ==  1) && (save_page_case_2 == 0) ) // not finished
                {
                    old_case_2_page = page; // page is saved
                    save_page_case_2 = 1;
                }
    
            }
            if(value_2 == 0)
            {
                loop_from_case_2_to_case_4 = 1;
            }
    
            break;
    
        case 3  :
            obj4.switch_zero(value_1);
            if(value_1 == 0)
            {
                // closes previous case which stops case 2 from completing its loop.
                // if case 1 is going to case 4
                // if case 2  testing case 1 had a high percentage, then prevent case 1 from going to case 4
    
                loop_closed = 1;
                case_that_closed_loop = 3;
                while_true = 0;
    
            }
    
            break;
        case 4  :
            obj5.switch_zero(value_1, value_2);
            if(value_2 == 1)
            {
                // if loop stops somewhere between case 4 and case 1 continue loop to case 1
    
                // loop from case 4 to case 1
    
                loop_from_case_4_to_case_1 = 1;
    
                //loop returns to this case then carries on to the current case loop value for that loop increment
    
                if( case_5_loop_not_finished == 1)
                {
                    check_case_4_value_2 += 1;
                }
            }
            else if(value_2 == 0)
            {
                loop_closed = 1;
                case_that_closed_loop = 4;
                while_true = 0;
            }
    
            break;
        case 5  :
            obj6.switch_zero(value_1, value_2);
            if(value_1 == 1)
            {
                // stay in case position to finish incrementing loop while also continuing on to next case
                // then go to previous case and test if value_2 == 1 for n number of repeated loops in case 2.
                // if previous case value_2 == 1 for n number of repeated loops = high percentage
                // then case 4 is aware of case 5.
                // if case 4 aware of case 5, then case 5 true = true, even if its true becomes false.
                // else if case 5 true becomes false, go to case 0 true
    
                cout << "are you finished this loop? 1 = yes, 0 = no: ";
                cin >> case_5_loop_not_finished;
    
                if(case_5_loop_not_finished ==  0) // finished
                {
                    old_case_5_page = page; // page is refreshed
                    save_page_case_5 = 0;
                }
                else if( (case_5_loop_not_finished ==  1) && (save_page_case_5 == 0) ) // not finished
                {
                    old_case_5_page = page; // page is saved
                    save_page_case_5 = 1;
                }
            }
    
            break;
            }
    }
    
    int main() {
    
        int loop_closed = 0;
        int continue_loop_count = 0;
        int check_case_1_value_2 = 0;
        int check_case_4_value_2 = 0;
        int case_that_closed_loop = 0;
        int case_0_true = 0;
        int loop_from_case_1_to_case_4 = 0;
        int loop_from_case_4_to_case_1 = 0;
        int case_2_loop_not_finished = 0;
        int case_5_loop_not_finished = 0;
        int page = 0;
        int old_case_2_page = 0;
        int old_case_5_page = 0;
        int save_page_case_2 = 0;
        int save_page_case_5 = 0;
        int while_true = 1;
        int loop_from_case_2_to_case_4 = 0;
    
        int value_1[6] = { 0, 0, 0, 0, 0, 0 };
        int value_2[6] = { 0, 0, 0, 0, 0, 0 };
    
        // if weight out between 1 and 5
        one obj1 {value_1[0]};
    
        // if added weight out between 1 and 0
        two obj2 {obj1.get_data_value(),
        value_1[1],
        value_2[1]};
    
        // if weight is continued to goal weight starting from previous last weight
        three obj3 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        value_1[2],
        value_2[2]};
    
        // if weight in between weight out and 1
        four obj4 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        value_1[3]};
    
        // if added weight out between 1 and 0
        five obj5 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        obj4.get_data_value(),
        value_1[4],
        value_2[4]};
    
        // if weight is continued to goal weight starting from previous last weight
        six obj6 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        obj4.get_data_value(),
        obj5.get_data_value(),
        obj5.get_second_data_value(),
        value_1[5],
        value_2[5]};
    
        while (while_true)
        {
    
        for (int x = 0; x < 6; ++x) {
            test(value_1[x],
                value_2[x],
                x,
                loop_closed,
                check_case_1_value_2,
                check_case_4_value_2,
                case_that_closed_loop,
                case_0_true,
                loop_from_case_1_to_case_4,
                loop_from_case_4_to_case_1,
                case_2_loop_not_finished,
                case_5_loop_not_finished,
                page,
                old_case_2_page,
                old_case_5_page,
                save_page_case_2,
                save_page_case_5,
                while_true,
                loop_from_case_2_to_case_4);
    
            if ((x == 0) && (value_1[0] == 1)) {
                // if weight out between 1 and 5
    
            } else if ((x == 1) && ((value_1[1] == 1) || (value_2[1] == 1)) && (value_1[0] == 1)) //
            {
                // if added weight out between 1 and 0
    
            } else if ((x == 2) && ((value_1[1] == 1) || (value_2[1] == 1))) {
                // if weight is continued to goal weight starting from previous last weight
    
            } else if ((x == 3) && (value_1[3] == 1)) {
                // if weight in between weight out and 1
    
            } else if ((x == 4) && ((value_1[4] == 1) || (value_2[4] == 1)) && (value_1[3] == 1)) {
                // if added weight out between 1 and 0
    
            } else if ((x == 5) && ((value_1[4] == 1) || (value_2[4] == 1))) {
                // if weight is continued to goal weight starting from previous last weight
    
            } else {
                cout << "\nexit program\n\n";
                break;
            }
        }
    
        page++;
    }
    
        return 0;
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The XY Problem
    Maybe you should state your actual question.
    Because what you have is unreadable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    im not familiar with the xy problem, but my problem was i had a switch condition i needed to clean up.

    i have since finished my initial examination of my code, and will post if here for you and all to read to see if theres something i missed if not i will put the if conditions in the switch statement into their respective classes to clean up the switch statement.

    i has a lot of old notes in my previous post. im not sure of yet i havent read it enough to know it by heart but since you might have people who might want to read it, and give some tips, by all means feel free to do so and if i can implement them i might.

    heres my current good copy. i will proof read it again later on right now im tired that too a lot of work.

    Code:
    #include <iostream>
    #include "one.h"
    #include "two.h"
    #include "three.h"
    #include "four.h"
    #include "five.h"
    #include "six.h"
    
    using namespace std;
    
    void test(int &value_1,
              int &value_2,
              int x,
              int &check_case_1_value_2,
              int &check_case_4_value_2,
              int &case_that_closed_loop,
              int &case_0_true,
              int &loop_from_case_1_to_case_4,
              int &loop_from_case_4_to_case_1,
              int &case_2_loop_not_finished,
              int &case_5_loop_not_finished,
              int page,
              int &old_case_2_page,
              int &old_case_5_page,
              int &save_page_case_2,
              int &save_page_case_5,
              int &while_true,
              int &loop_from_case_2_to_case_4,
              int &case_2_repeat_value,
              int &case_5_repeat_value,
              float &average_for_case_2_test,
              float &average_for_case_5_test,
              int &test_for_case_2_go_to_break,
              int &test_for_case_5_go_to_break)
    {
        int value_3[6] = { 0, 0, 0, 0, 0, 0 };
        int value_4[6] = { 0, 0, 0, 0, 0, 0 };
    
        // if weight out between 1 and 5
        one obj1 {value_3[0]};
    
        // if added weight out between 1 and 0
        two obj2 {obj1.get_data_value(),
        value_3[1],
        value_4[1]};
    
        // if weight is continued to goal weight starting from previous last weight
        three obj3 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        value_3[2],
        value_4[2]};
    
        // if weight in between weight out and 1
        four obj4 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        value_3[3]};
    
        // if added weight out between 1 and 0
        five obj5 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        obj4.get_data_value(),
        value_3[4],
        value_4[4]};
    
        // if weight is continued to goal weight starting from previous last weight
        six obj6 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        obj4.get_data_value(),
        obj5.get_data_value(),
        obj5.get_second_data_value(),
        value_3[5],
        value_4[5]};
    
        switch(x)
        {
    
        case 0  :
            obj1.switch_zero(value_1);
            if(case_0_true == 1) // go right
            {
                value_1 = 1;
            }
            else if(value_1 == 0) // go left
            {
                case_that_closed_loop = 0;
                while_true = 0;
            }
    
            break;
    
        case 1  :
            obj2.switch_zero(value_1, value_2);
            if(value_2 == 1) // left then right
            {
                // add to for loop as condition,
                // so that if loop stops somewhere between case 1 and case 4, case 1 continues loop to case 4.
                // loop from case 1 to case 4
                loop_from_case_1_to_case_4 = 1;
    
                //loop returns to this case on different page
    
                if(case_2_loop_not_finished == 1)
                {
                    check_case_1_value_2 += 1; // too much makes a high percentage which is a bad sign
                }
            }
            else if(value_2 == 0) // left then left
            {
                case_that_closed_loop = 1;
                while_true = 0;
            }
    
            break;
    
        case 2  :
            obj3.switch_zero(value_1, value_2);
            if(value_1 == 1)
            {
                // on next loop increment loop in this case
                // next loop counts check_case_1_value_2 value.
                // test if check_case_1_value_2 value is a high percentage of this cases repeated loop value.
    
                // if previous case value_2 == 1 for n number of repeated loops = high percentage
                // then case 1 is aware of case 2.
    
                cout << "are you finished this loop? 1 = yes, 0 = no: ";
                cin >> case_2_loop_not_finished;
    
                if(case_2_loop_not_finished ==  0) // finished
                {
                    old_case_2_page = page; // page is refreshed
                    save_page_case_2 = 0;
    
                    // test to see if the previous case is aware of this case
                    average_for_case_2_test = check_case_1_value_2 / case_2_repeat_value;
                    if(average_for_case_2_test > 0.3)
                    {
                        loop_from_case_1_to_case_4 = 0;
                        // break to end program, add condition to for loop to end program
                        test_for_case_2_go_to_break = 1;
                    }
                }
                else if( (case_2_loop_not_finished ==  1) && (save_page_case_2 == 0) ) // not finished
                {
                    old_case_2_page = page; // page is saved
                    save_page_case_2 = 1;
                    case_2_repeat_value++;
                }
                else
                {
                    case_2_repeat_value++;
                }
    
            }
            if(value_2 == 0)
            {
                loop_from_case_2_to_case_4 = 1;
            }
    
            break;
    
        case 3  :
            obj4.switch_zero(value_1);
            if(value_1 == 0)
            {
                // closes previous case which stops case 2 from completing its loop.
                // if case 1 is going to case 4
                // if case 2  testing case 1 had a high percentage, then prevent case 1 from going to case 4
    
                case_that_closed_loop = 3;
                while_true = 0;
    
            }
    
            break;
        case 4  :
            obj5.switch_zero(value_1, value_2);
            if(value_2 == 1) // left then right
            {
                // add to for loop as condition,
                // so that if loop stops somewhere between case 1 and case 4, case 1 continues loop to case 4.
                // loop from case 1 to case 4
    
                loop_from_case_4_to_case_1 = 1;
    
                //loop returns to this case on different page
    
                if( case_5_loop_not_finished == 1)
                {
                    check_case_4_value_2 += 1; // too much makes a high percentage which is a bad sign
                }
            }
            else if(value_2 == 0) // left then left
            {
                case_that_closed_loop = 4;
                while_true = 0;
            }
    
            break;
        case 5  :
            obj6.switch_zero(value_1, value_2);
            if(value_1 == 1)
            {
                // stay in case position to finish incrementing loop while also continuing on to next case
                // then go to previous case and test if value_2 == 1 for n number of repeated loops in case 2.
                // if previous case value_2 == 1 for n number of repeated loops = high percentage
                // then case 4 is aware of case 5.
                // if case 4 aware of case 5, then case 5 true = true, even if its true becomes false.
                // else if case 5 true becomes false, go to case 0 true
    
                cout << "are you finished this loop? 1 = yes, 0 = no: ";
                cin >> case_5_loop_not_finished;
    
                if(case_5_loop_not_finished ==  0) // finished
                {
                    old_case_5_page = page; // page is refreshed
                    save_page_case_5 = 0;
    
                    // test to see if the previous case is aware of this case
                    average_for_case_5_test = check_case_4_value_2 / case_5_repeat_value;
                    if(average_for_case_5_test > 0.3)
                    {
                        loop_from_case_4_to_case_1 = 0;
                        // break to end program, add condition to for loop to end program
                        test_for_case_5_go_to_break = 1;
                    }
                }
                else if( (case_5_loop_not_finished ==  1) && (save_page_case_5 == 0) ) // not finished
                {
                    old_case_5_page = page; // page is saved
                    save_page_case_5 = 1;
                    case_5_repeat_value++;
                }
                else
                {
                    case_5_repeat_value++;
                }
            }
    
            break;
            }
    }
    
    int main() {
    
        int check_case_1_value_2 = 0;
        int check_case_4_value_2 = 0;
        int case_that_closed_loop = 0;
        int case_0_true = 0;
        int loop_from_case_1_to_case_4 = 0;
        int loop_from_case_4_to_case_1 = 0;
        int case_2_loop_not_finished = 0;
        int case_5_loop_not_finished = 0;
        int page = 0;
        int old_case_2_page = 0;
        int old_case_5_page = 0;
        int save_page_case_2 = 0;
        int save_page_case_5 = 0;
        int while_true = 1;
        int loop_from_case_2_to_case_4 = 0;
        int case_2_repeat_value = 0;
        int case_5_repeat_value = 0;
        float average_for_case_2_test = 0;
        float average_for_case_5_test = 0;
        int test_for_case_2_go_to_break = 0;
        int test_for_case_5_go_to_break = 0;
    
        int value_1[6] = { 0, 0, 0, 0, 0, 0 };
        int value_2[6] = { 0, 0, 0, 0, 0, 0 };
    
        // if weight out between 1 and 5
        one obj1 {value_1[0]};
    
        // if added weight out between 1 and 0
        two obj2 {obj1.get_data_value(),
        value_1[1],
        value_2[1]};
    
        // if weight is continued to goal weight starting from previous last weight
        three obj3 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        value_1[2],
        value_2[2]};
    
        // if weight in between weight out and 1
        four obj4 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        value_1[3]};
    
        // if added weight out between 1 and 0
        five obj5 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        obj4.get_data_value(),
        value_1[4],
        value_2[4]};
    
        // if weight is continued to goal weight starting from previous last weight
        six obj6 {obj1.get_data_value(),
        obj2.get_data_value(),
        obj2.get_second_data_value(),
        obj3.get_data_value(),
        obj3.get_second_data_value(),
        obj4.get_data_value(),
        obj5.get_data_value(),
        obj5.get_second_data_value(),
        value_1[5],
        value_2[5]};
    
        while (while_true)
        {
    
        for (int x = 0; x < 6; ++x) {
            test(value_1[x],
                value_2[x],
                x,
                check_case_1_value_2,
                check_case_4_value_2,
                case_that_closed_loop,
                case_0_true,
                loop_from_case_1_to_case_4,
                loop_from_case_4_to_case_1,
                case_2_loop_not_finished,
                case_5_loop_not_finished,
                page,
                old_case_2_page,
                old_case_5_page,
                save_page_case_2,
                save_page_case_5,
                while_true,
                loop_from_case_2_to_case_4,
                case_2_repeat_value,
                case_5_repeat_value,
                average_for_case_2_test,
                average_for_case_5_test,
                test_for_case_2_go_to_break,
                test_for_case_5_go_to_break);
    
            if( (test_for_case_2_go_to_break) || (test_for_case_5_go_to_break) )
            {
                while_true = 0;
                break;
            }
    
            if ( ((x == 0) && (value_1[0] == 1)) || (loop_from_case_4_to_case_1) ) {
                // if weight out between 1 and 5
    
            } else if ((x == 1) && ((value_1[1] == 1) || (value_2[1] == 1)) && (value_1[0] == 1)) //
            {
                // if added weight out between 1 and 0
    
            } else if ( ((x == 2) && ((value_1[1] == 1) || (value_2[1] == 1))) || (loop_from_case_1_to_case_4) ) {
                // if weight is continued to goal weight starting from previous last weight
    
            } else if ( ((x == 3) && (value_1[3] == 1)) || (loop_from_case_1_to_case_4) ) {
                // if weight in between weight out and 1
    
            } else if ((x == 4) && ((value_1[4] == 1) || (value_2[4] == 1)) && (value_1[3] == 1)) {
                // if added weight out between 1 and 0
    
            } else if ( ((x == 5) && ((value_1[4] == 1) || (value_2[4] == 1))) || (loop_from_case_4_to_case_1) ) {
                // if weight is continued to goal weight starting from previous last weight
    
            } else {
                cout << "\nexit program\n\n";
                break;
            }
        }
    
        page++;
    }
    
        return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My main observation is to say that if you're really numbering your classes and other identifiers, then your code really is unreadable. One of the motivations of assembly language was to provide descriptive names to the numbers of machine language; one of the motivations of higher level languages was to provide descriptive names to the constructs of assembly language, etc. You've kinda regressed by relying so much on numbers instead of using names from your problem domain.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The XY problem is basically you're trying to solve X by trying to implement Y (badly).

    You need to state the actual problem (X).

    Looking at your broken Y solution, offering various ideas to help you 'solve' Y won't get you any nearer to X.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    May 2019
    Posts
    214
    @jeremy duncan

    One of the points these posts are trying to tell you (among a great many things) is that you may not actually even require a switch statement.

    If that is true, cleaning it up isn't accomplishing much of anything.

    The basics of switch statements is to keep each case simple, meaning one or two statements and a break. That bit of wisdom can't help you, though, because there's something laserlight is telling you that I don't believe you've understood.

    Look through your code and notice all of the repetition.

    Here I'm saying that obj1, obj2, obj3, obj4 don't make sense to any of us. This implies a container of objects, because most of them apparently are doing the same thing and I don't yet see where the types are declared and defined, so we can't tell what your overall need really is.

    I can make a prediction, which is often a fools errand and I risk being wrong.

    I think you could shorten your code by a factor of 6 (maybe a factor of 18) if WE, out here, understood what you are really trying to do, and you could gain from us (some with decades of experience) ways to leverage the language to make everything shorter, simpler, clearer and easier.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    i finished making what i was making and felt bad you didnt know what i was trying to code so i decided to share my github where my project is t and you can read all the code there to see what i did.

    GitHub - learningToProgramGames/flow: removing the jerkiness

    and here is the flowchart the code was trying to do, the flowchart is what formed the code. by repeating the code i built up the flowchart step by step.

    https://raw.githubusercontent.com/le...0logic%203.jpg

    so for this step of my project im done, but if you want you can read my code and point out some error or things i could do better and if its an error i cant fix i will be sure to ask you for help.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    fwiw i did a few bug fixes to the code, its all done now though but i wanted to say i updated it if you dl the old version and found the bugs. so from here on if you look at it and find bugs or something to do with it then let me know!

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    My point stands: your code is unreadable. It makes no sense. Why do you have numbered classes? What's the point of your code?

    You could have answered this in the README, but instead your README vaguely talks about a starting point, moving, an equation, then suddenly goes into rambling about being not jerky, then quickly moves on to talk about anti cheat. Huh? What has that got to do with the starting point thing, jerkiness, and classes named after numbers?!
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It looks like the kind of nonsense that results from trying to convert assembler back into some kind of source code.

    Meaningless variable names and obscure coding patterns.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    i updated the readme, to make it easier to understand what i was going for with the program.

    the names in the code are as good as i can make them, its not technically based its what i think they should be.

    theres an interpretive value for the names that means the name_interpretation = true for me but name_interpretation = false for you. nothing i can do to find a middle ground on the name values so you can understand them, sorry; youll have to run the program and read the comments and follow the flow chart as you run the program to see what the names in the program should be.

    and the obscure coding patterns follow the logic in the flow chart, and readme.

    i feel bad about the names now, but nothing i can do if i dont know if the new names i make will be any better or maybe even worse.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well it seems like you're trying to create an aim-bot, or some other type of cheat.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. Switch statement
    By beene in forum C++ Programming
    Replies: 21
    Last Post: 07-01-2007, 08:13 AM
  3. switch statement
    By crash88 in forum C Programming
    Replies: 4
    Last Post: 06-29-2006, 12:49 AM
  4. Can someone simplify this statement from this thead
    By cdalten in forum C Programming
    Replies: 1
    Last Post: 01-30-2006, 08:04 AM
  5. The Switch Statement
    By gozlan in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:44 AM

Tags for this Thread