Linker error in turboc++: undefined symbol - linker

I am trying the following program on TurboC. I am getting a linker error for one of the function prototypes.
The error is: undefined symbol checkbranch(char near*) in module student.cpp where student.cpp is the name of the file.
The code compiles with no errors and I am not able to detect the error here. Any help would be appreciated.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
#include<fstream.h>
#include<string.h>
#include<ctype.h>
void checkbranch(char string[]);
void checkusn(char string[]);
void checksem(char string[]);
void checkname(char string[]);
fstream file;
class iobuffer
{
public: char maxbyte[120];
iobuffer();
};
iobuffer::iobuffer()
{
for(int i=0; i<120; i++)
maxbyte[i]='\0';
}
class student
{
public: char usn[20], name[25], sem[10], address[20], branch[10];
student()
{
strcpy(name,"");
strcpy(usn, "");
strcpy(sem, "");
strcpy(branch, "");
strcpy(address, "");
}
void read();
void display();
void rsem();
void rname();
void rusn();
void rbranch();
void raddress();
};
void student::rusn()
{
cout<<"\n\tEnter the new usn: ";
gets(usn);
checkusn(usn);
}
void student::rname()
{
cout<<"\n\tEnter the name: ";
gets(name);
checkname(name);
}
void student::rsem()
{
cout<<"\n\tEnter the sem: ";
gets(sem);
checksem(sem);
}
void checkbranch(char string[]);
void student::rbranch()
{
cout<<"\n\tEnter the branch: ";
gets(branch);
checkbranch(branch);
}
void student::raddress()
{
cout<<"\n\tEnter the address: ";
gets(address);
}
void checkname(char arr[])
{
for(int j=0; arr[j]!='\0'; j++)
if((!isalpha(arr[j]))&&(arr[j]!=' '))
{
cout<<"\n\tWrong input. Re-enter: ";
gets(arr);
j=-1;
}
}
void student::read()
{
cout<<"\n\tUSN: ";
gets(usn);
checkusn(usn);
cout<<"\n\tName: ";
gets(name);
checkname(name);
cout<<"\n\tSem: ";
gets(sem);
checksem(sem);
cout<<"\n\tBranch: ";
gets(branch);
checkbranch(branch);
cout<<"\n\tAddress: ";
gets(address);
}
void checksem(char arr[])
{
int x=0;
if((strcmp(arr,"1")==0)||(strcmp(arr,"2")==0)||(strcmp(arr,"3")==0)||(strcmp(arr,"4")==0)||(strcmp(arr,"5")==0)||(strcmp(arr,"6")==0)||(strcmp(arr,"7")==0)||(strcmp(arr,"8")==0))
x=1;
if(x==0)
{
cout<<"\n\tInvalid sem. Re-enter: ";
gets(arr);
checksem(arr);
}
}
void checkusn(char string[])
{
fstream ftemp;
char temp[20], tempusn[20], usn[20];
int i;
if((strlen(string)!=10)||(!isdigit(string[0]))||(!isdigit(string[3]))||(!isdigit(string[4]))||(!isdigit(string[7]))||(!isdigit(string[8]))||(!isdigit(string[9]))||(!isalpha(string[1]))||(!isalpha(string[2]))||(!isalpha(string[5]))||(!isalpha(string[6])))
{
cout<<"\n\tInvalid USN. Re-enter: ";
gets(string);
checkusn(string);
}
file.open("studentdb.txt",ios::in|ios::binary);
while(file.read(temp,120))
{
i=0;
while((tempusn[i]=temp[i])!='|')
i++;
tempusn[i]='\0';
if(strcmp(tempusn,string)==0)
{
cout<<"\n\tUSN already exists. Re-enter: ";
gets(string);
checkusn(string);
}
}
file.close();
}
void student::display()
{
cout<<setiosflags(ios::left);
cout<<setw(15)<<usn<<setw(20)<<name<<setw(10)<<sem<<setw(15)<<branch<<setw(25)<<address;
}
class fixedlength:public iobuffer
{
public: char buffer[120];
void pack(student s);
void unpack(student s);
};
void fixedlength::pack(student s)
{
strcpy(maxbyte, "");
strcpy(maxbyte, s.usn);
strcat(maxbyte,"|");
strcat(maxbyte, s.name);
strcat(maxbyte, "|");
strcat(maxbyte, s.sem);
strcat(maxbyte, "|");
strcat(maxbyte, s.branch);
strcat(maxbyte, "|");
strcat(maxbyte, s.address);
strcat(maxbyte, "#");
}
void fixedlength::unpack(student s)
{
strcpy(buffer, maxbyte);
for(int i=0, j=1, k=0; j<=5; i++, j++)
{
switch(j)
{
case 1: while(buffer[i]!='|')
s.usn[k++]=buffer[i++];
s.usn[k]='\0';
k=0;
break;
case 2: while(buffer[i]!='|')
s.name[k++]=buffer[i++];
s.name[k]='\0';
k=0;
break;
case 3: while(buffer[i]!='|')
s.sem[k++]=buffer[i++];
s.sem[k]='\0';
k=0;
break;
case 4: while(buffer[i]!='|')
s.branch[k++]=buffer[i++];
s.branch[k]='\0';
k=0;
break;
case 5: while(buffer[i]!='\0')
s.address[k++]=buffer[i++];
s.address[k]='\0';
k=0;
}
}
}
class delim:public fixedlength
{
public: void writerecord(student);
void readrecord(student);
void deleterecord(student, char*);
void searchrecord(student, char*);
void modifyrecord(student, char*);
};
void delim::writerecord(student s)
{
pack(s);
file.open("studentdb.txt", ios::app|ios::binary);
file.write((char*)&maxbyte, sizeof(maxbyte));
file.close();
}
void delim::readrecord(student s)
{
file.open("studentdb.txt", ios::in|ios::binary);
cout<<"\n\n File Contents\n\n";
while(file.read((char*)&maxbyte, sizeof(maxbyte)))
{
unpack(s);
cout<<endl;
s.display();
}
getch();
file.close();
}
void delim::searchrecord(student s, char *key)
{
int flag=0;
file.open("studentdb.txt",ios::in|ios::binary);
while((file.read((char*)&maxbyte,sizeof(maxbyte)))&&flag==0)
{
unpack(s);
if(!strcmp(s.usn,key))
{
flag=1;
s.display();
}
}
if(flag==0)
{
cout<<"\n\tSpecified record doesn't exist";
getch();
file.close();
}
}
void delim::deleterecord(student s, char *a)
{
int flag=0;
char x;
fstream ftemp;
file.open("studentdb.txt",ios::in|ios::binary);
ftemp.open("temp.txt", ios::out|ios::trunc|ios::binary);
cout<<"\n\tAre you sure you want to delete (y/n)?";
cin>>x;
if(x=='y'||x=='Y')
{
while(file.read((char*)&maxbyte, sizeof(maxbyte)))
{
unpack(s);
if(strcmp(s.usn,a)==0)
flag=1;
else
ftemp.write((char*)&maxbyte,sizeof(maxbyte));
}
}
if(flag==1)
{
file.close();
ftemp.close();
remove("studentdb.txt");
rename("temp.txt","studentdb.txt");
}
if(flag==0)
{
cout<<"\n\tThe specified record is not found\n";
file.close();
ftemp.close();
}
}
void delim::modifyrecord(student s, char *a)
{
int flag=0, choice;
file.open("studendb.txt",ios::in|ios::out|ios::binary);
while(file.read((char*)&maxbyte,sizeof(maxbyte)))
{
unpack(s);
if(strcmp(s.usn,a)==0)
{
char x;
flag=1;
cout<<"\n\t Current contents: \n\n\n";
s.display();
do
{
cout<<"What do you want to change: ";
cout<<"\n\t1.Name\n\t2. Sem\n\t3. Branch\n\t4. Address\n\t5. Confirm changes\n\t6. Return to main\n";
cout<<"Enter choic: ";
cin>>choice;
switch(choice)
{
case 1: s.rname(); break;
case 2: s.rsem(); break;
case 3: s.rbranch(); break;
case 4: s.raddress(); break;
case 5: break;
case 6: return;
default: cout<<"Invalid option\n";
}
}while(choice==1||choice==2||choice==3||choice==4);
cout<<"\n\tDo you want to modify (y\n)?";
cin>>x;
if(x=='y'||x=='Y')
{
pack(s);
file.seekg(-120,ios::cur);
file.write((char*)&maxbyte,sizeof(maxbyte));
}
else
break;
}
}
if(flag==0)
{
cout<<"\n\t Specified record doesn't exist\n";
file.close();
}
}
void main()
{
clrscr();
iobuffer i;
student s;
fixedlength f;
delim d;
int option;
char no[20], key[20];
for(;;)
{
cout<<"\n\nMenu:- ";
cout<<"\n\t1. Insert\n\t2. Display\n\t3. Delete\n\t4. Modify\n\t5. Search\n\t6. End\n\t";
cin>>option;
switch(option)
{
case 1: s.read();
d.writerecord(s);
break;
case 2: d.readrecord(s);
break;
case 3: cout<<"Enter the usn to delete\n";
gets(no);
d.deleterecord(s, no); break;
case 5: cout<<"\nEnter usn to be searched\n";
gets(key);
d.searchrecord(s,key);
break;
case 4: cout<<"\n\tEnter the usn to be modified\n";
gets(no);
d.modifyrecord(s,no);
break;
case 6: exit(0); break;
}
}
}
I wasn't sure what the error would be therefore I tried:
Reordering the function prototypes statements (even then error would
only be for checkbranch)
Writing the function prototype for checkbranch elsewhere (again error would occur)
Removing the prototype itself (and then get the compilation error for lack of
prototype)

You have to implement the checkbranch function, compiler can not do that for you.

Related

In this trie data structure implementation the display function is not working.Why?

When i display I am getting different answer but search is working. It could be a semantic error.Please help to correct it. It could have an error somewhere. I don't know where is it.Please help me to find it.The insert does not work in the program.It could be an error somewhere.I need this trie data structure to work.
#include<stdio.h>
#include<stdlib.h>
#define maxlength 10
typedef struct node
{
int isend;
struct node *branch[27];
}trinode;
int len,count;
trinode *createnode()
{
trinode *new=(trinode *)malloc(sizeof(trinode));
int ch;
for(ch=0;ch<27;ch++)
{
new->branch[ch]=NULL;
}
new->isend=0;
return new;
}
trinode *insert_trie(trinode *root,char *newenty)
{int ind;
trinode *proot;
if(root==NULL)
root=createnode();
proot=root;
for(int i=0;i<maxlength;i++)
{
ind=newenty[i]-'a';
if(newenty[i]=='\0')
break;
else
{
if(root->branch[ind]==NULL)
root->branch[ind]=createnode();
root=root->branch[ind];
}
}
if(root->isend!=0)
printf("trying to insert duplicate");
else
root->isend=1;
return proot;
}
void print_trie(trinode *cur)
{
int count;
char word[40];
for(int i=0;i<26;i++)
{
if(cur->branch[i]!=NULL)
{
word[count++]=(i+'a');
if((cur->branch[i]->isend)==1)
{
printf("\n");
for(int j=0;j<count;j++)
{
printf("%c",word[j]);
}
}
print_trie(cur->branch[i]);
}
}
count--;
}
int search_trie(trinode *root,char *target)
{
int ind;
for(int i=0;i<maxlength &&root;i++)
{
ind=target[i]-'a';
if(target[i]=='\0')
break;
else
root=root->branch[ind];
}
if(root && root->isend==1)
return 1;
else
return 0;
}
int main()
{
int ch;
trinode *root=NULL;
char *newenty;
char *target;
int check;
newenty = (char *)malloc(maxlength);
target= (char *)malloc(maxlength);
while(1)
{
printf("\n enter option 1.insert_trie 2.display 3.search 4.exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter word");
scanf("%s",newenty);
root=insert_trie(root,newenty);
break;
case 2:
count =0;
print_trie(root);
break;
case 3:
printf("enter elem you want to search");
scanf("%s",target);
check=search_trie(root,target);
if(check==0)
printf("word not found");
else
printf("found");
break;
case 4:
exit(0);
break;
}
}
}

Heap Code Error

I am getting an error in the following code. I am building a heap and for that I use the buildup and move-up functions whereas for deleting the values I use move-down and deletion functions. And for inserting the values I use insert function.
My output is showing error for the deletion part.
#include<stdio.h>
#include<stdlib.h>
int a[100];
int size;
void display();
void moveup(int);
void insert(int);
void buildup();
int deletion();
void movedown(int);
int h;
void main()
{
size=5;
int i,j;
printf("enter the elements\n");
for(i=1;i<=5;i++)
{
scanf("%d",&a[i]);
}
a[0]=32767;
printf("\n");
buildup();
display();
insert(9);
display();
printf("\n after deletion\n");
deletion();
display();
}
void buildup()
{
int i;
for(i=2;i<=size;i++)
moveup(i);
}
void moveup(int i)
{
int p,temp;
temp=a[i];
p=i/2;
while(temp>a[p])
{
a[i]=a[p];
i=p;
p=i/2;
}
a[i]=temp;
}
void display()
{
int i;
for(i=1;i<=size;i++)
printf("%3d\n",a[i]);
}
void insert(int t)
{
printf("\n after insertion\n");
if(size==99)
printf("insertion not possible");
a[++size]=t;
moveup(size);
}
int deletion()
{
int t;
t=a[1];
h=a[size--];
a[1]=h;
movedown(1);
return t;
}
void movedown(int i)
{
int l;
int r;
i=2*i;
r=l+1;
while(r<=size)
{
if(h>a[l]&&h>a[r])
{
a[i]=h;
return;
}
else
{
if(a[l]>a[r])
{
a[i]=a[l];
i=l;
}
else
{
a[i]=a[r];
i=r;
}
}
l=2*i;
r=l+1;
}
if(l<=size&&a[l]>h)
{
a[i]=a[l];
i=l;
}
a[i]=h;
}

Snake Game in C about the switch statement

It's a simple game,just aim to input a character,move the snake and print the table everytime.
The switch statement in the main function settle 4 cases'a' 's' 'd' 'w' without default,However,when the program is running,if a character out of the cases is input,the program still reacts and output the table(besides,the talbe is output twice).Why?
Besides,it also confused me that the snake don't move correctly if the input is in the cases.
Forgot to add 'break'after cases before:)but the problem still confuse me.Now if I use'break'under default,the program still print the table twice,if I use 'continue'under default,the program won't react to the incorrect input.
But the snake still don't move correctly.:(
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SNAKE_MAX_LENGTH 20
void snakeMove(int, int);
void put_money(void);
void output(void);
void gameover(void);
char map[12][12] =
{"************",
"*XXXXH *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"};
int snakeX[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};
int snakeY[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};
int snakeLength = 5;
int game=1,origin=1,eaten=0;
int main() {
char ch;
srand(time(NULL));
put_money();
output();
origin=0;
while(game) {
scanf("%c",&ch);
switch(ch) {
case 'w':
snakeMove(1,1);
break;
case 's':
snakeMove(2,1);
break;
case 'a':
snakeMove(3,1);
break;
case 'd':
snakeMove(4,1);
break;
default:
continue;
}
if(eaten==1) {
put_money();
eaten=0;
}
output();
}
gameover();
return 0;
}
void snakeMove(int direction, int distance) {
int i,move=1;
switch(direction) {
case 1:
if(snakeY[3]==snakeY[4]+distance)
move=0;
else
snakeY[4]=snakeY[4]+distance;
break;
case 2:
if(snakeY[3]==snakeY[4]-distance)
move=0;
else
snakeY[4]=snakeY[4]-distance;
break;
case 3:
if(snakeX[3]==snakeX[4]-distance)
move=0;
else
snakeX[4]=snakeX[4]-distance;
break;
case 4:
if(snakeX[3]==snakeX[4]+distance)
move=0;
else
snakeX[4]=snakeX[4]+distance;
}
if (move==1) {
for(i=3;i>=0;i--){
snakeX[i]=snakeX[i+1];
snakeY[i]=snakeY[i+1];
}
}
}
void output(void) {
int x, y,i;
if(origin==0) {
for(x=1;x<11;x++) {
for(y=1;y<11;y++) {
if(map[x][y]!='$')
map[x][y]=' ';
}
}
if(map[snakeX[4]][snakeY[4]]=='$')
eaten=1;
for(i=0;i<4;i++)
map[snakeX[i]][snakeY[i]]='X';
map[snakeX[4]][snakeY[4]]='H';
if(snakeX[4]==0||snakeX[4]==11||snakeY[4]==0||snakeY[4]==11)
game=0;
for(i=0;i<4;i++) {
if(snakeX[4]==snakeX[i]&&snakeY[4]==snakeY[i])
game=0;
}
}
for(x=0;x<12;x++) {
for(y=0;y<12;y++)
printf("%c",map[x][y]);
printf("\n");
}
}
void gameover(void) {
printf("Game Over!!!\n");
exit(0);
}
void put_money(void) {
int foodx,foody,done=0;
while(done==0) {
foodx=rand()%12;
foody=rand()%12;
if(map[foodx][foody]==' ') {
map[foodx][foody]='$';
done=1;
}
}
}
Interesting. Your switch statement was fine. Here is a working code with changes commented, followed by a whole code:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SNAKE_MAX_LENGTH 20
void snakeMove(int, int);
void put_money(void);
void update_snake(void);
void output(void);
void gameover(void);
char map[12][12] =
{"************",
"*XXXXH *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"};
int snakeX[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};
int snakeY[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};
int snakeLength = 5;
int game=1,origin=1,eaten=0;
int main() {
char ch;
srand(time(NULL));
put_money();
output();
origin=0;
while(game) {
As said by Cool guy, a space in scanf works. This is because when you press enter, the newline is counted as a character.
scanf(" %c",&ch);
switch(ch) {
case 'w':
snakeMove(1,1);
break;
case 's':
snakeMove(2,1);
break;
case 'a':
snakeMove(3,1);
break;
case 'd':
snakeMove(4,1);
break;
default:
continue;
}
if(eaten==1) {
put_money();
eaten=0;
}
output();
}
gameover();
return 0;
}
This update function was added because you need to update the snake before you update the head. Otherwise, you'd lose the original head when you try to send it back.
void update_body()
{
int i;
for(i=0;i<4;i++){
snakeX[i]=snakeX[i+1];
snakeY[i]=snakeY[i+1];
}
}
void snakeMove(int direction, int distance) {
int move=1;
switch(direction) {
case 1:
The third issue is that in graphics, the origin, (0, 0) is in the top left corner instead of the bottom left. That means to go up, you have to subtract and to go down you have to add.
if(snakeY[3]==snakeY[4]-distance)
move=0;
else{
update_body();
snakeY[4]=snakeY[4]-distance;
}
break;
case 2:
if(snakeY[3]==snakeY[4]+distance)
move=0;
else{
update_body();
snakeY[4]=snakeY[4]+distance;
}
break;
case 3:
if(snakeX[3]==snakeX[4]-distance)
move=0;
else{
update_body();
snakeX[4]=snakeX[4]-distance;
}
break;
case 4:
if(snakeX[3]==snakeX[4]+distance)
move=0;
else{
update_body();
snakeX[4]=snakeX[4]+distance;
}
}
}
void output(void) {
int x, y,i;
if(origin==0) {
for(x=1;x<11;x++) {
for(y=1;y<11;y++) {
if(map[x][y]!='$')
map[x][y]=' ';
}
}
The last problem is that the map is upside down. This means that the X and Y coordinates were 1st and 2nd, when they should have been 2nd and 1st.
if(map[snakeY[4]][snakeX[4]]=='$')
eaten=1;
for(i=0;i<4;i++)
map[snakeY[i]][snakeX[i]]='X';
map[snakeY[4]][snakeX[4]]='H';
if(snakeX[4]==0||snakeX[4]==11||snakeY[4]==0||snakeY[4]==11)
game=0;
for(i=0;i<4;i++) {
if(snakeX[4]==snakeX[i]&&snakeY[4]==snakeY[i])
game=0;
}
}
for(x=0;x<12;x++) {
for(y=0;y<12;y++)
printf("%c",map[x][y]);
printf("\n");
}
}
void gameover(void) {
printf("Game Over!!!\n");
exit(0);
}
void put_money(void) {
int foodx,foody,done=0;
while(done==0) {
foodx=rand()%12;
foody=rand()%12;
if(map[foodx][foody]==' ') {
map[foodx][foody]='$';
done=1;
}
}
}
Here's the code whole
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SNAKE_MAX_LENGTH 20
void snakeMove(int, int);
void put_money(void);
void update_body();
void output(void);
void gameover(void);
char map[12][12] =
{"************",
"*XXXXH *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"* *",
"************"};
int snakeX[SNAKE_MAX_LENGTH] = {1, 2, 3, 4, 5};
int snakeY[SNAKE_MAX_LENGTH] = {1, 1, 1, 1, 1};
int snakeLength = 5;
int game=1,origin=1,eaten=0;
int main() {
char ch;
srand(time(NULL));
put_money();
output();
origin=0;
while(game) {
scanf(" %c",&ch);
switch(ch) {
case 'w':
snakeMove(1,1);
break;
case 's':
snakeMove(2,1);
break;
case 'a':
snakeMove(3,1);
break;
case 'd':
snakeMove(4,1);
break;
default:
continue;
}
if(eaten==1) {
put_money();
eaten=0;
}
output();
}
gameover();
return 0;
}
void update_body()
{
int i;
for(i=0;i<4;i++){
snakeX[i]=snakeX[i+1];
snakeY[i]=snakeY[i+1];
}
}
void snakeMove(int direction, int distance) {
int move=1;
switch(direction) {
case 1:
if(snakeY[3]==snakeY[4]-distance)
move=0;
else{
update_body();
snakeY[4]=snakeY[4]-distance;
}
break;
case 2:
if(snakeY[3]==snakeY[4]+distance)
move=0;
else{
update_body();
snakeY[4]=snakeY[4]+distance;
}
break;
case 3:
if(snakeX[3]==snakeX[4]-distance)
move=0;
else{
update_body();
snakeX[4]=snakeX[4]-distance;
}
break;
case 4:
if(snakeX[3]==snakeX[4]+distance)
move=0;
else{
update_body();
snakeX[4]=snakeX[4]+distance;
}
}
}
void output(void) {
int x, y,i;
if(origin==0) {
for(x=1;x<11;x++) {
for(y=1;y<11;y++) {
if(map[x][y]!='$')
map[x][y]=' ';
}
}
if(map[snakeY[4]][snakeX[4]]=='$')
eaten=1;
for(i=0;i<4;i++)
map[snakeY[i]][snakeX[i]]='X';
map[snakeY[4]][snakeX[4]]='H';
if(snakeX[4]==0||snakeX[4]==11||snakeY[4]==0||snakeY[4]==11)
game=0;
for(i=0;i<4;i++) {
if(snakeX[4]==snakeX[i]&&snakeY[4]==snakeY[i])
game=0;
}
}
for(x=0;x<12;x++) {
for(y=0;y<12;y++)
printf("%c",map[x][y]);
printf("\n");
}
}
void gameover(void) {
printf("Game Over!!!\n");
exit(0);
}
void put_money(void) {
int foodx,foody,done=0;
while(done==0) {
foodx=rand()%12;
foody=rand()%12;
if(map[foodx][foody]==' ') {
map[foodx][foody]='$';
done=1;
}
}
}

What is causing my blackjack program to crash before doing anything?

When I run my program, it will print "Welcome to Blackjack! Rules: " and then an error window will come up saying "Blackjack.exe has stopped working and I have to close the program. In the compiler window it says "Process terminated". How do I stop this from happening?
Is one of the loops not working correctly?
My code:
#include <stdio.h>
#include <stdlib.h>
void Initializedeck(int[]);
void Shuffledeck(int[]);
void displayscore(int, int);
int hdrawcards(int[], int[]);
char getsuit(int);
int cdrawcards(int[], int[]);
int main()
{
int deck[52];
char cont;
int hcherries=10;
int ccherries=10;
int hcards[10];
int ccards[10];
int htotal;
int ctotal;
printf("Welcome to Blackjack!\n");
printf("Rules: \n ");
Initializedeck(deck);
cont='y';
while(cont=='y')
{
Shuffledeck(deck);
system("cls");
displayscore(hcherries, ccherries);
htotal=hdrawcards(hcards, deck);
ctotal=cdrawcards(ccards, deck);
if(htotal>ctotal)
{
printf("You Win!");
}
else
{
printf("Computer Wins. :( ");
}
printf("Do you want to continue?(y/n)");
scanf("%c", &cont);
}
return 0;
}
void Initializedeck(int deck[])
{
int i=0;
while(i<52)
{
deck[i]=i;
i++;
}
}
void Shuffledeck(int deck[])
{
int hold;
int max=51;
int random;
while(max>=0)
{
random=rand()%(max)+1;
hold=deck[max];
deck[max]=random;
random=hold;
max--;
}
}
void displayscore(int hcherries, int ccherries)
{
printf("Human: %i(cherries) Computer: %i(cherries)", hcherries, ccherries);
}
int hdrawcards(int hcards[], int deck[])
{
char answer;
int i=0;
int score=0;
int total=0;
char rank;
int worth;
char suit;
int card;
printf("Do you want to draw a card? (y/n)");
scanf("%c", &answer);
while(answer=='y' && total<21)
{
score=score+deck[i];
hcards[i]=deck[i];
card=hcards[i];
worth=hcards[i]%13;
if(worth==10)
{
rank='T';
}
else if (worth==11)
{
rank='J';
}
else if (worth==12)
{
rank='Q';
}
else if (worth==13)
{
rank='K';
}
else if(worth==1)
{
rank='A';
}
else
{
rank=worth;
}
if(worth>10)
{
worth=10;
}
total=total+worth;
suit=getsuit(card);
printf("%c%c\n", rank, suit);
printf("Total: %i", total);
i++;
scanf("%c", &answer);
}
return total;
}
int cdrawcards(int ccards[], int deck[])
{
int i=26;
int total;
int score;
int worth;
char rank;
char suit;
int card;
while(total<18)
{
score=score+deck[i];
ccards[i]=deck[i];
card=ccards[i];
worth=ccards[i]%13;
if(worth==10)
{
rank='T';
}
else if (worth==11)
{
rank='J';
}
else if (worth==12)
{
rank='Q';
}
else if (worth==13)
{
rank='K';
}
else if(worth==1)
{
rank='A';
}
else
{
rank=worth;
}
if(worth>10)
{
worth=10;
}
total=total+worth;
suit=getsuit(card);
printf(" %c%c\n", rank, suit);
printf(" Total: %i", total);
i++;
}
return total;
}
char getsuit(int card)
{
char suit;
if(card<13)
{
suit='S';
}
else if(card>=13 && card<26)
{
suit='H';
}
else if(card>=26 && card<39)
{
suit='D';
}
else if(card>=39 && card<52)
{
suit='C';
}
return suit;
}
You have an array overflow in cdrawcards():
int ccards[10];
...
ctotal = cdrawcards(ccards, deck);
...
int cdrawcards(int ccards[], int deck[])
{
int i = 26;
...
ccards[i] = deck[i]; //Overflow, ccards[] only has 10 elements
Either change the initial value of i or make the array larger int ccards[52]. I would also add an explicit check in cdrawcards() and hdrawcards() to ensure you don't overflow the arrays, like:
while (total < 18 && i < 10)
For example, in hdrawcards() you can overflow the array if you get a bunch of low value cards in a row (ex: 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3(overflow), 3, ...).

infix to postfix using stack error in updating top variable

I'm trying to implement a code for infix to postfix conversion but I'm not able to obtain the output. By analyzing the program I found that the variable top s not getting updated even though I'm returning its value back to the main. Please help me figure it out. The code is:
#include<stdio.h>
char stack[15];
int check(char op)
{
int rank=0;
switch(op)
{
case '/':
rank=1;
break;
case '*':
rank=2;
break;
case '+':
rank=3;
break;
case '-':
rank=4;
break;
}
return rank;
}
int POP(char stack[15],int top)
{
if(top==-1)
{
printf("Stack Underflow");
return top;
}
else
{
top--;
printf("%c",stack[top+1]);
return top;
}
}
int PUSH(char stack[15],int top,char op)
{
char opstack;
int rank1,rank2;
if(top>=14)
{
printf("Stack Overflow");
return top;
}
else
{
if(top==-1)
{
top++;
stack[top]=op;
return top;
}
else
{
opstack=stack[top];
rank1=check(op);
rank2=check(opstack);
if( rank1 <= rank2 )
{
top++;
stack[top]=op;
}
else
{
top=POP(stack,top);
top=PUSH(stack,top,op);
}
return top;
}
}
}
int main()
{
char string[15],ch;
int top=-1,i;
printf("Enter the infix operation: ");
gets(string);
fflush(stdin);
for(i=0;string[i]!='\0';i++)
{
ch=string[i];
if( ((ch>='a') && (ch<='z'))||((ch>='A') &&(ch<='Z')) )
{
printf("%c",string[i]);
}
else
{
ch=string[i];
top=PUSH(stack,top,ch);
}
top=POP(stack,top);
}
return 0;
}
in main function add these three lines after for loop End.and remove top=POP(stack,top);
inside for loop.
while(top!=-1)
top=POP(stack,top);
printf("\n");
modified main function:
int main()
{
char string[15],ch;
int top=-1,i;
printf("Enter the infix operation: ");
gets(string);
fflush(stdin);
for(i=0;string[i]!='\0';i++)
{
ch=string[i];
if( ((ch>='a') && (ch<='z'))||((ch>='A') &&(ch<='Z')) )
{
printf("%c",string[i]);
}
else
{
ch=string[i];
top=PUSH(stack,top,ch);
}
}
while(top!=-1)
top=POP(stack,top);
printf("\n");
return 0;
}

Resources