# The value of the integer

I want to ask is why my "value w"

``````if(dataReceived[i] == test1[i])//比對回傳值
{
w++;
}
``````

come's out the at 5 but the other's like x,y,z come's out at 3?

``````//apart of T
char test1[3]={'O','K','A'};
char test2[3]={'O','K','B'};
char test3[3]={'O','K','C'};
char test4[3]={'O','K','D'};
char failed[6]={'f','a','i','l','e','d'};

void getData() {

int a=5;
int i=0,x=0,y=0,z=0,f=0,w=0;
{
newData = true;
for(i=0;i<a;i++)
{
{
w++;
}
{
x++;
}
{
y++;
}
{
z++;
}
{
f++;
}
}
``````
``````//apart of R

void send() {
if (newData == 1) {
bool rslt;

Serial.print("OK \n");

if (rslt) {
}
else {
}
Serial.println();
newData = 0;
}
else if(newData==2)
{
bool rslt;

Serial.print("OK \n");

if (rslt) {
}
else {
}
Serial.println();
newData=0;
}
else if(newData==3)
{
bool rslt;

Serial.print("OK \n");

if (rslt) {
}
else {
}
Serial.println();
newData=0;
}
else if(newData==4)
{
bool rslt;

Serial.print("OK \n");

if (rslt) {
}
else {
}
Serial.println();
newData=0;
}
else if(newData==5)
{
bool rslt;

Serial.print("failed \n");

if (rslt) {
}
else {
}
Serial.println();
newData=0;
}
}
``````

How long is test1?
test2?
test3?
test4?

1 Like

char test1[3]={'O','K','A'};
char test2[3]={'O','K','B'};
char test3[3]={'O','K','C'};
char test4[3]={'O','K','D'};

It was a rhetorical question.
I know how long they are - it is really obvious.

I was hoping to get you to think about the problem.

OK,thank's

I think if I use it like this,the w,x,y,z may have to be 5 but why the x,y,z still be 3?

You have three boxes in front of you.
Each has a different kind of chocolate bar in it
What type of bar is in the fourth and fifth boxes?

Uh.......I don't know? random?

I changed 'a' to 3 it's OK, but the 'failed' will only compare the first 3 letters

Failed has six letters

why not just properly use strings?

consider the following which attempt to simulates receiving data over the radio using the serial interface.

it's not obvious when radio.read() returns. how does it recognize the last char?

in the code below, readData() which attempts to simulate radio.read() has a 2 msec delay so that Serial.available() is called after the next char is received and readData() continues to read chars until the last char

``````const char *str [] = {
"OKA",
"OKB",
"OKC",
"OKD",
};
#define N_STR   (sizeof(str)/sizeof(char*))

char     s [80];

char *
static char buf [80];
unsigned i = 0;

while (Serial.available () && i < (sizeof(buf)-1))  {
delay (2);
}
buf [i] = '\0';

return buf;
};

int
chkData (
char *s )
{
for (unsigned n = 0; n < N_STR; n++)
if (! strncmp (s, str [n], strlen (str[n])))
return n;

return -1;
}

void
loop ()
{
if (Serial.available ())  {
int n = chkData ( readData ());

if (0 <= n)
Serial.println (str [n]);
else
Serial.println ("unknown");
}
}

void
setup ()
{
Serial.begin (9600);
}
``````
1 Like

Maybe random, but more likely you're looking at the boxes on the desk of the next guy along.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.