Friday, January 3, 2014

cpp,c++ or c gotoxy

The x and y axis of c++ or gotoxy()

Sometimes or several times the use of \t for tab, \n for new line and spacebar is very time consuming in preparing a nice and well formatted output in c++, as you read this blog you will learn how to format your output without using spacebar to align and \n to brake a line, and the most important you will learn how to use gotoxy in c++.

Before we start our discussion you must have an idea on Cartesian coordinates plane, the quadrants of (-x,+y), the quadrant of (+x,+y), the quadrant of (-x,-y) and lastly the quadrant of (+x,-y). Since this blog is focus on c++ and not in math subject in gotoxy we are only using the quadrant (+x,+y). I provide an image below so it will wash away the confusion inside you mind:

Remember the maximum value of x-axis is 80, then the y-axis is 50, negative value of coordinates is still accepted but there’s nothing change if you try to use it. As you can see the character M is place at bottom right of the window and to attain that position your gotoxy must have the values of (80,50) or gotoxy(80,50) in which x is 80 and y is 50.

gotoxy() required conio header(include) , the syntax of gotoxy is gotoxy(int x,int y) and remember the cursor will move to the position declared inside gotoxy function.

Sample codes of gotoxy in c++

#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
 gotoxy(80,50);
 printf(“right bottom”);
getch();
}

These codes will produce an output “right bottom” and it is place at the lower right of your c++ window. What if the code become like this :

clrscr();
 gotoxy(80,50);
 printf(“right bottom”);
printf(“another printf”);
getch();

In code above another printf is inserted below, thus the first printf is place at the lower right of the screen, the second printf will join the first printf but not place in the lower right but in lower left. The rule is like typing a paragraph in msword when you reach the right margin the next characters you type will be on the lower left of the previous characters you type. Lastly, what if you like to split the position of two printf? It is just simple put gotoxy function before the printf that you would want to declare the position. Example:

clrscr();
 gotoxy(80,50);
 printf("right bottom");
 gotoxy(0,25);
 printf("another printf");
getch();

C++ programming is easy and nice foundation, remember live and love, thank you for reading my simple tutorial about x axis and y axis or gotoxy in c++, I hope you’ll learn something. Thank you and have a nice day!

Saturday, October 19, 2013

Borrowing of book in c

Borrowing of books in c++ or c

The source code below is a simple library system (borrowing of books) inc or c++(cpp), where in the staff of the library input the student name, student number, book code and the days that the student lent the books.

    The method that used:
  • Function – for the displaying of label of the selection of book code.
  • Looping(for loop) – for the retrieval and storing the array value.
  • If else – for transferring the value of book code into the actual name of the book in library.
  • Array – serve as the holder of the input(book code and number of days to be borrow) of user.
  • Goto – I use it to delimit the error input of user.
#include<stdio.h>
#include<conio.h>
char selection()  ////This were I use the function wherein I display a top screen label for the user///////
{
 printf("BOOK'S\n 1) ENGLISH  2) MATH  3) FIL1  4) EPP  5) PE  6) SOCIAL  7) BOB'ong\n--------------------------------------------\n");
 return 0;
}
main() ////This is the main program where the processing of book in the library happens//////
{
char sno[10],name[25],dateb[20],actual_name[100];
intnbook,x,bcode[100],bday[100],y;
int limit=7; 
 clrscr();
  selection();
  printf("STUDENT NUMBER : ");
  scanf("%s",&sno);
  printf("\nSTUDENT NAME : ");
  scanf("%s",&name);
  printf("\nDATE BARROWED : ");
  scanf("%s",&dateb);
 clrscr();
  selection();
a:
  printf("HOW MANY BOOOK'S : ");
  scanf("%d",&nbook);
  if(nbook>limit){  //////// if the user input was higher than the number of book in the library the user will re-input the number of books that he/she want to borrow.
   printf("INVALID INPUT! the maximum is %d\n",limit);
   goto a;
  }
 clrscr();
  selection();
  for(x=1; x<=nbook; x++)
  {
   printf("%d) ENTER BOOK CODE : ",x);
   scanf("%d",&bcode[x]);

   printf("\t\t\tNUMBER OF DAY(S) : ");
   scanf("%d",&bday[x]);
  }
 clrscr();
  selection();

  printf("STUDENT NUMBER\t:\t%s",sno);
  printf("\nSTUDENT NAME\t:\t%s",name);

  printf("\n\nCOUNT\tBCODE\t\tDAYS\n");
  for(y=1; y<=nbook; y++)
  {
  printf("%d\t",y); ///////I use if else because the Lvalue required error was always popping, and I use if else to transfer the book code into the actual name of the book./////
   if(bcode[y]==1)
   {
printf("ENGLISH");
   }else if(bcode[y]==2){
    printf("MATH");
   }else if(bcode[y]==3){
    printf("FIL1");
   }else if(bcode[y]==4){
    printf("EPP");
   }else if(bcode[y]==5){
    printf("PE");
   }else if(bcode[y]==6){
    printf("SOCIAL");
   }else if(bcode[y]==7){
    printf("BOB'ong");
   }

   else{
    printf("INVALID");
   }
  printf("\t\t%d\n",bday[y]);
  }
getch();
}

Thank you for reading my simple library system (borrowing of books) in c or c++(cpp), I hope you learn some new thing in cpp or c.

Monday, September 9, 2013

Download and Install borland turbo cpp(c++)

how to install and download Borland turbo CPP(C++)?

1.) First step download the Borland turbo c by clicking this link borlandc.org.

2.) Make a folder named TC to your desired directory that you want to install the turbo c++(cpp).

3.) Extract the TurboC.zip

4.) Look and double click the file install.exe.

5.) And a dos base window will appear. Then press the enter key.

6.) After pressing the enter key. It will ask. Enter the source drive to use? or in other words the turbo c ask you what drive or directory you wish to install the turbo c. I choose the drive C:\ and press enter.

7.) After that it will ask the source files of turbo c++, locate and type the path where you save the extracted download from the step 1. I only type \turboc, because I paste it to C: directory. Then press enter again.

8.) Then after that the turbo C++ installer point the installation of program to the folder that you made named TC, prior to the step number 2. Press the down arrow key 2 times, then the Start Installation will be highlighted. Then press enter.

10.) Lastly the read me will prompt, just hit or press esc key to exit

That is the simple instruction with picture on where you can download and how to install turbo cpp(c++) in your computer.

DIFFERENT SYNTAX OF FOR LOOP IN CPP(C++)

There are different syntax of for loop in cpp(c++), but in tradition the syntax of for loop in c++ or c, looks like this:

For(initialization; condition; increment)
 Statement;

DIFFERENT SYNTAX OF FOR LOOP IN CPP(C++)

1. FOR LOOP w/o initialization - But do you think it is possible to remove the initialization; in for loop? If you say yes, your answer is correct, just remember declare the variable type and the initial value of that variable. Example I use x variable for the for loop:

Int x=0;
for(; x<5; x++)
 printf(“COUNT[%d]”,x);

*as you notice for(; x<5; x++) I have terminator(;) before the x<5, because if you remove that will get the following error:

  1. Code has no effect
  2. For statement missing
  3. Function should return a value

2. FOR LOOP w/o increment - Another different syntax of for loop is removing third statement which is the increment or decrement. But in-order to execute the for loop error free, you must include the increment or the decrement inside the for loop. Example:

Int x;
for(x=0; x<5){
 printf(“COUNT[%d]”,x);
 x++; 
}

3. FOR LOOP w/o initialization and increment - Then lastly, is the removing of both initialization and the increment or in other word the combination of the example one and two. Example:

Int x=0;
for(; x<=0;){
 printf(“COUNT[%d]”,x);
 x++; 
}

Remember there’s nothing wrong trying new thing in programming, it is much wrong if you don’t take a risk and try new things. I hope you enjoy reading my different syntax of for loop in cpp.

Friday, August 16, 2013

Nested loop in c++

What is nested loop in c++

There are 3 types of nested looping in c++ and these are, nested for loop, nested while and lastly the nested do.. while statement. Nested looping in c++ simply means, loop inside a loop or in other words loop statement within a loop statement. The main and subfolder structure is similar in the nested looping. See the figure bellow.

  • ABOUTMAIN FOLDER
    • C++SUB FOLDER
      • CPPSUB FOLDER
    • MakeSUB FOLDER
    • LearnSUB FOLDER
    • Turbo CSUB FOLDER

The color blue box represents as the main folder and it contains several folders inside, and as you noticed the c++ subfolder contain a subfolder inside, and this kind of structure is the best representation of a nested looping statement in cpp.

The nested looping can handle almost 256 level of nesting statement, how to count the level in nested looping? In the figure above the level one is the blue box entitled About, level 2 is the green boxes and the level 3 is the color red box entitled CPP. The second figure show, how the curly brace identified the looping within the main loop.

  • WHILE(CONDITION)MAIN or PARENT Loop
    • {OPEN Curly brace
      • WHILE(CONDITION)SUB or Child Loop
        • {OPEN Curly brace
          • CODE TO EXECUTESUB or Child Loop code to execute
        • }CLOSE Curly brace
      • CODE TO EXECUTEMAIN or PARENT Loop code to execute
    • }CLOSE Curly brace

Try to hover your mouse on the top of main while(condition), as you notice the color of paired curly braces become grey, it means that all codes or statement enclose by the paired of grey curly braces is instantly executing, it will not stop its execution as long the condition in the main while loop is true. The main while loop contain while loop inside and that is the reason why it become nested, simply loop within a loop. Now try to hover your mouse in the sub while(condition) as you notice the paired curly braces become grey, and codes that will executed is only the codes enclose by the grey curly braces, the code outside of the grey curly braces will not execute or triggered.

The different types of nested looping.

The nested for loop statement in c++ syntax:

for (initialization; condition; increment or decrement)
{
   statement/s; ###YOU CAN PUT MORE STATEMENTS###
   for (initialization; condition; increment or decrement)
   {
      statement/s; ###YOU CAN PUT MORE STATEMENTS###
   }
}

The nested while loop statement in c++ syntax:

while(condition)
{
   statement/s; ###YOU CAN PUT MORE STATEMENTS###
   while(condition)
   {
      statement/s; ###YOU CAN PUT MORE STATEMENTS###
   }
}

The nested do while loop statement in c++ syntax:

do
{
   statement/s; ###YOU CAN PUT MORE STATEMENTS###
   do
   {
      statement/s; ###YOU CAN PUT MORE STATEMENTS###
   }while(condition);

}while(condition);
for (initialization; condition; increment or decrement){
   do
   {
   while(condition)
   {
      statement/s; ###YOU CAN PUT MORE STATEMENTS###
   }
   }while(condition);
}

I hope you learn some something in c++ nested looping statement, and I forgot to mention you can mix those three(3) types of nested loop (nested for loop, nested while loop, nested do while loop) to generate a mix nested loop (code above).

Sunday, August 11, 2013

Calculator in c, c++, cpp or turbo c

How to make an calculator in C++

The source code bellow is the simple sample calculator in cpp, c++ or c. This will serve as tutorial for the beginners who want to learn c language. This simple calculator that I make, allows the user to inputs 2(two) numbers and choose an operation to be used.

Instead of using symbols such as +(add), -(minus), *(multiply) and /(divide), I use letter to represent a single operation.

    This are the following letter that I used:
  • A = Addition
  • S = Subtraction
  • M = Multiplication
  • D = Division

If the user input another letter that does not include to the choices, 'invalid operation' will show.

Basic knowledge

If else statement, declaring variables, data types, printf, scanf,

    Advantage
  • It can compute number with decimal point.
  • Easy to use and understand.
  • It shows the complete answer together with the user’s input.
    Disadvantage
  • Only 2 numbers are allow to input.
  • It can’t perform multiple operations at a time.
  • Decimal point always shows (because I use float data type)

*My Compiler is turbo c.

The calculator source code in c++,c or cpp

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define p printf
#define s scanf
 main()
 {
 float fnum,snum,ans;
 char operation, ope;
 clrscr();
 p("Simple Calculator \n A=adddition, S=subtraction, M=multiplication, D=division\n");
 p("First number:");
 s("%f",&fnum);
 p("Operation:");
 operation=getche();
 operation=toupper(operation);
 p("\nSecond Number:");
 s("%f",&snum);
  if(operation=='A'){
  ope='+';
  ans=fnum+snum;}
  else if (operation=='S'){
  ope='-';
  ans=fnum-snum;}
  else if (operation=='M'){
  ope='x';
  ans=fnum*snum;}
  else if (operation=='D'){
  ope='/';
  ans=fnum/snum;}
  else p("Invalid Operation!");
  p("%f %c %f the Answer is %f",fnum, ope, snum, ans);
 getch();
}

I hope you like my tutorial in making an easy and simple calculator in C, C++ or CPP. So for the beginners (like me) out there, what are you waiting for try it now!

Sunday, August 4, 2013

Jack N Poy in C++,C,Turbo C

How to make a simple jak n poy program in C++.

First of all you must have the basic knowledge in
  • If else statement.
  • Must be familiar in logical operators.
  • Deriving the variables value.
In code bellow is example of jak n poy program in c++, the process of this code: there are 2 players (player 1 and player 2). The turn starts at player 1 then player 2. The allowed inputs of both players are:
  • P – Stands for paper
  • R – meaning Rock
  • S - for scissors
Either P,R or S are the allowed inputs, if the user tries to input other character the code will terminated or end. Instead of scanf I use getche for the process of input.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define p printf
main()
{
 char p1,P1,p2,P2;
 p("Player1");
 p1=getche();
 P1= toupper(p1);
 if(P1=='P' ||P1=='S' ||P1=='R'){
  p("\n Player2");
  p2=getche();
  P2= toupper(p2);
  if(P2=='P' ||P2=='S' ||P2=='R'){
   if(P1==P2)
   p("\N Invalid input player 2");
   else if(P1=='P'&&P2=='R')
    p("\n Player1 wins!\n");
   else if(P1=='S'&&P2=='P')
    p("\n Player1 wins");
   else if(P1=='R'&&P2=='S')
    p("\n Player1 wins");
   else if(P1=='P'&&P2=='S')
    p("\n Player2 wins");
   else if(P1=='S'&&P2=='R')
    p("\n Player2 wins");
   else if(P1=='R'&&P2=='P')
    p("\n Player2 wins");
  }
  else{
  p("\n invalid");
  }
 }
 else{
  p("\N Invalid input player 1");
 }
 getch();
}
My Source code formatter : codeformatting.blogspot.com



The code toupper serve as character converter, it convert the lower case character to uppercase. So that we dont need to make any condition for the difference text format of the user. That is the simple source code of c++ jak and poy.Thank you.