PSP Hacks - Forums

Go Back   PSP Hacks - Forums > PSP Community > PSP Programming & Development

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 03-11-2009, 11:28 PM
Furypaw's Avatar
Furypaw Furypaw is offline
Programmer
PSP Veteran
 
Join Date: Jul 2007
Location: Furypaw.rar
Posts: 1,058
Furypaw is on a distinguished road
Default Furypaw's C/C++ beginner tutorials

I decided I would write a few tutorials on basic things like variables, loops, formatting text etc, so enjoy.

Variables
Loops
Formatted Text

Sample code:
Spoiler for Simple PC Calculator:
Just a simple calculator I made for basic stuff. And I know, the source code is ugly

Code:
#include <stdio.h>
#include <stdlib.h>

int a = 0;
int b = 0;
int use = 0;
char command;

int add(){
    use = 1;
    printf("\nPlease enter the first integer:");
    scanf("%d", &a);
    printf("\nPlease enter the second integer");
    scanf("%d", &b);
    int sum = a+b;
    printf("\nThe sum of %d + %d = %d", a, b, sum);
}

int subtract(){
    use = 1;
    printf("\nPlease enter the first integer:");
    scanf("%d", &a);
    printf("\nPlease enter the second integer");
    scanf("%d", &b);
    int sum = a-b;
    printf("\nThe sum of %d - %d = %d", a, b, sum);
}

int multiply(){
    use = 1;
    printf("\nPlease enter the first integer:");
    scanf("%d", &a);
    printf("\nPlease enter the second integer");
    scanf("%d", &b);
    int sum = a*b;
    printf("\nThe sum of %d x %d = %d", a, b, sum);
}

int divide(){
    use = 1;
    printf("\nPlease enter the first integer:");
    scanf("%d", &a);
    printf("\nPlease enter the second integer");
    scanf("%d", &b);
    int sum = a/b;
    printf("\nThe sum of %d divided by %d = %d", a, b, sum);
}

int main(){
    printf("Simple calculator\n");
    printf("%18s\n", "By Dark_Sabre");
    printf("---------------------------------------------------------------------------\nA for addition, S for subtraction, M for multiplication and D for division.\n%75s\n", "Press Q to quit.");
    while(1){
             command = getchar();
             if(command == 'A' || command == 'a')add();
             if(command == 'S' || command == 's')subtract();
             if(command == 'M' || command == 'm')multiply();
             if(command == 'D' || command == 'd')divide();
             if(command == 'Q' || command == 'q')break;
             a = 0;
             b = 0;
             if(use == 0)printf("\n---------------------------------------------------------------------------\nPlease enter a new command (A,S,M,D,Q)\n");
             use = 0;
    }
    return(0);
}
There isn't much new introduced here. getchar(); gets a character from the keyboard, break; exits the loop (and effectively the program), although something you may find unusual is this:
Code:
printf("\nThe sum of %d divided by %d = %d", a, b, sum);
The first bit is your normal newline and then text. But what are those %d's you might ask? They are a placeholder pretty much for integer variables, which I have put in order from the ending " symbol.

You could if you wish use unsigned int's or long int's or even unsigned long int's if you need larger numbers. But remember, with unsigned, you cannot go into negative numbers.


Spoiler for Guess the number game:
Code:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string> // All the usual libraries here - nothing new.
#include <time.h>

using namespace std;

string name;
int guess;
int level;
int n;
int c;

int main(){

    system("color 17");

    srand((unsigned)time(NULL));
    
    n = rand() % 50 + 1;

    cout << "Please enter your name: ";
    cin >> name;
    cout << "Hello, " << name << "!";
    
    while(1){
        cout << "\nWhich level would you like to play? (1-3)" << "\n1 - Beginner \n2 - Intermediate \n3 - Hard: ";
        cin >> level;
        if(level < 1 || level > 3)cout << "Enter a valid number!";
        else break;
    }
    
    cout << "\n" << "Guess the number under 50: ";
    
    for(c = 1; guess != n; c++) /* For is a style of loop. It simply repeats until the condition is no longer true. 
                                         The first piece, before the first semicolon, is what it does the first time through.
                                         The second piece is the condition, in this case, while guess isn't equal to 27 
                                         (guess != 27;). The third piece is what happens each time you go through the loop, 
                                         in this case it adds one to the variable 'c', or the counter.*/
    {
        cin >> guess;
        if(guess > n)cout << "\nToo high! Try again.\n"; // The if statement simply means if something is happening, do this.
        else if(guess < n)cout << "\nToo low! Try again.\n"; // else if is simply an extension of the previous statement.
        else {                                               // On the last else, it means eveerything else to do with the variable.
                system("color 27");
                cout << "Good job! you got it right in only " << c << " tries!\n";
                break; // break takes you out of a loop, and in this case, ends the program.
        }
        cout << "You have had " << c << " tries.\n";
        if(level == 1){
                cout << "You have " << (12 - c) << " tries left.\n";
                if((12 - c) == 0){
                                cout << "I'm sorry, you lost.\n";
                                system("color 47");
                                break;
                }
        }
        if(level == 2){
                cout << "You have " << (8 - c) << " tries left.\n";
                if((8 - c) == 0){
                                cout << "I'm sorry, you lost.\n";
                                system("color 47");
                                break;
                }
        }
        if(level == 3){
                cout << "You have " << (4 - c) << " tries left.\n";
                if((4 - c) == 0){
                                cout << "I'm sorry, you lost.\n";
                                system("color 47");
                                break;
                }
        }
    }
    system("PAUSE");
    return(0);
}


Spoiler for Encryption/Decryption:
I did this today to show my friend a simple way to encrypt stuff. If you felt like it, you could make it a much larger equasion, but then you have to do it backwards again to decrypt it ^^

Code:
int x;

for(x = 0;password[x] != '\0';x++){ 
          password[x] += 4; 
          }
To decrypt just use -= instead of +=. You can use fstream to write to a file.

Now, a simple rundown:
the first part of the for statement sets the variable "x" to be 0. The next means loop while password[x] isn't equal to '\0', or the terminating character. The third part simply means add one to x each time it loops. This encryption is simple, all it does is add 4 to the character, e.g. abc123 would be efg567 encrypted.


Hope these have helped people
__________________

Last edited by Furypaw; 03-29-2009 at 01:31 AM.
Reply With Quote
  #2  
Old 03-11-2009, 11:30 PM
Furypaw's Avatar
Furypaw Furypaw is offline
Programmer
PSP Veteran
 
Join Date: Jul 2007
Location: Furypaw.rar
Posts: 1,058
Furypaw is on a distinguished road
Default

Variables
There are a few different kinds of variables. The ones I will cover here are 'int', 'float', 'double' and 'char'. I will cover more advanced ones like arrays (which char variables actually are but I won't go there now) or structs if enough people ask.

int
int is your average integer variable. It can contain numbers between -2147483648 to 2147483647 (the same as long, although you don't have to specify long like unsigned or short), 0 to 4294967295 when you put 'unsigned' before int and -32768 to 32767 with short or 0 to 65535 unsigned short. None of these can use decimals, although float can. It can be used like this:

Code:
int variable;
or, if you want to initialise the variable at the same time, like this:

Code:
int variable = 9812;

float
float is very much like int, although it can use decimals. It is precise to 7 digits. It looks like this:

Code:
float variable;
or you can initialise it like int's:

Code:
float variable = 238.2717
Be aware that that variable would be as precise as possible.


double
There isn't much to say for double, except that is is the same as float, apart from the 15 digit precision. It is used like this:

Code:
double variable;
or you can initialise it like int's:

Code:
double variable = 238.271792752545
Be aware that that variable would be as precise as possible.


char
The char variable can hold letters, or strings. It's range is about -128 to 127, about the amount of letters on your keyboard. It is used like this:

Code:
char variable[8];
9 is the amount of characters it can hold. It includes 8 characters (including 0), but the last one is always \0, or the end pretty much.(thanks BP )

it can also be initialised:

Code:
char variable[8] = "JimBob";
__________________

Last edited by Furypaw; 03-12-2009 at 12:42 AM.
Reply With Quote
  #3  
Old 03-11-2009, 11:30 PM
Furypaw's Avatar
Furypaw Furypaw is offline
Programmer
PSP Veteran
 
Join Date: Jul 2007
Location: Furypaw.rar
Posts: 1,058
Furypaw is on a distinguished road
Default

Loops
There are two different loops that I am currently aware of. The first is the 'for' loop, the second is the 'while' loop.

for:
The for loop is simple although it is slightly more powerful in my opinion then the while loop. it's basic structure is this:

Code:
for(1;2;3){
//write your awesome stuff here. Nao.
}
1:Where you initialise variable values when the user enters the loop e.g pie_eaten = 0;
2:This is where you set the condition for the loop e.g count < 0;. leave blank for a never ending loop (or until you use break)
3:What happens every time the user goes through the loop e.g count ++;



While:
The while loop is simple, and can be used if you don't need anything to happen every time it loops etc. It looks like this:

Code:
while(1){
//I said write your epic stuff here!
}
1:Where you put the condition of the loop, e.g. exit == 0;. Although if you just put 1, it turns into an endless loop.


do-while loops
These loops are simply backwards while loops, and are always executed at least once. it looks like this:

Code:
do{
//your stuff here
}
while(thisthingy == 1);

break
break is a simple function that simply exits the loop you are currently in, it makes it easier to get out of infinite loops without having defined a way of exiting normally. e.g.

Code:
int count = 0;

while(1){
printf("infinite loop :D");
count ++;
if(count == 15)break;
}
//This is where break leaves you

continue
continue is very similar to break, it simply returns you to the start of the loop without executing anything after it, e.g.

Code:
while(1){
//your code here, continue; returns you to here
continue;
//this won't be executed.
}

Thanks to lokiare1 for reminding me of break; and continue;, and Xitherun/TacticalBread for reminding me of do-while loops.
__________________

Last edited by Furypaw; 03-14-2009 at 02:59 PM.
Reply With Quote
  #4  
Old 03-11-2009, 11:31 PM
Furypaw's Avatar
Furypaw Furypaw is offline
Programmer
PSP Veteran
 
Join Date: Jul 2007
Location: Furypaw.rar
Posts: 1,058
Furypaw is on a distinguished road
Default

Formatted text
There are several thing you can do with formatted text inside printf (or sprintf,as printf isn't that great on PSP).

%.2f
This is the same as %f (to show float variables) although it rounds it to 2 d.p (decimal points for all you math dropouts )

%9s
This can be used in displaying a variety of variables, here strings (or chars). What is does is it right justifies it to 9 characters along.

Escape characters
Escape characters are used when a certain character is used for something else, like \, which is an escape character in itself, so you go \\ to show it in printf etc. \n, is a newline, \t, is a tab, and there are many others that I simply can't remember at the moment.
__________________

Last edited by Furypaw; 03-11-2009 at 11:33 PM.
Reply With Quote
  #5  
Old 03-11-2009, 11:32 PM
Xitherun's Avatar
Xitherun Xitherun is offline
Senior Member
PSP Titan
 

Join Date: Nov 2006
Location: The Void.
Posts: 4,564
Xitherun is a name known to allXitherun is a name known to allXitherun is a name known to allXitherun is a name known to allXitherun is a name known to allXitherun is a name known to all
Default

what about do...while loops?

lol, if those are even worth talking about.

if you need moar room for reserved posts, let me know.
__________________
| music is the cure |
Xylophones inspecting tortured hearts equal rambunctious unicorn's nightmares.

Reply With Quote
  #6  
Old 03-11-2009, 11:38 PM
RainMotorsports RainMotorsports is offline
Programmer
PSP Guru
 

Join Date: May 2008
Location: 0xSerbia
Posts: 2,171
RainMotorsports has a spectacular aura aboutRainMotorsports has a spectacular aura aboutRainMotorsports has a spectacular aura about
Default

Quote:
Originally Posted by Xitherun View Post
what about do...while loops?

lol, if those are even worth talking about.

if you need moar room for reserved posts, let me know.
Why would he ask u for more room for reserved posts... u just fucking posted where the next post would have been lmfao.

I wasnt gonna post for atleast 30 minutes but then i saw u come in.

Anyways i jus wanted to say "Can I Haz C++ Now :-p"
__________________
SOFTWARE: Rain's MMS Makers / PSP Grader | Rain's IPL Tool
Reply With Quote
  #7  
Old 03-11-2009, 11:39 PM
Furypaw's Avatar
Furypaw Furypaw is offline
Programmer
PSP Veteran
 
Join Date: Jul 2007
Location: Furypaw.rar
Posts: 1,058
Furypaw is on a distinguished road
Default

Ah... I forgot about that, it's pretty much a backwards while loop hold up
__________________
Reply With Quote
  #8  
Old 03-11-2009, 11:41 PM
Xitherun's Avatar
Xitherun Xitherun is offline
Senior Member
PSP Titan
 

Join Date: Nov 2006
Location: The Void.
Posts: 4,564
Xitherun is a name known to allXitherun is a name known to allXitherun is a name known to allXitherun is a name known to allXitherun is a name known to allXitherun is a name known to all
Default

Quote:
Originally Posted by RainMotorsports View Post
Why would he ask u for more room for reserved posts... u just fucking posted where the next post would have been lmfao.
i.e. I would delete my post, you tard.

lol.

anyway, nice guide.
__________________
| music is the cure |
Xylophones inspecting tortured hearts equal rambunctious unicorn's nightmares.

Reply With Quote
  #9  
Old 03-11-2009, 11:44 PM
RainMotorsports RainMotorsports is offline
Programmer
PSP Guru
 

Join Date: May 2008
Location: 0xSerbia
Posts: 2,171
RainMotorsports has a spectacular aura aboutRainMotorsports has a spectacular aura aboutRainMotorsports has a spectacular aura about
Default

TacticalBread you freaking Javatard..... Javatards fucking up C++ Tutorials since 1995.

No but seriously, Id like to learn C++ and ive had trouble in the past soo. This is fucking awesome.
__________________
SOFTWARE: Rain's MMS Makers / PSP Grader | Rain's IPL Tool
Reply With Quote
  #10  
Old 03-12-2009, 12:21 AM
Blade_punk's Avatar
Blade_punk Blade_punk is offline
Programmer
PSP Monk
 

Join Date: Nov 2006
Location: Candy Mountain
Posts: 2,741
Blade_punk will become famous soon enoughBlade_punk will become famous soon enough
Default

Quote:
Originally Posted by Dark_Sabre View Post
Code:
char variable[8];
8 is the amount of characters it can hold. Really it's only 7 though, as the last one is always \0, or the end pretty much.
That holds 8, not 7 characters
Code:
variable[0]
variable[1]
variable[2]
variable[3]
variable[4]
variable[5]
variable[6]
variable[7]
__________________

-Rock Station-
PSN: Xsjado7

Reply With Quote
Reply

 



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:17 PM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©