James's profileElroymanBlogListsGuestbookMore Tools Help

Blog


    March 09

    Some Simple Specifics that I might have known...

    Well, I've been spending a little time going back over some programming stuff...just trying to relearn what I might have known but have lost along the way.  Below I found some specifics about variable declarations that aren't quite so obvious to the beginning programmer.

    Aside from the regular declarations and calculations that can be done, A variable can also be signed or unsigned. Signed means that it can have negative numbers and unsigned means it can't but unsigned gives a variable a greater positive range. You simply put the words signed or unsigned in front of a variable declaration to use them.

    unsigned int i;

    Putting short or long in front of a variable gives it a smaller or bigger range respectively.

    short int i;

    Using signed, unsigned, short and long without a variable type will use int by default.

    signed s;
    unsigned u;
    short sh;
    long l;

    A string is a type of variable that can store words and sentences. There are 2 kinds of strings. The first kind is a string pointer. You declare it as a *char. The * means that it points to the first char of the string.

    char *s;
    s = "Hello";

    The second kind is an array of characters which you must give a size when you declare it. You have to use the strcpy command to put values in it. You must also include the string header file to be able to use the strcpy command.

    #include<string>
    ...
    char t[10];
    strcpy(t,"Hello");

     

    You can also declare constants much as in other languages.  I've been using fortran in a class at University, in which you can declare constants which cannot be changed by adding the declaration ", PARAMETER" at the point of assignment.  C++ is much more straightforward.

    The value of a constant must always be set when it is declared. There are 2 types of constants. The first uses the word const in front of the declaration.

    const PI = 3.14;

    The other type of constant uses #define to create a constant.

    #define PI 3.14

    March 02

    CINful quirks

    Well, I was working on a problem for www.eulerproject.net and found and interesting little something within the cin function.  The cin command allows the programmer to take an input from the user and assign it to a variable.  Simple enough.  First you would ask for something using the cout command and then use the cin thusly:

    cout<<"something something something...just write something and press enter.";

    cin<<x;

    Its pretty straightforward and used to the point that it becomes second nature.  I did find something that is a little strange about it.  I was writing a loop, with iterations defined by the user.  This being the case I was testing some lines of code.   I had written something to the screen and then planned to wait for an enter key, or something else, before actually giving the instruction to the user to input some information.  I had been using the cin.get() form of the command, trying to help myself take it step by step. 

    So when I went to use the variable, which was an int type, I kept getting an error telling me that there was a problem with my int variable.  Apparently the variable was having some distinctly char characteristics and could not be used as an int in the way I had initialized it.

    Long story short, cin.get(), while while keeping the screen from closing, has an annoying tendency to include the enter key at the end of the variable declaration.  Use cin.ignore();.  Here is what I had when calling for the input before.

    //int nthPrime;
    //cout<<"input the nth prime number you would like to find: ";
    //cin >> nthPrime;
    //cin.get();
    //cout<<"\n you typed "<< nthPrime;
    //cin.get();

    and after:

    //int nthPrime;
    //cout<<"input the nth prime number you would like to find: ";
    //cin >> nthPrime;
    //cin.ignore();
    //cout<<"\n you typed "<< nthPrime;
    //cin.get();

    The difference (in the fourth line of code) was 4 errors avoided, and a working program.

    I never did actually finish the eulerproject problem, but I learned a couple of interesting things.  Maybe sometime when I can't think of anything more interesting than flushing dead fish down the toilet, I'll get into my other mundane discovery, the math.h header