Friday, March 9, 2012

Really Basic programs + learning arrays!

Anyone who has ever taken a course in programming knows all about hello world. It's a program that essentially is just a statement that prints out the words "Hello World! My name is [insert name here]!" I'm going to demonstrate this one. It's very easy.


C++ version first

#include (Note: This allows us to print out using cout and take input using cin.)

using namespace std;

void main(){
cout << "Hello World! My name is Everett!"<< endl;
}

javaversion

public static void main(String[] args){
System.out.println("Hello World! My name is Everett!");
}

Both of these programs do the same thing. They just simply print out the words inside the quotations.

Time to introduce the String type. The words between the quotations are considered a temporary string. A string is a collection of characters. It can be multiple lines, it can be one line, it can be empty, and it can be one letter. Pretty much anything can be stored in a string.

Another type is int. Int is short for Integer. As we all learned in arithmetic an integer is a whole number. It's the same in programming.

The next type is double. A double is what we would categorize as any possible number. It doesn't have to be whole, it can have .5 in it. It is any value. This has its advantages and its disadvantages as well.

And the next main type is Bool. A bool, or Boolean, is a statement either true or false. No ifs or buts about it. It is either TRUE or it is FALSE. In C++ it is either a 0( indicating false) or anything else. It can be a word. It can be a negative value. it can be anything.

These four data types will take care of just about every problem you can come by.

Now if you needed to make a new data type. Let's say you are the boss of somewhere and you need a list of people. And each of those people have a few specific things to each of them. Those things being; the amount of hours they work, their pay rate per hour, and their name. Also, for simplicities sake lets say its up to 100 workers. We are going to use arrays.

An array is essentially a list of things. When you declare an array you have to declare how big it is. So lets say we want to go shopping and we can shop for up to ten things. Our array would be
string shop[10];

Think about it like a piece of paper with 10 lines on it. You can't add any more lines to it. And you can't remove any lines from it.

So in order to make a data type in C++ you would need to make a struct.

The format for making a struct is.

struct NAME{
variables
variables
variables
}NAMES;
In java you make a new class with the name of the data type. So lets say you make a class called people.

public NAME{
variables
variables
}

Now lets fill it in for how we want it for this boss.

Quick thing about the cin / cout stuff in c++. When you use a cin. You assume that you are taking in input from the command line. The user will input something in. They will almost ALWAYS mess it up. If you put a string in where you ask for an integer.... scary things happen. Thank goodness for the checks we have. We can use cin.fail() to see if they put that in. Also we can check the input to make sure it is valid.

Another thing about cin. Let's say you are asking for a string. A one word string. They in put the word "Cheese". You store that. But what they really typed is "Cheese\n" The cin doesn't grab the \n. It just leaves it there for you to deal with later. In order to get rid of it you want to use cin.ignore(1000, '\n'). I choose 1000 because its a large number. You can put any large number in there. And I am using '\n' because that is what I want to ignore.

Now in order to get multiword input on a single line you use
getline(WHERE FROM, Store where?);
So an example would be
string wholename;
getline(cin , wholename);

The where from dictates that we are getting it from the C input. Which is the command line in this situation.



C++ FILE STARTS HERE.

#include
#include

using namespace std;

struct people{
string name;
int hours;
double payrate;
}list[100] , employee;

void main(){
int size = 0;
int n = 0;
cout << "Welcome to the employee listings!" << endl;
while( n != 4)
{
cout << "Please select an option" <> n;
while(cin.fail() || n > 4 || n <= 0){
cout << "Please try again" << endl;
cin >>n;
}
if( n == 1) {
cout << "What is the new employees name?" << endl;
string temp;
cin >> temp;
while(cin.fail()){
cout << "Please try again" << endl;
cin >>temp;
}
employee.name = temp;
cout << "How many hours do they work?" << endl;
int n;
cin >> n;
while(cin.fail()){
cout << "Please try again" << endl;
cin >>n;
}
employee.hours = n;
double m;
cout << "How much do you pay them?" << endl;
cin >> m;
while(cin.fail()){
cout << "Please try again" << endl;
cin >>m;
}
employee.payrate = m;
list[size] = employee;
size++;
}
if(n == 2)
{
cout << "What's their name?" << endl;
string temp;
cin.ignore(1000, '\n');// <--- this clears the buffer from the earlier cin. When cin grabs a value it leaves a value in the buffer. The buffer is what your program looks at when you use a cin.
getline(cin, temp); //<----- This function grabs a whole line of words. the regular cin just grabs one object until it finds whitespace.

for(int m = 0; m < size; m++)
{
if(list[m].name == temp)
{
for(int x = m; x < size - 1; x++)
{
list[x] = list[x + 1];
}
size = size - 1;//(can be shortened to size--;)
}

}
}

if( n == 3 )
{
for(int m = 0; m < size; m++)
{
cout << list[m].name << " is paid " << list[m].payrate << " and he / she works " << list[m].hours << " a week. " << endl;
}
}

}

}

Now that's the end of the C++ code. It may seem a bit daunting. But its only using a few things that I've talked about here. Remember that this is low level programming. I have not shown how to deal with text files and everything else yet.

Also, this code is not efficient. It also doesn't use anything very dynamic yet. We will talk about vectors, arraylists, maps, hashmaps, sets, treesets, trees, and much more in the future. This is just to show how I plan to discuss and to show you these things.

AND I CAN'T FIGURE OUT HOW TO DO THE FORMATTING ON THIS BLOG D:

Thank you for reading!

No comments:

Post a Comment