如果我添加或减去一个值,我在变量中的值就会消失(不打印)

My value in my variable disapears (does not print) if i add or subtract a value

我正在为学校做一个项目,我需要做一个将温度保持在设定温度附近的系统(这是通过一个 phidget 传感器并转动加热和冷却来完成的 元件打开和关闭。)传感器值可以使用公式((当前温度 + 50)* 4)转换为摄氏度,我让它在静态 20 摄氏度下工作,但现在我试图从一个数据库并将其保存在该温度附近(因此可以在加热和冷却元件所在的房间内更改温度)。但是当我尝试使用公式将从数据库获得的温度转换为传感器值时,温度变量开始给出随机数,我不知道为什么。有人可以解释或帮助吗?

(问题定位见代码示例底部的注释)

//my code is a code written for controlling the temprature in a room
//this is done by a phidget sensor and turning a heating and cooling
//element on and of.

#include <phidget21.h> // voor phidgetfucties
#include <stdio.h>   // voor printf()
#include <mysql/mysql.h> // voor mysql querys
#include <my_global.h>

int main (int argc, char* argv[])
{   //variables
int val;
int gewensteTempratuur = 280;
int gewensteGradenCelcius = 0;
int i = 0;
int temp = 0;

CPhidgetInterfaceKitHandle ifKit = 0;
CPhidgetInterfaceKit_create(&ifKit);

CPhidget_open((CPhidgetHandle)ifKit,-1);

for(;;)
{
    CPhidgetInterfaceKit_getSensorValue(ifKit,0,&val);


    printf("Value %d \n", val);

    if(val < (gewensteTempratuur - 4)){
    //verwarming
    CPhidgetInterfaceKit_setOutputState(ifKit,0,1);
    //airco
    CPhidgetInterfaceKit_setOutputState(ifKit,1,0);
    }

    if(val > (gewensteTempratuur + 4)){
        //verwarming
        CPhidgetInterfaceKit_setOutputState(ifKit,0,0);
        //airco
        CPhidgetInterfaceKit_setOutputState(ifKit,1,1);

    }

    if((val < (gewensteTempratuur + 4)) && (val > (gewensteTempratuur - 4)))         {
        //verwarming
        CPhidgetInterfaceKit_setOutputState(ifKit,0,0);
        //airco
        CPhidgetInterfaceKit_setOutputState(ifKit,1,0);

    }

    //Database Connection
    MYSQL *conn;
    //Verbings gegevens

    char *server = "server";
    char *user = "user";
    char *password = "pass";
    char *database = "database";

    conn = mysql_init(NULL);
    if (!mysql_real_connect(conn, server,
        user, password, database, 0, NULL, 0)) {
        fprintf(stderr, "%s\n", mysql_error(conn));
    }

    //getting information from the database
    if (mysql_query(conn, "SELECT Gewenstetemperatuur FROM SensorValue")){
        mysql_error(conn);
        }

        MYSQL_RES *result = mysql_store_result(conn);

        if (result == NULL){
        mysql_error(conn);
        }

        int num_fields = mysql_num_fields(result);

        MYSQL_ROW row;

        while ((row = mysql_fetch_row(result))){
            for (i = 0; i < num_fields; i++){
            printf("%s ", row[i] ? row[i] : "NULL");

            gewensteGradenCelcius = (int)row[i];
            temp = gewensteGradenCelcius;

            printf("\n %s \n", gewensteGradenCelcius);
            printf("%d \n",temp )

            //Here is were is get stuck if i try to take the value and run it trough a 
            //formula like (gewensteGradenCelcius + 50) * 4 and the try to print the 
            //gewensteGradenCelcius just puts out nothing
            }
            printf("\n");
            }

            mysql_free_result(result);
            usleep(1500000);
            }

    }

行[i]是一个文本(第“20”行或“18”行),所以这一行是错误的:

        gewensteGradenCelcius = (int)row[i];

你应该写

        gewensteGradenCelcius = atoi(row[i]);

number 包含在 string 格式中的代码转换(row[i] ) 做错了....

A C string 无法像您尝试的那样复制到 int 中。 C 确实提供了几种方法来执行此操作,包括以下...更改:

gewensteGradenCelcius = (int)row[i];//incorrect

收件人:

//EITHER
gewensteGradenCelcius = atoi(row[i]);//simple and legal C99, but not recommended
//OR
char *endptr;
gewensteGradenCelcius = (int)strtol(row[i], &endptr, 10);//recommended method
//OR
sscanf(row[i], "%d",&gewensteGradenCelcius);//recommended method

(为什么不推荐atoi()

然后,因为 gewensteGradenCelcius 是一个 int 你的 printf 语句,所以使用 int format specifier .变化:

printf("\n %s \n", gewensteGradenCelcius);//wrong format specifier for int

收件人:

printf("\n %d \n", gewensteGradenCelcius);//correct format specifier
           ^^