Variable question

sooooooooooooooooooooo c++ is NOT PHP…

i cant seem to be able to set text as a variable x = n; // variable x = the letter n

how can i make this work

i need to be able to change x to one of 4 letters depending on what y =

ie

if y == o {
x = N;
}
else if y == 1 {
x = S;
}
else if y == 2 {
x = E;
}
else if == 3 {
x = W;
}

unless i can put an if statement within an if statement… wich i havent tried yet but see as asking for problems i have to find a way to make a variable be a letter

https://www.arduino.cc/en/Reference/Char

2 Likes

well thanks for the prompt reply guess your a better googler than i and for that i bow to you sir

2 Likes

I know you already solved this, but I’m guessing from your code that what you actually wanted was compass directions.

Instead of using char you’d be better off using an enum class.
(I apologise in advance if this is a lot to take in.)

For example:

enum class Direction : char { None, North, South, East, West };

Which you can then use like:

Direction x;
if (y == 0) { x = Direction::North; }
else if (y == 1) { x = Direction::South; }
else if y == 2 { x = Direction::East; }
else if == 3 { x = Direction::West; }

Or better yet:

Direction x;
switch(y)
{
    case 0: x = Direction::North; break;
    case 1: x = Direction::South; break;
    case 2: x = Direction::East; break;
    case 3: x = Direction::West; break;
}

Even better than that, wrap that in a function:

Direction NumberToDirection(const char & c)
{
    switch(c)
    {
        case 0: return Direction::North;
        case 1: return Direction::South;
        case 2: return Direction::East;
        case 3: return Direction::West;
        default: return Direction::None;
    }
}

And use it like x = NumberToDirection(y);.

An enum is basically the same as a number (char, int, etc), but instead of having ‘magic numbers’ with a non-obvious meaning, each value is given a name and you don’t have to worry about what the numbers actually are. In addition they have what is called ‘type safety’. An enum class value can only be assigned to a variable of the same type.

e.g.

enum class Direction { North, South, East, West };
enum class Colour { Red, Blue, Green };
Direction variable = Colour::Red; // Won't compile

If you want, you can also say what number you want each value to represent. Using the direction example again:

enum class Direction : char
{
North = 0,
South = 1,
East = 2,
West = 3
};

Which means you can then do:

Direction x = static_cast<Direction>(y);

So that big chain of if statements can be reduced to a single static_cast. Make sure that y is in the correct range though, using a value other than 0, 1, 2 or 3 could cause errors.

2 Likes

Alternative answer that still deals with letters that you can display.

char x = (y >= 0 && y<4) ? "NSEW"[y] : '?';

:smile: really depends on your intended use. Pharaps answer is very good, fyi. Understanding it will help you out a lot.

1 Like

I admit my comment would be of more use to experienced programmers, but that’s one of the joys of programming - if you don’t understand it now you can always come back later on when you can understand it and feel even better about how much progress you’ve made.

1 Like