Skip to content

Instantly share code, notes, and snippets.

@brianisbrilliant
Created March 17, 2021 16:23
Show Gist options
  • Save brianisbrilliant/6c44f5acb85d40570ca590d1f54aedbd to your computer and use it in GitHub Desktop.
Save brianisbrilliant/6c44f5acb85d40570ca590d1f54aedbd to your computer and use it in GitHub Desktop.

I think perhaps you mean "how do I start writing the code of a program?" and I think the answer you may be looking for is pseudocode.

Programming requires us to take "the problem we want to solve" and break it into singular instructions that our computer can solve.

An initial question might be "how many days old am I?" or "I want to build a program that calculates a person's age in days instead of years." so the pseudocode works like this. I would literally start building my program with this chunk of text.

// what do I want to do?
// I want to ask the user for their age
// then I want to multiply it by 365.25 (that's how many days are in a year)
// then I want to tell the user how many days old they are.

So I'll start building my code underneath this code:

// I want to ask the user for their age
cout << "Hi! How many years old are you?\n";
int ageInYears = 0;
cin >> ageInYears;

// then I want to multiply it by 365.25 (that's how many days are in a year)
int ageInDays = ageInYears * 365.25;
// then I want to tell the user how many days old they are.
cout << "On your last birthday you were " << ageInDays << " days old!\n";

I realized as I was making it that it would only give me the accurate number of days ON the user's birthday, so I had to adjust how I displayed the data. You can try it out here: https://repl.it/@bfoster6/AgeInDays#main.cpp (Links to an external site.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment