IF statements and Radio Buttons

I have 2 radio buttons. When I select one of them, the function is called and both execute. I select radio_button_1, it and radio_button_2 execute. I select radio_button_2, same thing: radio_button_1 executes, then radio_button_2. My if statements are like this.

1
2
3
4
5
6
7
8
if (radio_button_1)
{
   code
} 
if (radio_button_2)
{
   code
}


I thought an if statement performed a boolean check. And that's all I have here. I don't know if it would make a difference, but I'm using Code::Blocks. Will someone tell me what I'm doing wrong? Why do both if statements execute?

Here's the code in full:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
void Angela001Frame::OnListBox1Select(wxCommandEvent& event)
{
    if (RadioButton_amountDescending)
    {
        try
        {
            wxMessageBox("Descending", "DESC");
            Grid_View_Recipes->ClearGrid();
            wxString recipeName = ListBox1->GetString(ListBox1->GetSelection());

            db->Open("zyx.db");

            wxSQLite3Statement stmt3 = db->PrepareStatement("SELECT recipe_id, note, standard "
                                                            "FROM recipes "
                                                            "WHERE recipe_name = ?");
            stmt3.Bind(1, recipeName);
            wxSQLite3ResultSet result22 = stmt3.ExecuteQuery();

            /* CHECK THE FIELD "standard" TO SEE HOW WE LABEL THE GRID */
            if (result22.GetInt(2) == 0)
                Grid_View_Recipes->SetColLabelValue(1, "GRAMS");
            else
                Grid_View_Recipes->SetColLabelValue(1, "OUNCES");

            int recipe_ID = result22.GetInt(0);
            TextCtrl1->SetValue(result22.GetAsString(1)); // UPDATE NOTE

            int newRow = 0;
            wxSQLite3Statement stmt2 = db->PrepareStatement("SELECT ingredient, amount "
                                                            "FROM recipe_ingredients "
                                                            "INNER JOIN recipes ON recipes.recipe_id = recipe_ingredients.recipe_id "
                                                            "WHERE recipe_ingredients.recipe_id = ?"
                                                            "ORDER BY amount DESC");
            stmt2.Bind(1, recipe_ID);
            wxSQLite3ResultSet result1 = stmt2.ExecuteQuery();
            int colCount1 = result1.GetColumnCount();
            while (result1.NextRow())
            {
                for (int i = 0; i < colCount1; i++) // VARIABLE i MOVES THE COLUMN CELL OVER
                {
                    Grid_View_Recipes->SetCellValue(newRow, i, result1.GetAsString(i)); // UPDATE CURRENT CELL
                }
                newRow++;
            }
                stmt2.Finalize();
        } catch(wxSQLite3Exception& e) {

        }
        db->Close();

    }
    if (RadioButton_amountAscending)
    {
        try
        {
            wxMessageBox("Ascending", "ASC");
            Grid_View_Recipes->ClearGrid();
            wxString recipeName = ListBox1->GetString(ListBox1->GetSelection());

            db->Open("zyx.db");

            wxSQLite3Statement stmt3 = db->PrepareStatement("SELECT recipe_id, note, standard "
                                                            "FROM recipes "
                                                            "WHERE recipe_name = ?");
            stmt3.Bind(1, recipeName);
            wxSQLite3ResultSet result22 = stmt3.ExecuteQuery();

            /* CHECK THE FIELD "standard" TO SEE HOW WE LABEL THE GRID */
            if (result22.GetInt(2) == 0)
                Grid_View_Recipes->SetColLabelValue(1, "GRAMS");
            else
                Grid_View_Recipes->SetColLabelValue(1, "OUNCES");

            int recipe_ID = result22.GetInt(0);
            TextCtrl1->SetValue(result22.GetAsString(1)); // UPDATE NOTE

            int newRow = 0;
            wxSQLite3Statement stmt2 = db->PrepareStatement("SELECT ingredient, amount "
                                                            "FROM recipe_ingredients "
                                                            "INNER JOIN recipes ON recipes.recipe_id = recipe_ingredients.recipe_id "
                                                            "WHERE recipe_ingredients.recipe_id = ?"
                                                            "ORDER BY amount ASC");
            stmt2.Bind(1, recipe_ID);
            wxSQLite3ResultSet result1 = stmt2.ExecuteQuery();
            int colCount1 = result1.GetColumnCount();
            while (result1.NextRow())
            {
                for (int i = 0; i < colCount1; i++) // VARIABLE i MOVES THE COLUMN CELL OVER
                {
                    Grid_View_Recipes->SetCellValue(newRow, i, result1.GetAsString(i)); // UPDATE CURRENT CELL
                }
                newRow++;
            }
                stmt2.Finalize();
        } catch(wxSQLite3Exception& e) {

        }
        db->Close();
    }
}
Well, obviously if one of the booleans is true and the other is false then only one of your ifs will execute. If both of those if's are actually executing their bodies then both of those booleans are true. Why not print (to a file or message box) the state of those two variables at the top of the function?

You have a horrific amount of repetition in that piece of code. How about something like this (untested) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
void Angela001Frame::OnListBox1Select(wxCommandEvent& event)
{
    const char *long_form = "Descending";
    const char *short_form = "DESC";

    if (RadioButton_amountAscending) {
        long_form = "Ascending";
        short_form = "ASC";
    }

    char select[500] = "SELECT ingredient, amount "
                           "FROM recipe_ingredients "
                           "INNER JOIN recipes ON recipes.recipe_id = recipe_ingredients.recipe_id "
                           "WHERE recipe_ingredients.recipe_id = ?"
                           "ORDER BY amount ";
    strcat(select, short_form);

    try
    {
        wxMessageBox(long_form, short_form);
        Grid_View_Recipes->ClearGrid();
        wxString recipeName = ListBox1->GetString(ListBox1->GetSelection());
        db->Open("zyx.db");
        wxSQLite3Statement stmt3 = db->PrepareStatement("SELECT recipe_id, note, standard "
                                                        "FROM recipes "
                                                        "WHERE recipe_name = ?");
        stmt3.Bind(1, recipeName);
        wxSQLite3ResultSet result22 = stmt3.ExecuteQuery();

        /* CHECK THE FIELD "standard" TO SEE HOW WE LABEL THE GRID */
        if (result22.GetInt(2) == 0)
            Grid_View_Recipes->SetColLabelValue(1, "GRAMS");
        else
            Grid_View_Recipes->SetColLabelValue(1, "OUNCES");
        int recipe_ID = result22.GetInt(0);
        TextCtrl1->SetValue(result22.GetAsString(1)); // UPDATE NOTE
        int newRow = 0;

        wxSQLite3Statement stmt2 = db->PrepareStatement(select);

        stmt2.Bind(1, recipe_ID);
        wxSQLite3ResultSet result1 = stmt2.ExecuteQuery();
        int colCount1 = result1.GetColumnCount();
        while (result1.NextRow())
        {
            for (int i = 0; i < colCount1; i++) // VARIABLE i MOVES THE COLUMN CELL OVER
            {
                Grid_View_Recipes->SetCellValue(newRow, i, result1.GetAsString(i)); // UPDATE CURRENT CELL
            }
            newRow++;
        }
        stmt2.Finalize();
    } catch(wxSQLite3Exception& e) {

    }
    db->Close();
}

I know my code blows. I do. A lot of redundancy all over the place.

I'm working with what you provided. Right now, it defaults to the ASC radio button, even when the DESC is selected. I appreciate your help. Thanks.
You code doesn't blow. It's actually pretty good. It's often faster to just copy a block of code, but if there's only a couple of differences it just makes the code more bloated and harder for others to understand (since they need to scan the two blocks by eye to see what the differences are).

Anyway, it seems that both booleans are true. Try printing them to a file at the top of the function and then look at the file:

1
2
3
4
5
6
#include <fstream> //  at the top of the cpp file

// just inside the function before any other statement
std::ofstream fout("boolean.txt");
fout << RadioButton_amountAscending << ", " << RadioButton_amountDescending << '\n';
fout.close();

Then look at boolean.txt. If it says "1, 1" then they are both true.
Last edited on
I'm way out of my league on this. But here's the result:

0x556de6731400, 0x556de672bd40
Last edited on
This is just a recipe program for my girlfriend. The ascending and descending queries were just extras to give her more to look at. Thing is, since this has become such an issue, I'm dropping the radio buttons for now and will move on with other code.

I really appreciate your help. I'm new to C++ and you've shown me some "new" ways to do some things. I think my thing here is that, while I've done some online tutorials on C++, I keep throwing a wall up and kind of dividing GUI from console. And what I mean by that is when I use the GUI, I tend to block out the use of things like strcat(), const. I guess that's because all the tutorials are geared toward console programming. So using a GUI...you get my drift.

Thanks again for your help, tph.
Last edited on
So apparently they weren't booleans at all. In a boolean context those values would both be "true" (since they are not zero). But if you've given up on that, that's okay.

If you want to give it one last try, you could replace the first part of the code I gave above with this:

1
2
3
4
5
6
7
8
    bool ascending = ((wxRadioButton*)RadioButton_amountAscending)->GetValue();
    const char *long_form = "Descending";
    const char *short_form = "DESC";
    if (ascending) {
        long_form = "Ascending";
        short_form = "ASC";
    }
    // ... 

It's really just a (barely-educated) guess, though, so it might not work.
Last edited on
Yeah. I've already removed everything. I'll come back to it though, at a later time. I won't let it whip me. Not even close.I'll create a new project and try to figure out why the radio buttons aren't boolean. Sort it all out there and then bring it back to the recipe program. Baby loves clicking buttons and stuff :D
Topic archived. No new replies allowed.