Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Making things the hard way
#1
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.

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! Laughing + Tongue sticking out

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! Laughing

Nope, you wulfo, I'm not talking about some Texan TV animated series at all! Angry
"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.

[Image: SP1-Scripter.png]
[Image: SP1-Writer.png]
[Image: SP1-Poet.png]
[Image: SP1-PixelArtist.png]
[Image: SP1-Reporter.png]

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! Laughing + Tongue sticking out

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
Reply }


Messages In This Thread
Making things the hard way - by kyonides - 09-24-2018, 02:28 AM
RE: Making things the hard way - by DerVVulfman - 09-24-2018, 04:31 PM
RE: Making things the hard way - by MetalRenard - 09-25-2018, 12:17 AM
RE: Making things the hard way - by DerVVulfman - 09-25-2018, 03:24 AM
RE: Making things the hard way - by kyonides - 10-01-2018, 07:59 AM
RE: Making things the hard way - by DerVVulfman - 10-02-2018, 03:10 AM
RE: Making things the hard way - by kyonides - 10-02-2018, 01:24 PM
RE: Making things the hard way - by DerVVulfman - 10-02-2018, 11:59 PM
RE: Making things the hard way - by kyonides - 10-03-2018, 12:55 AM
RE: Making things the hard way - by DerVVulfman - 10-03-2018, 03:36 AM
RE: Making things the hard way - by kyonides - 12-10-2021, 08:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
   Making a "Wait Until Button Input" PK8 1 5,693 05-28-2012, 12:50 PM
Last Post: yamina-chan
   Making a Successful comedy RPG DerVVulfman 0 4,853 05-17-2011, 05:18 AM
Last Post: DerVVulfman
   The Ultimate Guide for Making Nifty Fantasy RPGs by Magus Masque DerVVulfman 5 10,331 06-07-2010, 02:44 PM
Last Post: sakhawat21
   Making an Impact, Part 3 Kaos Tenshi 0 3,509 01-19-2009, 05:33 PM
Last Post: Kaos Tenshi
   Making an Impact, Part 2 Kaos Tenshi 0 3,459 01-19-2009, 05:30 PM
Last Post: Kaos Tenshi
   Making An Impact, Part 1 Kaos Tenshi 0 3,534 01-19-2009, 05:26 PM
Last Post: Kaos Tenshi
   Rose Skye's Game Making Tips RoseSkye 0 3,340 12-06-2008, 05:06 PM
Last Post: RoseSkye



Users browsing this thread: