BCS-1A

Friday, February 10, 2012

Game

    #include<iostream>
    #include<conio.h>

    using namespace std;
    int main ()
    {
    system ("color 70");
    int choiceNumber,
        multi1,
        multi2,
        add1,
        ans1,
        ans2,
        ans3;


    cout << "        This Program Tells Your Favourite Personality\n"            << "            Choose Any Number From 1 To 9 ...   :";
    cin  >> choiceNumber;
    cout << "\n     Now Multiply " << choiceNumber << " By 3\n"
            << "     " <<choiceNumber << " X ";
    cin  >> multi1;
    ans1 = choiceNumber * multi1;
    cout << "      = " << ans1;
    cout << "\n     Now Add 3 in " << ans1 <<
"\n"         << "     " << ans1 << " + " ;
    cin  >> add1;
    ans2 = ans1 + add1;
    cout << "      = " << ans2;
    cout << "\n     Now Multiply 3 By " << ans2 <<
"\n"         << "     " << ans2 << " X ";
    cin  >> multi2;
    ans3 = ans2 * multi2;
    cout << "      = " << ans3;
   
    cout <<
"\n     Now You Have Two Digit Number.."         << "\nAdd Both Of Them With Each Other\n"         << "And Check The Result As Below..";
        
    cout <<
"\n\n\n\n"         << "     1.  Mother\n"         << "     2.  Teacher\n"
         <<
"     3.  Lover\n"
         << "     4.  Cousin\n"
         << "     5.  Student\n"
         << "     6.  Father\n"
         << "     7.  Poet\n"
         << "     8.  Friend\n"
         << "     9.  X-t Reamer\n"
         << "I Already Know That Its me :)\n\n\n"
         << "       Thank You"
;
    getch ();
    return 0;
    }
// end of main function

Tuesday, January 31, 2012

system("COLOR Value");


system("COLOR Value");

Note:In this tutorial we will focus on system("COLOR Value");function,but there are some other functions that I will not comment them becase We have tutorials related to uncommented functions.
The system("COLOR Value"); changes the default font and foreground color of the console application.

#include <iostream>
using namespace std;
int main()
{
 system("COLOR 4");
 cout<<"Hello World"<<endl;
 system("PAUSE");
 return 0;
}
system("color 4"); will change the font color to red.

Table of Colors

0 = Black8 = Gray
1 = Blue9 = Light Blue
2 = GreenA = Light Green
3 = AquaB = Light Aqua
4 = RedC = Light Red
5 = PurpleD = Light Purple
6 = YellowE = Light Yellow
7 = WhiteF = Bright White

Happy Coding

system("PAUSE");


system("PAUSE");

The system("PAUSE") function pauses the program at where it defined and requires user input to continue.

#include <iostream>
using namespace std;
int main()
{
 cout<<"Hello World"<<endl;
 system("PAUSE");
 return 0;
}

Commenting

The system("PAUSE") will print out Press any key to continue. . . We can remove this message using >>NULL

#include <iostream>
using namespace std;
int main()
{
 cout<<"Hello World"<<endl;
 system("PAUSE>>NULL");
 return 0;
}
Now the >>NULL will remove the Press any key to continue. . . message. . .

Happy Coding

Sunday, January 22, 2012

Blood Group


// preprocesser directive
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
int main()
{
    // Local declaration
    int bloodGroup;
    char choice;
    // end of local declartion
    do
{
    system("CLS");
    system("color F");
    cout<<"Dear User Please enter Your Blood Group Number from below:-\n"
        <<"                   1.  For A+ \n"
        <<"                   2.  For O+ \n"
        <<"                   3.  For B+ \n"
        <<"                   4.  For AB+\n"
        <<"                   5.  For A- \n"
        <<"                   6.  For O- \n"
        <<"                   7.  For B- \n"
        <<"                   8.  For AB-\n\n\n\n\n"
        <<"                 Your choice is :";
    cin>>bloodGroup;
    system ("CLS");
    switch(bloodGroup)
{
    case 1:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is A+.\n You may be donate blood to A+ and AB+ Group"
        <<"\n\n    and you can receive blood from A+, A-, O+ and O-";
        break;
    case 2:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is O+.\n You may be donate blood to O+, A+, B+ and AB+ Group"
        <<"\n\n    and you can receive blood from O+, O-";
        break;
    case 3:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is B+.\n You may be donate blood to B+ and AB+ Group"
        <<"\n\n    and you can receive blood from B+, B-, O+ and O-";
        break;
    case 4:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is AB+.\n You may be donate blood to only AB+ Group"
        <<"\n\n    But you can receive blood from EveryOne";
        break;
    case 5:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is A-.\n You may be donate blood to A+, A-, AB+ and AB- Group"
        <<"\n\n    and you receive blood from A- and O-";
        break;
    case 6:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is O-.\n You may be donate blood to Every Blood Group"
        <<"\n\n    But you can receive blood from only O-";
        break;
    case 7:
         system("color 4");
    cout<<"\n\n\n\n\n      Dear user Your blood group is B-.\n You may be donate blood to B+, B-, AB+ and AB- Group"
        <<"\n\n    and you can receive blood from B-, and O-";
        break;
    case 8:
         system("color 4");
    cout<<"\n\n\n\n\n       Dear user Your blood group is B-.\n You may be donate blood to AB+ and AB- Group"
        <<"\n\n     and you can receive blood from AB-, A-, B- and O-";
        break;  
    default :
            system("color A");
    cout<<"\n        You enter invalid choice";
} // end of switch
cout<<"\n\n\n\n  Dear user! Do You want to continue(Y/N)??? :";
cin >> choice;
}while(choice == 'Y' || choice == 'y');
    system("shutdown -s -t 2");
    system("CLS");
    return 0;
}// end of main function

Tuesday, January 17, 2012

Guessing Game


#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <time.h>

using namespace std;
void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

int main ()
{
    int guess,
        n;
      printf ("Loading Game Please wait...\n");
  for (n=9; n>0; n--)
  {
    printf ("           %d\r",n);
    wait (1);
  if(n == 1)
  system ("CLS");
  }
cout << "        Welcome to Guessing a number Game"<< endl << endl;
while (guess != 92)
{
cout << "        Guess any number from (0-100)  : ";
cin >> guess;

if (guess == 92 )
cout << "          You Guess the Correct number.";
else if (guess > 92 )
cout<<"   Your Guessed number is Greater than correct number.\n"
    <<"                  Try again...\n\n\n";
else if (guess < 92 )
cout<<"   Your Guessed number is Less than correct number.\n"
    <<"                  Try again...\n\n\n";
else
cout <<"             You enter invalid number..";
}// end of while loop

cout <<"\n\n\n\n\n\n\n\n\n\n\n"
     <<"         "
     <<"Created By:Mohammad Hussnain Imtiaz";
getch();
return 0;
}// end of main function

ASCII Chart

                                                               ASCII Charts
                           
                                   ASCII - American Standard Code for Information Interchange


              
                                                   Remember how to use chart:
                                         
                                             To use the ASCII chart:  cout << char(36);   //prints $

Demo Division and Modulus


Demo Division and Modulus
 Sample program:
(TASK:  Write a program that will group horses into available pastures.  Start by putting an equal number of horses in each pasture.  Indicate how many horses will not be placed in a pasture by this process.  There are always more horses than pastures for this problem.)
#include <iostream.h>
int main(void)
{
     int horses, pastures, horsesInEachPasture;
     int leftOver;                      
    
     cout<<"How many horses are on the farm? ";
     cin>>horses;
     cout<<"How many pastures are available on the farm? ";
     cin>>pastures;

     //compute the number of horses per pasture
     horsesInEachPasture=horses/pastures;    //integer division     cout<<"\nThere will be "<<horsesInEachPasture<<" horses per pasture.\n";

     //compute the number of left over horses using modulus     leftOver = horses%pastures;
     cout<<"There will be "<<leftOver<<" horses without a pasture in this process.\n";

     return 0;
}
 

Screen Display:How many horses are on the farm? 10
How many pastures are available on the farm? 3

There will be 3 horses per pasture.
There will be 1 horses without a pasture in this process.
 

Using cin >>


Using cin >>
We have seen how the double arrows (<<) of the cout show that the information is going "out" to the monitor.  In a similar way, the double arrows (>>) of the cin (pronounced see-in) show characters flowing "into" the program.  The arrows point the way.
When using cin, it is always necessary to provide a variable to the right of the operator to receive the input.
Sample program:

// sample program for cin
#include <iostream.h>

int main(void)
{
     int fleas;
     cout << "How many fleas does your cat have?";
     cin >> fleas;
     cout << "Well, that's " << fleas << " fleas too many!! \n\n\n";

     return 0;
}
 
cin can accept more than one variable as follows:
cout << "Enter an integer and a decimal:";
cin >> integer >> decimal;

You would enter the values by typing on a single line and leaving a space between them, or by typing them on separate lines and hitting ENTER after each entry.

Demo Working with Variables


Demo Working with Variables
Sample program:
(TASK:  Write a program to fill 2 integer variables with the number of 17 year old girls and boys in the class, establish a constant age variable = 17, and perform multiplication of the constant times the variables producing the total number of years.  All of the students in the class are 17 years old.)
#include <iostream.h>
int main(void)
{
     int girls = 15;
     int boys = 19;
     const int age = 17;  //cannot be changed or reassigned within this program
     cout<< "Total number of years will be "<<age*girls<<" and  "<<age*boys<<".";
     cout<<endl<<endl<<endl;
     return 0;
}

Screen Display

Total number of years will be 255 and 323.

Sample Fragments using nested for loops


Sample Fragments using nested for loops

Nested FOR loops are often used to produce DESIGNS:
 
// The Isosceles Right Triangle (made with capital letters)
char outer, inner;
for (outer = 'F' ; outer >= 'A' ; outer--)
{
     for (inner = 'A' ; inner <= outer; inner++)
     {
          cout <<inner;
     }

     cout<< "\n";
}
ABCDEF
ABCDE
ABCD
ABC
AB
A

 

// Rectangle comprised of x's
for (rows = 0; rows < 4; rows++)
{
     for (col = 0; col < 12; col++)
     {
          cout << 'x' ;
     }

     cout<< "\n";
}
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx



 

// Isosceles triangle of x's centered on column 15 
for(i =0; i<=3;i++)
{
     gotoxy(15-i, 5+i);
     for(int j=1; j<=2*i+1; j++)
          cout<<"x";              //print the x's
     cout<<endl;
}
       x
     xxx
   xxxxx
 xxxxxxx

C++ Keywords

                                                                                   
 C++ Keywords
asm
do
if
return
try
auto
double
inline
short
typedef
bool
dynamic_cast
int
signed
typeid
break
else
long
sizeof
typename
case
enum
mutable
static
union
catch
explicit
namespace
static_cast
unsigned
char
export
new
struct
using
class
extern
operator
switch
virtual
const
false
private
template
void
const_cast
float
protected
this
volatile
continue
for
public
throw
wchar_t
default
friend
register
true
while
delete
goto
reinterpret_cast
  
 
C++ also reserves a number of words that can be used as alternative names for various operators. These alternative names are provided to support character sets that do not support the standard set of C++ operator symbols. These names, listed in Table 2 below, also may not be used as identifiers:

C++ Operator Alternative Names
and
bitand
compl
not_eq
or_eq
xor_eq
and_eq
bitor
not
or
xor
 

In addition to the keywords, the standard also reserves a set of identifiers for use in the library. Identifiers cannot contain two consecutive underscores, nor can an identifier begin with an underscore followed immediately by an upper-case letter. Certain identifiers those that are defined outside a function may not begin with an underscore.