Strings

A String is an array of characters; like a word or a sentence.

Here’s an example:


#include <iostream>

using namespace std;

int main()
{
 //Declare string size 100.
 char string[100];

 //Prompt user to input a word.
 cout << "Please enter a word." << endl;

 //Reads input, store in string, \n waits for user to push enter.
 cin.getline(string, 100, '\n');

 //Outputs first character in string.
 cout << "Your word started with a: " << string[0] << endl;
 cin.get();
}

Advertisements

Arrays

I just finished a course in Python which I used lists for everything. So I couldn’t wait to learn arrays.

But C++ does not make it easy… where’s my .sort function?

An Array is a list of elements, each with it’s own index; kind of like a set of shelves.

Here’s a simple example:


#include <iostream>

 using namespace std;
 int main()
{
 //DECLARED A NEW ARRAY, TYPE INT, SIZE 8.
 int array[8];

 //ASSIGNING VALUES 0-7 TO MY EIGHT ELEMENTS
 for(int x=0; x<8; X++)
 cin>> ;

 //OUTPUT ARRAY TO SCREEN ONE ELEMENT AT A TIME
 for(int x = 0; x<8; x++)
 cout<<array[x];

 return 1;
}

Note: I have only used type int here, and I can use other numerical types easily. I will write another post on using characters to make words. For now, I don’t need them.

Note II: Next post, how to sort!

Note III: Just for fun, here’s a 2D array.


//DECLARING A 2D ARRAY

int twodimensionalarray[8][8];

//HOW TO ACCESS EACH ELEMENT

twodimensionalarray[arrayindexnumber1][arrayindexnumber2] = someInt;

Switch Case

At university I was lazy when it came to programming, and relied on IFs, FORs and WHILEs, and didn’t get much further.

Here’s a switch statement. It’s kind of like a IF statement.


int a;

int b;

int c;

switch (a) {

case b:

//code

break;

case c:

//code

break;

default:

//code

break;

}

A quick explanation:

If a is equal to Case b do the code, etc. The breaks are there to exit the loop, otherwise the program will fall to the next one, and eventually output the default too. The default is like else.

A good example of using the Switch Case is a cola machine; the user has to input the number assigned to the drink.


#include <iostream>

using namespace std;

int main()
{
 int input;

 cout<<"1 Coke\n";
 cout<<"2 Water\n";
 cout<<"3 Sprite\n";
 cout<<"4 Fanta\n";
 cout<<"5 Pocari Sweat\n";
 cin>> input;
 switch (input) {
 case 1:
 cout<<"Your Coke is ready!";
 break;
 case 2:
 cout<<"Your Water is ready!";
 break;
 case 3:
 cout<<"Your Sprite is ready!";
 break;
 case 4:
 cout<<"Your Fanta is ready!";
 break;
 case 5:
 cout<<"Your Pocari Sweat is ready!";
 break;
 default:
 cout <<"Error. choice was not valid, here is your money back.\n";
 break;
 }
 cin.get();

}

 

Functions

When I was at university I had a mental block when it came to functions. I’m slowly getting used to them.

They are important. We take them for granted, especially in a language such as Python, every line I wrote included a built in function. It’s a good idea to use functions, it’s more efficient and when it comes to the next time you want to use a similar method, you already have your template.

I’m going to keep this brief, as I’m not 100% confident with functions yet. Here’s an example of an Addition Function:


#include <iostream>

using namespace std;

/* This function takes in two arguments a and b.

Then calculates the sum and return the answer */

int addition(int a, int b)
{
 int r;
 r = a + b;
 return r;
}

int main()
{
 int z;
 z = addition(5,3);             // Calling the Addition function
 cout << "The result is " << z;
}

Just a note, the addition function is type int because it returns an integer. A function with type void has no return value.

While Loops

While Loops can be used like For Loops, however, they have some differences.

Here’s the syntax:


while (condition){

// code

}

or


do{

} while(condition);

To be truthful, I’ve never used the second example.

While Loops can be used in a similar way to For Loops; as an example, while x < 10, do some thing, then add one to x each loop.

However, the difference is that you don’t have to use it for iterating.

How about this: you have 10 cups, and there’s a marble under one of them, the computer has to find the marble. A For Loop would look under all 10 cups before finishing, whereas, a while loop would stop the loop when it found the marble. This doesn’t make much difference with such small number, but imagine the difference over a thousand cups.

This example below is a program that generates a random number between 0-100. The user than has to guess the number, as program tells the user higher or lower. It’s called a Bracketing Search.


#include <iostream>

using namespace std;

int main()
{

int guess = 0; // users guess.
int t = 0; // number of tries.

srand(time(0)); // creates a seed based on the ever changing time.
int r = 1+ rand() % 100; // generates a rand # between 1-100.

// while guess is not the random number
//and number of tries is less than 10.
while (guess != r && t < 10)
{
 cout << "Guess a number between 1 and 100." << endl;
 cin >> guess;

if (guess > 0 && guess < 100)
 {
 if (guess == r)
 {
 cout << "You win! You guessed " << r << "!!!" << endl;
 }
 else if (r > guess)
 {
 cout << "HIGHER" << endl;
 }
 else
 {
 cout << "LOWER" << endl;
 }
 t ++;
 }
 else
 {
 cout << "Invalid Input" << endl;
 }
}

cout << "Number of tries: " << t << endl << endl;

if (t == 10)
{
 cout << "Sorry no more chances!" << endl;
}

}

For Loops

Firstly I’d say I didn’t know I had to press Ctrl C to stop a program running. When I saw my first, second and third infinite loop I pooped my pants… and clicking the x in the corner closes everything, very annoying!

So here’s how a For Loop is set up, followed by an example.

I always learn more playing with examples and figuring out myself.


for (variable initialization; condition; variable update)

{

/* Do some awesome thing while the condition is true.

}

Example of a For Loop:


for ( int x=0; x<10; x++)

{

cout << x ;// code runs, adds 1 to x each time, stops when x=10.

}

Outputs:


0123456789

If, else…

I’m a teacher by profession and I feel like these two words come out of my mouth far too often! But they’re really useful and I can actually start writing programs now ^^*.

Simply, if somethings true, do this. Else if that’s not true and this other thing is true, do this. Else, do this other thing. Here’s simple code I made.


#include <iostream>

using namespace std;

int main()
{
 int score = 0;
 cout<<"What's your score out of 100? \n";
 cin>> score;

 if (score == 100)
 {
 cout<<"You got a perfect score!";
 }
 else if (score > 90 && score <100)
 {
 cout<<"You scored an A";
 }
 else if (score > 80 && score <90)
 {
 cout<<"You scored an B";
 }
 else if (score > 70 && score <80)
 {
 cout<<"You scored an C";
 }
 else if (score > 60 && score <70)
 {
 cout<<"You scored an D";
 }
 else if (score >= 0 && score <60)
 {
 cout<<"You scored an F";
 }
 else
 {
 cout<<"Did you enter a number between 0-100?";
 }

}

I got a little head of myself when I wrote this and started using Boolean operators; AND &&, OR || and NOT !. NOT confused me at first but a good example is !(TRUE) = FALSE.