Sometimes we want to do things the hard way because we either ignore what we are doing or somebody is hiding something from us or because nobody was thinking out of the box. I bring you an example on C++ and Ruby.
Here's the first part of our C++ code that will enable our IDE or stuff that makes the compiling and linking needs badly.
I won't enter into details up there for a good reason.
Let's try to make a number print the month we picked
Then you should just make some int main function to include the call to this function of ours and display the result on our console alias CMD (MSDOS shell) or Konsole (if using KDE based GUI for Linux distros).
Then you would a text telling you "Current Month is (number) or (month name)."
[rant]Obviously including the break statements after each case is a pain you know where![/rant]
Then somebody tells you there's an easy way to deal with it... Really? Then why didn't you tell us about it from the very beginning!?
But why would we make things that complicated? Use a ternary condition instead!
Really, whoever can't see the beauty of ternary conditions is dead meat already!
Here's the same code in Ruby:
It would print out...
"Current Month is 9 or better known as September."
Again there's an easy way to deal with this...
And you would get the very same output as in the example above. And yeah, Ruby is a cutie!
Nope, you wulfo, I'm not talking about some Texan TV animated series at all!
Here's the first part of our C++ code that will enable our IDE or stuff that makes the compiling and linking needs badly.
Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
I won't enter into details up there for a good reason.
Let's try to make a number print the month we picked
Code:
const char* int2month(int month)
{
cout << month << " or ";
const char* pointer_value;
switch(month)
{
case 1: pointer_value = "January";
break;
case 2: pointer_value = "February";
break;
case 3: pointer_value = "March";
break;
case 4: pointer_value = "April";
break;
case 5: pointer_value = "May";
break;
case 6: pointer_value = "June";
break;
case 7: pointer_value = "July";
break;
case 8: pointer_value = "August";
break;
case 9: pointer_value = "September";
break;
case 10: pointer_value = "October";
break;
case 11: pointer_value = "November";
break;
case 12: pointer_value = "December";
break;
default: pointer_value = "Invalid value!";
}
return pointer_value;
}
Then you should just make some int main function to include the call to this function of ours and display the result on our console alias CMD (MSDOS shell) or Konsole (if using KDE based GUI for Linux distros).
Code:
int main()
{
cout << "Current Month is " << int2month(9) << "." << endl;
}
Then you would a text telling you "Current Month is (number) or (month name)."
[rant]Obviously including the break statements after each case is a pain you know where![/rant]
Then somebody tells you there's an easy way to deal with it... Really? Then why didn't you tell us about it from the very beginning!?
Code:
const char* int2month(int month)
{
cout << month << " or ";
const char *const months[] = {
"invalid",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
if (month < 1 || month > 12)
{
return "invalid";
}
return months[month];
}
But why would we make things that complicated? Use a ternary condition instead!
Code:
const char* int2month(int month)
{
cout << month << " or ";
const char *const months[] = {
"invalid",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
return (month < 1 || month > 12) ? months[0] : months[month];
}
Really, whoever can't see the beauty of ternary conditions is dead meat already!
Here's the same code in Ruby:
Code:
def int_month(month)
current_month = case month
when 1 then "January"
when 2 then "February"
when 3 then "March"
when 4 then "April"
when 5 then "May"
when 6 then "June"
when 7 then "July"
when 8 then "August"
when 9 then "September"
when 10 then "October"
when 11 then "November"
when 12 then "December"
else "invalid"
end
puts "Current Month is #{month} or better known as #{current_month}."
end
int_month(9)
It would print out...
"Current Month is 9 or better known as September."
Again there's an easy way to deal with this...
Code:
def int_month(month)
months = ["invalid",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"]
month_name = month.between?(1, 12)? months[month] : months[0]
puts "Current Month is #{month} or better known as #{month_name}."
end
int_month(9)
And you would get the very same output as in the example above. And yeah, Ruby is a cutie!
Nope, you wulfo, I'm not talking about some Texan TV animated series at all!
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE