Skip to content

Instantly share code, notes, and snippets.

@matxpg
Created October 1, 2014 02:02
Show Gist options
  • Save matxpg/119f3cbbfe75d1cc4235 to your computer and use it in GitHub Desktop.
Save matxpg/119f3cbbfe75d1cc4235 to your computer and use it in GitHub Desktop.
CMPT-111 demo
//Just run this code in eclipse or whatever you have set up
/* MOST IMPORTANT THINGS I HAVE TO SAY:
* 1: Be careful! There are a lot of SMALL mistakes that can be hard to catch
* eg. every { has a }
* this may cause the program not even to compile and run
* -Every statement has a ; at the end
*
* Mistakes like forgetting a ;
* have caused me to spend hours trying to figure out why seemingly perfect code wasn't working
* and then when you realize you've forgotten a semicolon and the code works perfectly
* it's a (╯°□°)╯︵ ┻━┻ moment
*
* Anyways. Be careful! Keep an eye out for this kind of mistake.
*
* Another mistake I've seen once or twice with other 111 students in the last week has an easy fix,
* -cout uses <<
* -cin uses >> and they are NOT interchangeable!!!!
*
*****************************************************
* 2: Order of operations
* When you manipulate numbers, like two integers x, y,
* THERE ARE ORDER OF OPERATIONS!
* x + y / 2 is equal to x + (y/2),
* NOT equal to (x + y) / 2
*
*This is because *, /, % are calculated before +, -
****************************************************
* 3: Getting user input.
* Whenever you need the user to enter something, you NEED a place to store what they will enter.
* So for example, before you use
* cin >> someInteger;
* to get an integer from a user,
* you need to have declared a variable of type int called someInteger.
* int someInteger;
* cin >>someInteger;
* so in your code, can be broken into
* //Variable declarations BEFORE input
* int x, y, z;
* string a;
* string b;
* ...ETC
* //User input
* cin >>x;
* cin >>y >> z;
* ...
* //More code using the variable
*
*****************************************************
*3: BREAKING DOWN PROBLEMS!
* Read the question carefully. If the question involves getting information from the user,
* doing something with that information,
* Generally the code miiiiiight look something like this:
* //1. Declare variables you need.
* int someNumber;
* string someString;
* //now that you have variables, you can read user input into them!
*
* cout <<"Please enter a number and a word, seperated by spaces." <<endl;
*
* cin >>someNumber >>someString;
*
* 4. Do what you need to do with the variables.
* //more code
*/
#include <iostream>
using namespace std;
int main() {
//In this program, there will be
/*Examples of input and output*/
cout <<"Hello, how are you?"<< endl;
//is the same as:
cout <<"Hello, " <<"how " << "are " << "you?" <<endl;
string greeting = "Hello, ";
// is the same as:
cout <<greeting <<"how are you?" <<endl;
/*An important thing to note:
* You can mix together "Things you write" and data from variables you already have when you output to the screen.
* This next example will demonstrate that.
*/
//Two Sample problems
//Problem 1: Ask a user for their first name, last name, and age, and then output the user's information with
//the data entered.
cout <<endl <<endl <<endl <<"Problem 1:" <<endl;
//My approach to the problem:
//1. What do I need? Declare the variables for the data you need to use: first name, last name, and age
string firstName;
string lastName;
int age;
//2. Get the data from the user:
//Use cout to display to the user what you want,
//and then use cin to get the user input and store it in variabels you declared.
cout <<"Please enter your first name: " <<endl;
cin >>firstName;
cout <<"Please enter your last name: " <<endl;
cin >>lastName;
cout <<"Please enter your age (in years, no decimals): " <<endl;
cin >>age;
//3. Output the sentence
cout <<"Thanks! Here is your information:" <<endl <<endl;
//First:
//What do I want to tell the user?
//If I want to write:
//"Your name is ___ ___, and you are __ years old"
//the ___ is where variables I already have will go. Break it down into multiple <<___'s
cout <<"Your name is " <<firstName <<" " <<lastName <<", and you are " <<age <<" years old." <<endl;
//Double check that each thing you write has a opening and closing ", and that there's a semicolon at the end
//those are easy mistakes that can be harder to catch at first!
//If I want to write:
//Name: ___ ___
//Age: __
//It would look like:
cout <<"Name: " <<firstName <<" " <<lastName <<endl //Note the space between the names, and a new line (endl)
<<"Age: " <<age <<endl;
//For the above, I only used cout once - so there is only one semicolon. I could have done:
//cout <<"Name: " <<firstName <<" " <<lastName <<endl;
//cout <<"Age: " <<age <<endl; //The semicolon is the end of the cout
//So you can use one cout and multiple lines, or multiple cout's, just make sure that there's one and only ONE ; for each cout you use.
cout <<"End of problem 1...\n Problem 2: \n";
//Problem 2: Declare integer variables x0, x1, x2, x3
//and get the input from a user,
//and then output the following equation:
// ((x0 + x1)/2 + x2/5 + x3*4) % 5
//So this is a mess. Step one:
//DECLARE THE VARIABLES! you need a place to store the user input.
int x0, x1, x2, x3;
// you could also do
// int x0;
// int x1;
// int x2;
// int x3;
//Next, get the input!
cout <<"Please enter four integers (numbers without decimals) seperated by spaces: " <<endl;
cin >>x0 >>x1 >>x2 >>x3;
//now that we have the data, let's break down the equation
// The order of operations is important. saying:
// x0 + x1 / 2
// would be the same as
// x0 + (x1/2)
//This is because of the order of operations:
// *, /, % happen before +,-
//I could write:
//cout <<"((x0 + x1)/2 + x2/5 + x3*4) % 5 is: " <<endl;
//but instead, I'll write the same thing, but use the numbers the user entered.
cout <<"((" <<x0 <<" + " <<x1 <<")/2 + "<<x2 <<"/5 + "<<x3 <<"*4) % 5 is: " <<endl;
cout << ( ((x0 + x1)/2) + (x2/5) + (x3*4) ) % 5;
//If I wanted, rather than doing the calculations inside of the cout statement,
//I could first declare a variable equal to the result of the calculation
//int result = ( ((x0 + x1)/2) + (x2/5) + (x3*4) ) % 5;
//and then display it:
//cout << result <<endl;
//and it would be the same.
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment