Simple, simple question - adding a constant to a byte variable

I know this is a simple question, but I am a 75 year old guy (who used to be a pretty good FORTRAN programmer) who is having trouble with C++ (Arduino).

So .... I have a variable called hour and is declared as a byte type. All I want to do is add 1 to the value in hour.

I have tried hour = hour + 1 . It doesn't work. Where am I going wrong?

schuh8:
I know this is a simple question, but I am a 75 year old guy (who used to be a pretty good FORTRAN programmer) who is having trouble with C++ (Arduino).

So .... I have a variable called hour and is declared as a byte type. All I want to do is add 1 to the value in hour.

I have tried hour = hour + 1 . It doesn't work. Where am I going wrong?

that should do it or:

hour += 1;

or just :

hour++;

is something changing it back?

what does your code look like?

hour = hour + 1 will absolutely work. if it didn't, you are doing something wrong and need to post the code you think didn't work.

Regards,
Ray L.

I think hour = hour +1, should have worked just fyn.. could you pls post the entire code so that we can be of more help..

Sorry Ray, didnt read your comment

hour = hour + 1 will absolutely work.

Most likely problem: you have put the statement outside any function.
In a fortran program, execution starts at the top of the file, which can call other functions, but doesn't need to.
In a C program or arduino sketch, all executable statements must be inside some function like main() {} or Setup() {}
(some C statements can appear outside of a function, but they're nominally "definition" statements rather than executable statements.)

// GLOBAL VARIABLES
// ... INITIIALIZED TO ZERO BY DEFAULT ...
// ... UNLESS SPECIFICALLY OTHERWISE ...

int     hour;


// ARDUINO - REQUIRED
void loop()
{   }

// ARDUINO - REQUIRED
void setup()
{
    // LOCAL VARIABLES MUST BE SPECIFICALLY INITIALIZED
    // ... OR WILL CONTAIN WHATEVER GARBAGE WAS ...
    // ... AT THEIR ADDRESS ON THE STACK ...

    int     hour    = 0;

    // initialize serial port to 9600 baud
    Serial.begin(9600);

    Serial.println(hour);
    
    hour = hour + 1;

    Serial.println(hour);
}

lloydean dont you feel it would have been better to wait for schuh8's response on this one considering he is a total newbie to C, C++ programming. Moreover what purpose would a confusing(for a newbie) example code ,containing a global and a local variable of same name and without proper indentation with a different data type than one mentioned in the problem, serve.

Chaitanya1:
lloydean dont you feel it would have been better to wait for schuh8's response on this one considering he is a total newbie to C, C++ programming. Moreover what purpose would a confusing(for a newbie) example code ,containing a global and a local variable of same name and without proper indentation with a different data type than one mentioned in the problem, serve.

What as silly question.

If I felt it would've been "better" to wait I would've waited but I "felt" it would be better to show a working example then to make him guess.

I, very much on purpose, show both local and global variables; that the two can have the same name but not be the same values. I comment on the differences in initializing global vs local variables.

"... same name and without proper indentation with a different data type than one mentioned in the problem, serve."

But these confuse me.

What are you going on about with "proper" indentation?

A different "type" than the one mentioned? He mentions no type so how can I be using something different?

lloyddean:
A different "type" than the one mentioned? He mentions no type so how can I be using something different?

Read the thread title...

A 'byte' is such a generic thing and I NEVER use it as a type as it is ambiguous when doing cross platform programming sometimes signed and others unsigned.

schuh8:
I know this is a simple question, but I am a 75 year old guy (who used to be a pretty good FORTRAN programmer) who is having trouble with C++ (Arduino).

So .... I have a variable called hour and is declared as a byte type. All I want to do is add 1 to the value in hour.

I have tried hour = hour + 1 . It doesn't work. Where am I going wrong?

This works fine. At a guess, you are initializing the value of hour to zero in your loop().

schuh8:
I know this is a simple question, but I am a 75 year old guy (who used to be a pretty good FORTRAN programmer) who is having trouble with C++ (Arduino).

75 years or not, you get a much better answer, and save a lot of guessing, by posting all your code.

Read http://snippets-r-us.com/ and weep. :wink: