zer02pi
Posts: 18
Joined: Thu Dec 01, 2016 6:39 pm

how can i define boolean ?

Sun Feb 12, 2017 3:20 pm

I try to determine output's situtaion. For example if outputs are 1000, I will do adim1 = true and other adims will be false. But when I try to build this program it says "was not declared" for adim.

How can i define boolean in c++?

Code: Select all

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "pigpio.h"
#include "qmessagebox.h"
int pin[4]; // A = 1, B = 2, C = 3, D = 4
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    

    ui->setupUi(this);

    gpioInitialise();

    if (gpioInitialise() < 0)
    {
        QMessageBox::information(this, "Error!", "PIGPIO Library has not loaded!");
    }


}

MainWindow::~MainWindow()
{
    delete ui;
}

void pindurum()
{
    for (int i = 1; i < 5; i++)
        bool durum[i] = gpioRead(pin[i]);

    if (durum1 && !durum2 && !durum3 && !durum4)
        for (int i = 1; i < 9; i++)
            if (i!=1)
                bool adim[i] = false;
            else
                bool adim[i] = true;

    else if (durum1 && durum2 && !durum3 && !durum4)
        for (int i = 1; i < 9; i++)
            if (i!=2)
                bool adim[i] = false;
            else
                bool adim[i] = true;
}

void MainWindow::on_pinok_clicked()
{
    int pin1 = ui->pina->text().toInt();
    int pin2 = ui->pinb->text().toInt();
    int pin3 = ui->pinc->text().toInt();
    int pin4 = ui->pind->text().toInt();

    for (int i = 1; i < 5; i++)
        if (!pin[i])
            QMessageBox::information(this, "Error!", "You have to define PIN numbers!");

    for (int i = 1; i < 5; i++ )
    {
        gpioSetMode(pin[i], PI_OUTPUT);
    }
}

User avatar
buja
Posts: 567
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: how can i define boolean ?

Sun Feb 12, 2017 3:45 pm

The problem is that adim is an array.
Your program would have worked if adim had been a simple variable (bool adim = true), but if it's an array you should declare it as an array, including its size somewhere before you start using it: bool adim[number_of_elements].

Doesn't the compiler complain about durum too? That has the same problem, unless it's defined in one of the header files you include.

Ernst
Posts: 1334
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: how can i define boolean ?

Sun Feb 12, 2017 3:55 pm

zer02pi wrote:I try to determine output's situtaion. For example if outputs are 1000, I will do adim1 = true and other adims will be false. But when I try to build this program it says "was not declared" for adim.

How can i define boolean in c++?

Code: Select all

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "pigpio.h"
#include "qmessagebox.h"
int pin[4]; // A = 1, B = 2, C = 3, D = 4
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    

    ui->setupUi(this);

    gpioInitialise();

    if (gpioInitialise() < 0)
    {
        QMessageBox::information(this, "Error!", "PIGPIO Library has not loaded!");
    }


}

MainWindow::~MainWindow()
{
    delete ui;
}

void pindurum()
{
    for (int i = 1; i < 5; i++)
        bool durum[i] = gpioRead(pin[i]);

    if (durum1 && !durum2 && !durum3 && !durum4)
        for (int i = 1; i < 9; i++)
            if (i!=1)
                bool adim[i] = false;
            else
                bool adim[i] = true;

    else if (durum1 && durum2 && !durum3 && !durum4)
        for (int i = 1; i < 9; i++)
            if (i!=2)
                bool adim[i] = false;
            else
                bool adim[i] = true;
}

void MainWindow::on_pinok_clicked()
{
    int pin1 = ui->pina->text().toInt();
    int pin2 = ui->pinb->text().toInt();
    int pin3 = ui->pinc->text().toInt();
    int pin4 = ui->pind->text().toInt();

    for (int i = 1; i < 5; i++)
        if (!pin[i])
            QMessageBox::information(this, "Error!", "You have to define PIN numbers!");

    for (int i = 1; i < 5; i++ )
    {
        gpioSetMode(pin[i], PI_OUTPUT);
    }
}
this might be your problem:

Code: Select all

        bool adim[9];
        for (int i = 1; i < 9; i++)
            if (i!=2)
                adim[i] = false;
            else
                adim[i] = true;
but there are a few more things that you should attend to, have a look at the following:

Code: Select all

        bool adim[8];  // array of 8 boolean values, index starts at 0
        for (int i = 0; i < 8; i++)
        {
            if (i!=1) // strange, adjusted by one to stay with example
            {
                adim[i] = false;
            }
            else
            {
                adim[i] = true;
            }
        }  
The road to insanity is paved with static ip addresses

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: how can i define boolean ?

Sun Feb 12, 2017 7:06 pm

AFAIK, as in C the smallest memory / data type unit is a byte, there is - other than for Pascal - actually no 1-bit C datatype "boolean", just char / unsigned char.
2nd, even if the value "false" is unique (== 0), the value of "true" is ambiguous, because any other value different from 0 will be identified as true, e.g.: -1, +1, +123, -5,...

Having said that, I would recommend to initialize all your "boolean" arrays as of type "char" and exchange all "false" values by 0 (zero) and all "true" values by 1, and then try this as a workaround.

Ernst
Posts: 1334
Joined: Sat Feb 04, 2017 9:39 am
Location: Germany

Re: how can i define boolean ?

Sun Feb 12, 2017 7:18 pm

1dot0 wrote:AFAIKa, as in C the smallest memory / data type unit is a byte, there is - other than for Pascal - actually no 1-bit C datatype "boolean", just char / unsigned char.
2nd, even if the value "false" is unique (== 0), the value of "true" is ambiguous, because any other value different from 0 will be identified as true, e.g.: -1, +1, +123, -5,...

Having said that, I would recommend to initialize all your "boolean" arrays as of type "char" and exchange all "false" values by 0 (zero) and all "true" values by 1, and then try this as a workaround.
I am glad that I do not have to analyze a bug in your source code.

The proper way is to use the compiler defined types and constants.
The road to insanity is paved with static ip addresses

User avatar
buja
Posts: 567
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: how can i define boolean ?

Sun Feb 12, 2017 7:57 pm

1dot0 wrote:AFAIK, as in C the smallest memory / data type unit is a byte, there is - other than for Pascal - actually no 1-bit C datatype "boolean", just char / unsigned char.
2nd, even if the value "false" is unique (== 0), the value of "true" is ambiguous, because any other value different from 0 will be identified as true, e.g.: -1, +1, +123, -5,...

Having said that, I would recommend to initialize all your "boolean" arrays as of type "char" and exchange all "false" values by 0 (zero) and all "true" values by 1, and then try this as a workaround.
This will not solve any problem the topic starter has.
In fact, the use of the symbolic values true and false are positive things in his code, which is C++ by the way and not C.

Macros like #define TRUE 1 and #define FALSE 0 were common, and I guess they still are because they are quite clear, but as of C99 C has an official built-in boolean type: _Bool (not very pretty) or bool (if you #include <stdbool.h>). And such a boolean type can have only 2 values: 0 or 1 (false and true, as defined in stdbool.h). Even if you assign a value of 5 or -5 to a boolean variable it will be converted to 1.

zer02pi
Posts: 18
Joined: Thu Dec 01, 2016 6:39 pm

Re: how can i define boolean ?

Sun Feb 12, 2017 8:36 pm

buja wrote:
1dot0 wrote:AFAIK, as in C the smallest memory / data type unit is a byte, there is - other than for Pascal - actually no 1-bit C datatype "boolean", just char / unsigned char.
2nd, even if the value "false" is unique (== 0), the value of "true" is ambiguous, because any other value different from 0 will be identified as true, e.g.: -1, +1, +123, -5,...

Having said that, I would recommend to initialize all your "boolean" arrays as of type "char" and exchange all "false" values by 0 (zero) and all "true" values by 1, and then try this as a workaround.
This will not solve any problem the topic starter has.
In fact, the use of the symbolic values true and false are positive things in his code, which is C++ by the way and not C.

Macros like #define TRUE 1 and #define FALSE 0 were common, and I guess they still are because they are quite clear, but as of C99 C has an official built-in boolean type: _Bool (not very pretty) or bool (if you #include <stdbool.h>). And such a boolean type can have only 2 values: 0 or 1 (false and true, as defined in stdbool.h). Even if you assign a value of 5 or -5 to a boolean variable it will be converted to 1.

Including stdbool.h is solved the problem. And I fixed durum1 to durum[1] and others like this.

Thank you.

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: how can i define boolean ?

Sun Feb 12, 2017 11:57 pm

...or bool (if you #include <stdbool.h>). And such a boolean type can have only 2 values: 0 or 1 (false and true, as defined in stdbool.h). Even if you assign a value of 5 or -5 to a boolean variable it will be converted to 1.
CMIIW, but I didn't see a #include <stdbool.h> in the OP's former code:

Code: Select all

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "pigpio.h"
#include "qmessagebox.h"
//*SNIP*
bool adim[9];
adim[i] = false;
//*SNIP*
adim[i] = true;
But of course I stand corrected about the C++ code issue.

Return to “C/C++”