Showing posts with label C. Show all posts
An assingment of nust. ATM machine software using file handling in C++
1. Our Modern ATM machine has the following Menu
a. Create new account
i. When user selects this option, ask for username and password. Create a new text file with his/her name. If user already exists then ask the user to enter another name.
b. Cash Withdraw
i. Ask the user the amount he/she wants to withdraw. Check his account if the user has that amount of money in his/her account. If the amount exceeds, a message should be display saying that please enter less amount.
c. Cash Deposit
d. Fill Cash in ATM
i. Ask for amount and add in ATM machine.
f. The menu should be displayed again after every action. E.g., After cash withdrawal ATM software should display the menu again instead of exiting.
a. Create new account
i. When user selects this option, ask for username and password. Create a new text file with his/her name. If user already exists then ask the user to enter another name.
b. Cash Withdraw
i. Ask the user the amount he/she wants to withdraw. Check his account if the user has that amount of money in his/her account. If the amount exceeds, a message should be display saying that please enter less amount.
c. Cash Deposit
d. Fill Cash in ATM
i. Ask for amount and add in ATM machine.
f. The menu should be displayed again after every action. E.g., After cash withdrawal ATM software should display the menu again instead of exiting.
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
void main(){
char a;
FILE *myFile;
char name[10];
char password[10];
char temp;
int id_no = 0;
int user_id = 0;
int all_bal[1000];
int acc_bal = 0;
int bal = 0;
int counter = 0;
bool vl = 0;
int char_matched = 0;
int atm_bal = 0;
while (true){
cout << "Select Option: "
<< endl << "1. Create New Account"
<< endl << "2. Withdraw Cash"
<< endl << "3. Cash Deposit"
<< endl << "4. Cash fill in atm"
<< endl << "5. Account Summary" << endl;
a = _getch();
system("cls");
switch (a){
case '1':
cout << "Enter Your Name: ";
cin >> name;
cout << "Enter Your Password: ";
do{
temp = _getch();
cout << "*";
password[counter] = temp;
counter++;
} while (temp != 13 && counter < 10);
counter--;
password[counter] = '\0';
counter = 0;
cout << endl;
cout << "Enter Opening balance:";
cin >> acc_bal;
if (acc_bal < 0){
acc_bal = 0;
}
vl = 0;
myFile = fopen("all_names.txt", "r");
if (myFile){
while (fscanf(myFile, "%c", &temp) == 1){
if (temp != '-'){
if (name[counter] == temp){
char_matched++;
counter++;
}else {
char_matched = 0;
counter = 0;
}
}
if (temp == '-'){
if (char_matched == strlen(name)){
vl = 1;
}
counter = 0;
char_matched = 0;
}
}
fclose(myFile);
counter = 0;
char_matched = 0;
}
if (vl == 0){
myFile = fopen("all_names.txt", "a");
fprintf(myFile, "%s", name);
fprintf(myFile, "%c", '-');
fclose(myFile);
myFile = fopen("all_passwords.txt", "a");
fprintf(myFile, "%s", password);
fprintf(myFile, "%c", '-');
fclose(myFile);
myFile = fopen("all_accounts.txt", "a");
fprintf(myFile, "%d", acc_bal);
fprintf(myFile, "%c", '\n');
fclose(myFile);
cout << "Account created succesfully" << endl;
}
else {
cout << "Name Already In Use............." << endl;
}
break;
case '2':
cout << "Enter Your Name: ";
cin >> name;
vl = 0;
counter = 0;
char_matched = 0;
myFile = fopen("all_names.txt", "r");
if (myFile){
while (fscanf(myFile, "%c", &temp) == 1){
if (temp != '-'){
if (name[counter] == temp){
char_matched++;
counter++;
}
else {
char_matched = 0;
counter = 0;
}
}
if (temp == '-'){
if (char_matched == strlen(name)){
vl = 1;
user_id = id_no;
}
id_no++;
counter = 0;
char_matched = 0;
}
}
fclose(myFile);
counter = 0;
char_matched = 0;
}
else {
cout << "account does not exists..." << endl;
}
if (vl == 0){
cout << "account does not exists..." << endl;
}
else {
cout << "Please enter your password" << endl;
do{
temp = _getch();
cout << "*";
password[counter] = temp;
counter++;
} while (temp != 13 && counter < 10);
counter--;
password[counter] = '\0';
counter = 0;
cout << endl;
vl = 0;
counter = 0;
char_matched = 0;
id_no = 0;
myFile = fopen("all_passwords.txt", "r");
if (myFile){
while (fscanf(myFile, "%c", &temp) == 1){
if (temp != '-' && id_no == user_id){
if (password[counter] == temp){
char_matched++;
counter++;
}
else {
char_matched = 0;
counter = 0;
}
}
if (temp == '-'){
if (char_matched == strlen(password)){
vl = 1;
}
id_no++;
counter = 0;
char_matched = 0;
}
}
fclose(myFile);
counter = 0;
char_matched = 0;
}
if (vl == 1){
cout << "ACESS GURANTED***" << endl
<< "Enter Amount to withdraw: ";
cin >> acc_bal;
vl = 0;
counter = 0;
myFile = fopen("all_accounts.txt", "r");
if (myFile){
while (counter <= user_id){
counter++;
fscanf(myFile, "%d", &bal);
}
myFile = fopen("atm_balance.txt", "r");
if (myFile){
fscanf(myFile, "%d", &atm_bal);
} else {
myFile = fopen("atm_balance.txt", "w");
fprintf(myFile, "%d", atm_bal);
}
fclose(myFile);
if(bal < acc_bal){
cout << "You Have only: " << bal << "$" << endl;
}
if(acc_bal > atm_bal && bal >= acc_bal) {
cout << "Machine Have only: " << atm_bal << "$" << endl;
}else if(acc_bal <= atm_bal && bal >= acc_bal){
myFile = fopen("all_accounts.txt", "r");
counter = 0;
while (fscanf(myFile, "%d", &all_bal[counter]) == 1){
counter++;
}
fclose(myFile);
all_bal[user_id] -= acc_bal;
myFile = fopen("all_accounts.txt", "w");
id_no = 0;
while (counter != 0){
fprintf(myFile, "%d", all_bal[id_no]);
fprintf(myFile, "%s", "\n");
counter--;
id_no++;
}
fclose(myFile);
atm_bal -= acc_bal;
myFile = fopen("atm_balance.txt", "w");
fprintf(myFile, "%d", atm_bal);
fclose(myFile);
cout << "Take your money. THankx.." << endl;
}
counter = 0;
char_matched = 0;
}
} else{
cout << "Acess Denied.." << endl;
}
}
id_no = 0;
user_id = 0;
break;
case '3':
cout << "Enter Your Name: ";
cin >> name;
vl = 0;
counter = 0;
char_matched = 0;
myFile = fopen("all_names.txt", "r");
if (myFile){
while (fscanf(myFile, "%c", &temp) == 1){
if (temp != '-'){
if (name[counter] == temp){
char_matched++;
counter++;
}
else {
char_matched = 0;
counter = 0;
}
}
if (temp == '-'){
if (char_matched == strlen(name)){
vl = 1;
user_id = id_no;
}
id_no++;
counter = 0;
char_matched = 0;
}
}
fclose(myFile);
counter = 0;
char_matched = 0;
}
else {
cout << "account does not exists..." << endl;
}
if (vl == 0){
cout << "account does not exists..." << endl;
}
else {
cout << "Please enter your password" << endl;
do{
temp = _getch();
cout << "*";
password[counter] = temp;
counter++;
} while (temp != 13 && counter < 10);
counter--;
password[counter] = '\0';
counter = 0;
cout << endl;
vl = 0;
counter = 0;
char_matched = 0;
id_no = 0;
myFile = fopen("all_passwords.txt", "r");
if (myFile){
while (fscanf(myFile, "%c", &temp) == 1){
if (temp != '-' && id_no == user_id){
if (password[counter] == temp){
char_matched++;
counter++;
}
else {
char_matched = 0;
counter = 0;
}
}
if (temp == '-'){
if (char_matched == strlen(password)){
vl = 1;
}
id_no++;
counter = 0;
char_matched = 0;
}
}
fclose(myFile);
counter = 0;
char_matched = 0;
}
if (vl == 1){
cout << "ACESS GURANTED***" << endl
<< "Enter Amount to deposit: ";
cin >> acc_bal;
vl = 0;
counter = 0;
myFile = fopen("all_accounts.txt", "r");
if (myFile){
while (counter <= user_id){
counter++;
fscanf(myFile, "%d", &bal);
}
myFile = fopen("atm_balance.txt", "r");
if (myFile){
fscanf(myFile, "%d", &atm_bal);
} else {
myFile = fopen("atm_balance.txt", "w");
fprintf(myFile, "%d", atm_bal);
}
fclose(myFile);
if(acc_bal <0){
myFile = fopen("all_accounts.txt", "r");
counter = 0;
while (fscanf(myFile, "%d", &all_bal[counter]) == 1){
counter++;
}
fclose(myFile);
all_bal[user_id] += acc_bal;
myFile = fopen("all_accounts.txt", "w");
id_no = 0;
while (counter != 0){
fprintf(myFile, "%d", all_bal[id_no]);
fprintf(myFile, "%s", "\n");
counter--;
id_no++;
}
fclose(myFile);
cout << "CASH DEPOSITED. THankx.." << endl;
}
fclose(myFile);
counter = 0;
char_matched = 0;
}
} else{
cout << "Acess Denied.." << endl;
}
}
id_no = 0;
user_id = 0;
break;
case '4':
cout << "Enter Amount to add: ";
cin >> bal;
myFile = fopen("atm_balance.txt", "r");
if (myFile){
fscanf(myFile, "%d", &atm_bal);
} else {
myFile = fopen("atm_balance.txt", "w");
fprintf(myFile, "%d", atm_bal);
}
fclose(myFile);
atm_bal += bal;
myFile = fopen("atm_balance.txt", "w");
fprintf(myFile, "%d", atm_bal);
fclose(myFile);
break;
default:
break;
}
cout << "Press any key to continue....";
_getch();
system("cls");
}
}
Thursday, 29 January 2015
Posted by Unknown
Program to tell whether entered number is +ive,-ive or Zero
//Program to tell whether entered number is +ive,-ive or Zero
#include<stdio.h>
#include<conio.h>
//main function
void main()
{
int x;
printf("Enter x:");
scanf("%d",&x);
if(x==0)
{
printf("The number is Zero");
}
else if(x>0)
{
printf("The number is Positive");
}
else if(x<0)
{
printf("The number is Negative");
}
getch();
}
#include<stdio.h>
#include<conio.h>
//main function
void main()
{
int x;
printf("Enter x:");
scanf("%d",&x);
if(x==0)
{
printf("The number is Zero");
}
else if(x>0)
{
printf("The number is Positive");
}
else if(x<0)
{
printf("The number is Negative");
}
getch();
}
Saturday, 17 January 2015
Posted by Unknown
Program/code to swap two numbers in C language
This code demonstrate the swaping of two numbers.
//program to swap two numbers input by user
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,a;
printf("Enter x:");
scanf("%d",&x);
printf("Enter y:");
scanf("%d",&y);
a=x;
x=y;
y=a;
printf("x is %d\n",x);
printf("y is %d",y);
getch();
}
//program to swap two numbers input by user
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,a;
printf("Enter x:");
scanf("%d",&x);
printf("Enter y:");
scanf("%d",&y);
a=x;
x=y;
y=a;
printf("x is %d\n",x);
printf("y is %d",y);
getch();
}
Posted by Unknown
Filing a name and searching it again to match from file in C.
This code demonstrate the filing part of C language. This program is built to save name of users in a file on hard disk you can also retrieve that file from hard disk using this program and could also search how many times a name is found in a file.
The program also uses some basic function of C++ that are just for convenience you can replace them by C language
printf("some text"); is used as cout << "some text";
scanf(&d) id used as cin >> d;
so dont worry if you need code in C only you could replace it
The special data type FILE is used used to create stream for input or output data on hard disk.
in fopen function a mean file is opened in append mode, r mean read mode, w mean write mode.
system("cls"); is iostream part so if you want avoid C++ you could simply delete it that will not effect the code. this function just delete all data from console and clear it.
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
void main() {
FILE* myFile;
FILE* readFile;
char name[20];
int con = 1;
char temp;
while (true){
myFile = fopen("names.txt", "a");
cout << "Press 1 to Enter name..2 to search name.. 3 to display file: ";
cin >> con;
system("cls");
//enter name
if (con == 1){
cout << "Enter A name: ";
cin >> name;
fprintf(myFile, "%s", name);
fprintf(myFile, "%c", '-');
cout << "\nNAME ENTERED\n";
}
fclose(myFile);
//search in file
int counter = 0;
readFile = fopen("names.txt", "r");
if (con == 2){
cout << "Enter a name to search:";
cin >> name;
int matched = 0;
int chars = strlen(name);
while (fscanf(readFile, "%c", &temp) == 1){
if(temp!= '-'){
if(name[matched] == temp){
matched++;
} else{
matched = 0;
}
} else if(matched == chars){
counter++;
matched = 0;
} else{
matched = 0;
}
}
}
if(counter > 0){
cout << "\n\aName is found in file " << counter << " times.\n";
} else if(con == 2) {
cout << "Name is not in file...\n";
}
if(con == 3){
//display file
fclose(readFile);
fopen("names.txt", "r");
cout << "Display File text:\n";
while (fscanf(readFile, "%c", &temp)== 1){
if(temp != '-'){
printf("%c", temp);
} else {
printf("\n");
}
}
}
printf("\n");
}
_getch();
}
The program also uses some basic function of C++ that are just for convenience you can replace them by C language
printf("some text"); is used as cout << "some text";
scanf(&d) id used as cin >> d;
so dont worry if you need code in C only you could replace it
The special data type FILE is used used to create stream for input or output data on hard disk.
in fopen function a mean file is opened in append mode, r mean read mode, w mean write mode.
system("cls"); is iostream part so if you want avoid C++ you could simply delete it that will not effect the code. this function just delete all data from console and clear it.
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
void main() {
FILE* myFile;
FILE* readFile;
char name[20];
int con = 1;
char temp;
while (true){
myFile = fopen("names.txt", "a");
cout << "Press 1 to Enter name..2 to search name.. 3 to display file: ";
cin >> con;
system("cls");
//enter name
if (con == 1){
cout << "Enter A name: ";
cin >> name;
fprintf(myFile, "%s", name);
fprintf(myFile, "%c", '-');
cout << "\nNAME ENTERED\n";
}
fclose(myFile);
//search in file
int counter = 0;
readFile = fopen("names.txt", "r");
if (con == 2){
cout << "Enter a name to search:";
cin >> name;
int matched = 0;
int chars = strlen(name);
while (fscanf(readFile, "%c", &temp) == 1){
if(temp!= '-'){
if(name[matched] == temp){
matched++;
} else{
matched = 0;
}
} else if(matched == chars){
counter++;
matched = 0;
} else{
matched = 0;
}
}
}
if(counter > 0){
cout << "\n\aName is found in file " << counter << " times.\n";
} else if(con == 2) {
cout << "Name is not in file...\n";
}
if(con == 3){
//display file
fclose(readFile);
fopen("names.txt", "r");
cout << "Display File text:\n";
while (fscanf(readFile, "%c", &temp)== 1){
if(temp != '-'){
printf("%c", temp);
} else {
printf("\n");
}
}
}
printf("\n");
}
_getch();
}
Tic tac coding/program in C language.(without using array)
This coding demonstrate the usage of only basic data type and basic command(if, for, while functions, etc) to make a practical game that could be run. The code just uses the basic commands. This program also demonstrate the usage of function.
Comments are added to let you understand the program still if you have any further query you can comment below. Thanks hope this will help you.
#include<stdio.h>
#include<conio.h>
//Player turns
int turn(int &user, int &t, int &a1, int &a2, int &a3, int &a4, int &a5, int &a6, int &a7, int &a8, int &a9) {
int pos;
if(user%2 == 1){printf("Player1\n");}
if(user%2 == 0){printf("Player2\n");}
printf("Enter the postion You Want to selct: ");
scanf_s("%d", &pos);
if(pos == 1 && a1 == 0){//chk avalability of player selected box
if(user%2 == 1){//if player1 chosed box
a1 = 1; //box = 1 for p1 chek the box
user++; //chng user for next turn
t++; //turn count
} else if (user%2 == 0) {//if player 2 chosed box
a1 = 2; //box = 2 for p2 checked the box
user++;
t++;
}
} else if (pos == 1 && a1 != 0){//if box is already filled
printf("Already checked!\n");
}
//same structure for next 9 boxes
if(pos == 2 && a2 == 0){
if(user%2 == 1){
a2 = 1;
user++;
t++;
} else if (user%2 == 0) {
a2 = 2;
user++;
t++;
}
} else if (pos == 2 && a2 != 0){
printf("Already checked!\n");
}
if(pos == 3 && a3 == 0){
if(user%2 == 1){
a3 = 1;
user++;
t++;
} else if (user%2 == 0) {
a3 = 2;
user++;
t++;
}
} else if (pos == 3 && a3 != 0){
printf("Already checked!\n");
}
if(pos == 4 && a4 == 0){
if(user%2 == 1){
a4 = 1;
user++;
t++;
} else if (user%2 == 0) {
a4 = 2;
user++;
t++;
}
} else if (pos == 4 && a4 != 0){
printf("Already checked!\n");
}
if(pos == 5 && a5 == 0){
if(user%2 == 1){
a5 = 1;
user++;
t++;
} else if (user%2 == 0) {
a5 = 2;
user++;
t++;
}
} else if (pos == 5 && a5 != 0){
printf("Already checked!\n");
}
if(pos == 6 && a6 == 0){
if(user%2 == 1){
a6 = 1;
t++;
user++;
} else if (user%2 == 0) {
a6 = 2;
user++;
t++;
}
} else if (pos == 6 && a6 != 0){
printf("Already checked!\n");
}
if(pos == 7 && a7 == 0){
if(user%2 == 1){
a7 = 1;
user++;
t++;
} else if (user%2 == 0) {
a7 = 2;
user++;
t++;
}
} else if (pos == 7 && a7 != 0){
printf("Already checked!\n");
}
if(pos == 8 && a8 == 0){
if(user%2 == 1){
a8 = 1;
user++;
t++;
} else if (user%2 == 0) {
a8 = 2;
user++;
t++;
}
} else if (pos == 8 && a8 != 0){
printf("Already checked!\n");
}
if(pos == 9 && a9 == 0){
if(user%2 == 1){
a9 = 1;
user++;
t++;
} else if (user%2 == 0) {
a9 = 2;
user++;
t++;
}
} else if (pos == 9 && a9 != 0){
printf("Already checked!\n");
}
return 0;
}
//check if any player had made the combination
int win_cheker(int &a1, int &a2, int &a3, int &a4, int &a5, int &a6, int &a7, int &a8, int &a9){
//chk first row for p1 else p2
if(a1 == a2 && a2 == a3 && a3 == 1){
printf("winning combination\n");
printf("\n%d %d %d\n* * *\n* * *\n ", a1, a2, a3);
return 1;
} else if (a1 == a2 && a2 == a3 && a3 == 2) {
printf("winning combination\n");
printf("\n%d %d %d\n* * *\n* * *\n ", a1, a2, a3);
return 2;
}
//chk 2nd row for p1 else p2
if(a4 == a5 && a5 == a6 && a6 == 1){
printf("winning combination\n");
printf("\n* * *\n%d %d %d\n* * *\n ", a4, a5, a6);
return 1;
} else if(a4 == a5 && a5 == a6 && a6 == 2) {
printf("winning combination\n");
printf("\n* * *\n%d %d %d\n* * *\n ", a4, a5, a6);
return 2;
}
//chk 3rd row for p1 else p2
if(a7 == a8 && a8 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n|* * *\n|* * *\n|%d %d %d\n ", a7, a8, a9);
return 1;
} else if(a7 == a8 && a8 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n* * *\n* * *\n%d %d %d\n ", a7, a8, a9);
return 2;
}
//chk 1st column for p1 else p2
if(a1 == a4 && a4 == a7 && a7 == 1){
printf("winning combination\n");
printf("\n%d * *\n%d * *\n%d * *\n ", a1, a4, a7);
return 1;
} else if(a1 == a4 && a4 == a7 && a7 == 2) {
printf("winning combination\n");
printf("\n%d * *\n%d * *\n%d * *\n ", a1, a4, a7);
return 2;
}
//chk 2nd column for p1 else p2
if(a2 == a5 && a5 == a8 && a8 == 1){
printf("winning combination\n");
printf("\n* %d *\n* %d *\n* %d *\n ", a2, a5, a8);
return 1;
} else if(a2 == a5 && a5 == a8 && a8 == 2) {
printf("winning combination\n");
printf("\n* %d *\n* %d *\n* %d *\n ", a2, a5, a8);
return 2;
}
//chk 3st column for p1 else p2
if(a3 == a6 && a6 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n* * %d\n* * %d\n* * %d\n", a3, a6, a9);
return 1;
} else if(a3 == a6 && a6 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n* * %d\n* * %d\n* * %d\n", a3, a6, a9);
return 2;
}
//chk diagnol-1 for p1 else p2
if(a1 == a5 && a5 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n%d * *\n* %d *\n* * %d\n", a1, a5, a9);
return 1;
} else if(a1 == a5 && a5 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n%d * *\n* %d *\n* * %d\n", a1, a5, a9);
return 2;
}
//chk diagnol-2 for p1 else p2
if(a3 == a5 && a5 == a7 && a7 == 1){
printf("winning combination\n");
printf("\n* * %d\n* %d *\n%d * *\n", a3, a5, a7);
return 1;
} else if(a3 == a5 && a5 == a7 && a7 == 2) {
printf("winning combination\n");
printf("\n* * %d\n* %d *\n%d * *\n", a3, a5, a7);
return 2;
}
printf("\n");
return 0;
}
//the main structure of progrmam
void main() {
int chk = 0; //turn to 1 if p1 wins. or turns to 2 if p2 wins
int user = 1; /*user determination by remainder method, incrementing user value after
every turn, using user%2 formula 0 for p-1, 1 for player-2*/
int t = 0; //count turns made
int a1 = 0; //a1-a9 position of box. 0 for empty box, 1 for p1 checked, 2 for p2 checked
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
int a6 = 0;
int a7 = 0;
int a8 = 0;
int a9 = 0;
printf("\n%d %d %d\n%d %d %d\n%d %d %d\n\n", a1, a2, a3, a4, a5, a6, a7, a8, a9);
while(chk == 0 && t < 9){
//call turn function
turn(user, t, a1, a2, a3, a4, a5, a6, a7, a8, a9);
//show the grid
printf("\n%d %d %d\n%d %d %d\n%d %d %d\n ", a1, a2, a3, a4, a5, a6, a7, a8, a9);
//chk for win combination
chk = win_cheker(a1, a2, a3, a4, a5, a6, a7, a8, a9);
}
if(chk == 1) {
printf("player1 you won");
} else if (chk == 2) {
printf("player2 you won");
} else {
printf("Match Tie");
}
_getch();
}
Comments are added to let you understand the program still if you have any further query you can comment below. Thanks hope this will help you.
#include<stdio.h>
#include<conio.h>
//Player turns
int turn(int &user, int &t, int &a1, int &a2, int &a3, int &a4, int &a5, int &a6, int &a7, int &a8, int &a9) {
int pos;
if(user%2 == 1){printf("Player1\n");}
if(user%2 == 0){printf("Player2\n");}
printf("Enter the postion You Want to selct: ");
scanf_s("%d", &pos);
if(pos == 1 && a1 == 0){//chk avalability of player selected box
if(user%2 == 1){//if player1 chosed box
a1 = 1; //box = 1 for p1 chek the box
user++; //chng user for next turn
t++; //turn count
} else if (user%2 == 0) {//if player 2 chosed box
a1 = 2; //box = 2 for p2 checked the box
user++;
t++;
}
} else if (pos == 1 && a1 != 0){//if box is already filled
printf("Already checked!\n");
}
//same structure for next 9 boxes
if(pos == 2 && a2 == 0){
if(user%2 == 1){
a2 = 1;
user++;
t++;
} else if (user%2 == 0) {
a2 = 2;
user++;
t++;
}
} else if (pos == 2 && a2 != 0){
printf("Already checked!\n");
}
if(pos == 3 && a3 == 0){
if(user%2 == 1){
a3 = 1;
user++;
t++;
} else if (user%2 == 0) {
a3 = 2;
user++;
t++;
}
} else if (pos == 3 && a3 != 0){
printf("Already checked!\n");
}
if(pos == 4 && a4 == 0){
if(user%2 == 1){
a4 = 1;
user++;
t++;
} else if (user%2 == 0) {
a4 = 2;
user++;
t++;
}
} else if (pos == 4 && a4 != 0){
printf("Already checked!\n");
}
if(pos == 5 && a5 == 0){
if(user%2 == 1){
a5 = 1;
user++;
t++;
} else if (user%2 == 0) {
a5 = 2;
user++;
t++;
}
} else if (pos == 5 && a5 != 0){
printf("Already checked!\n");
}
if(pos == 6 && a6 == 0){
if(user%2 == 1){
a6 = 1;
t++;
user++;
} else if (user%2 == 0) {
a6 = 2;
user++;
t++;
}
} else if (pos == 6 && a6 != 0){
printf("Already checked!\n");
}
if(pos == 7 && a7 == 0){
if(user%2 == 1){
a7 = 1;
user++;
t++;
} else if (user%2 == 0) {
a7 = 2;
user++;
t++;
}
} else if (pos == 7 && a7 != 0){
printf("Already checked!\n");
}
if(pos == 8 && a8 == 0){
if(user%2 == 1){
a8 = 1;
user++;
t++;
} else if (user%2 == 0) {
a8 = 2;
user++;
t++;
}
} else if (pos == 8 && a8 != 0){
printf("Already checked!\n");
}
if(pos == 9 && a9 == 0){
if(user%2 == 1){
a9 = 1;
user++;
t++;
} else if (user%2 == 0) {
a9 = 2;
user++;
t++;
}
} else if (pos == 9 && a9 != 0){
printf("Already checked!\n");
}
return 0;
}
//check if any player had made the combination
int win_cheker(int &a1, int &a2, int &a3, int &a4, int &a5, int &a6, int &a7, int &a8, int &a9){
//chk first row for p1 else p2
if(a1 == a2 && a2 == a3 && a3 == 1){
printf("winning combination\n");
printf("\n%d %d %d\n* * *\n* * *\n ", a1, a2, a3);
return 1;
} else if (a1 == a2 && a2 == a3 && a3 == 2) {
printf("winning combination\n");
printf("\n%d %d %d\n* * *\n* * *\n ", a1, a2, a3);
return 2;
}
//chk 2nd row for p1 else p2
if(a4 == a5 && a5 == a6 && a6 == 1){
printf("winning combination\n");
printf("\n* * *\n%d %d %d\n* * *\n ", a4, a5, a6);
return 1;
} else if(a4 == a5 && a5 == a6 && a6 == 2) {
printf("winning combination\n");
printf("\n* * *\n%d %d %d\n* * *\n ", a4, a5, a6);
return 2;
}
//chk 3rd row for p1 else p2
if(a7 == a8 && a8 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n|* * *\n|* * *\n|%d %d %d\n ", a7, a8, a9);
return 1;
} else if(a7 == a8 && a8 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n* * *\n* * *\n%d %d %d\n ", a7, a8, a9);
return 2;
}
//chk 1st column for p1 else p2
if(a1 == a4 && a4 == a7 && a7 == 1){
printf("winning combination\n");
printf("\n%d * *\n%d * *\n%d * *\n ", a1, a4, a7);
return 1;
} else if(a1 == a4 && a4 == a7 && a7 == 2) {
printf("winning combination\n");
printf("\n%d * *\n%d * *\n%d * *\n ", a1, a4, a7);
return 2;
}
//chk 2nd column for p1 else p2
if(a2 == a5 && a5 == a8 && a8 == 1){
printf("winning combination\n");
printf("\n* %d *\n* %d *\n* %d *\n ", a2, a5, a8);
return 1;
} else if(a2 == a5 && a5 == a8 && a8 == 2) {
printf("winning combination\n");
printf("\n* %d *\n* %d *\n* %d *\n ", a2, a5, a8);
return 2;
}
//chk 3st column for p1 else p2
if(a3 == a6 && a6 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n* * %d\n* * %d\n* * %d\n", a3, a6, a9);
return 1;
} else if(a3 == a6 && a6 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n* * %d\n* * %d\n* * %d\n", a3, a6, a9);
return 2;
}
//chk diagnol-1 for p1 else p2
if(a1 == a5 && a5 == a9 && a9 == 1){
printf("winning combination\n");
printf("\n%d * *\n* %d *\n* * %d\n", a1, a5, a9);
return 1;
} else if(a1 == a5 && a5 == a9 && a9 == 2) {
printf("winning combination\n");
printf("\n%d * *\n* %d *\n* * %d\n", a1, a5, a9);
return 2;
}
//chk diagnol-2 for p1 else p2
if(a3 == a5 && a5 == a7 && a7 == 1){
printf("winning combination\n");
printf("\n* * %d\n* %d *\n%d * *\n", a3, a5, a7);
return 1;
} else if(a3 == a5 && a5 == a7 && a7 == 2) {
printf("winning combination\n");
printf("\n* * %d\n* %d *\n%d * *\n", a3, a5, a7);
return 2;
}
printf("\n");
return 0;
}
//the main structure of progrmam
void main() {
int chk = 0; //turn to 1 if p1 wins. or turns to 2 if p2 wins
int user = 1; /*user determination by remainder method, incrementing user value after
every turn, using user%2 formula 0 for p-1, 1 for player-2*/
int t = 0; //count turns made
int a1 = 0; //a1-a9 position of box. 0 for empty box, 1 for p1 checked, 2 for p2 checked
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
int a6 = 0;
int a7 = 0;
int a8 = 0;
int a9 = 0;
printf("\n%d %d %d\n%d %d %d\n%d %d %d\n\n", a1, a2, a3, a4, a5, a6, a7, a8, a9);
while(chk == 0 && t < 9){
//call turn function
turn(user, t, a1, a2, a3, a4, a5, a6, a7, a8, a9);
//show the grid
printf("\n%d %d %d\n%d %d %d\n%d %d %d\n ", a1, a2, a3, a4, a5, a6, a7, a8, a9);
//chk for win combination
chk = win_cheker(a1, a2, a3, a4, a5, a6, a7, a8, a9);
}
if(chk == 1) {
printf("player1 you won");
} else if (chk == 2) {
printf("player2 you won");
} else {
printf("Match Tie");
}
_getch();
}