Pi Monkeh Dot Net
main
Pi Monkeh!
c++
page 1    page 2     page 3    page 4    page 5    page 6    page 7    page 8

let's look at storage. this lesson contains three types of storage, the second will be more of a novelty to you at this point in your life of programming.

code:
#include <iostream>
#include <string>    //new header file!
using std::string;

string storage[10];
string input;

int main()
{
    for (int i = 0; i < 5; i++)
    {
        std::cout << "string: ";
        std::cin >> input;
        storage[i] = input;
    }
    for (int j = 0; j < 5; j++)
        std::cout << storage[j] << "\n";
}

wow! what the hell did we just do? let's find out.

#include <string>    //new header file!
not only did i include the string header file, i showed you how to make a comment. any line that starts with // (or any block of text that starts and ends with /* and */) is a comment, ignored by the compiler and helpful to the human. anyway, the string header allows us to make strings! strings are variables that are encased in quote marks, basically. a string lets you enter something besides a number, such as a name or something.

string storage[10];
what? in this page, i've introduced you to an array. an array is a set of variables, all of the same type. in this example, we made an array of strings, to store all the user's input in. you access an element of an array like so: arrayname[element]. the element is either a number or a variable that is a number. arrays start at 0 (keep this in mind) so both i and j did as well.

storage[i] = input;
this is a review of what i just told you. we set the #i element of the storage array to be whatever input is at the time.

code:
#include <iostream>
#include <vector>

std::vector<int> storage;

int main() {
    for (int i = 0; i < 10; i++)
        storage.push_back(i);
}

vectors. they look complicated, but they really aren't. they're more fun to work with than arrays usually, because they come with their own functions (like push_back() for instance). you use a vector much like you use an array, minus the accompanying functions. you access the elements the same way, vectorname[element]. if you're curious as to how many things you've put in the vector, use vectorname.size(). there are a bunch of vector functions, you can look them up yourself since i'm only here to teach you the basics. this was a treat, grovel for me.

code:
char name[9] = "peaceofpi";

this is an array of char, it can hold 9 elements and it's quite full right now. elements 0-8 are p e a c e o f p i and element 9 is \0, the null character which is used to end all c-style strings. what are c-style strings? you just looked at one. before std::strings were made, all strings had to be in a char array of the right size. if you go out of bounds with your array of char (say, element 10 from name[9]), trouble in river city*.

page 1    page 2     page 3    page 4     page 5    page 6    page 7    page 8