Hey guys and gals! I'm having a bit of problem with my C codes. I was supposed to make a program that adds, removes and views books. Yeahp, it's a Library System. Well, the problem is that when I input some text in like "Hello Hi", it would automatically jump to "Thank you for using the Library System" and would eventually exit. It happens when I input two or more words. I was able to add the books and all, and there are no errors, but when I get to that part, it does that. I can add 1-word letters like "Hello" and "Hi" and it wouldn't have any errors. Can you tell me what the problem is? :(
Here's my code!
There, edited it. I hope it's clear enough. XDCode:#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<windows.h>
char decide, choice;
int booknum;
struct libbooks{
char title[40];
char author[50];
} book1, book2, book3, book4, book5, bookx[99];
void viewbook();
void addbook();
void adminact();
void removebook();
void open();
main()
{
int adminuser, adminpass, memberuser, memberpass, cmpadmin, cmpmember;
char user[20], pass[20];
strcpy(book1.title,"\n\t\t\t The Legends Of Tom Sawyer ");
strcpy(book2.title,"\t\t\t The Success Principles ");
strcpy(book3.title,"\t\t\t The Lord Of The Rings ");
strcpy(book4.title,"\t\t\t Star Wars: Clone Wars ");
strcpy(book5.title,"\t\t\t Philosophy Of the Human Mind ");
puts("\n\n");
puts("\t\t\t******************************");
puts("\t\t\t*Don Bosco Technical College*");
puts("\t\t\t******************************");
puts("\n");
puts("\t\t\t******************************");
puts("\t\t\t* Welcome to Library System. *");
puts("\t\t\t******************************");
puts("\t\t\t\tEnter username: ");
printf("\t\t\t\t ");
scanf("%s", user);
puts("\t\t\t\tEnter password: ");
printf("\t\t\t\t ");
scanf("%s", pass);
adminuser = strcmp(user, "admin");
adminpass = strcmp(pass, "dbtcadmin");
memberuser = strcmp(user, "member");
memberpass = strcmp(pass, "dbtcmember");
if(adminuser == 0 && adminpass == 0)
adminact();
else if(memberuser == 0 && memberpass == 0)
printf("Declare function");
else printf("Cannot log in. Username and Password invalid");
getch();
}
void addbook()
{
int i;
system("cls");
puts("\n\n");
puts("\t\t\t************************************\n");
printf("\t\t ** How many books do you want to enter? **\n");
puts("\n\t\t\t************************************");
printf("\n\t\t\t You may enter up to 100 books.\n");
puts("\n\t\t\t************************************");
printf("\t\t\t\t\t ");
scanf("%d", &booknum);
printf("\t\t\t\t Enter the title(s):\n");
printf(" \t\t\t\t");
for (i=0;i<booknum;i++){
printf("\n \t\t\t\t");
scanf("%s", bookx[i].title);
}
printf("\n\tBooks successfully added.\n");
printf("\n\tDo you want to go back to the option admin system?<Y/N>");
scanf("%c", &choice);
choice = getch();
switch(choice)
{
case 'Y': case 'y':
return adminact();
break;
case 'N': case 'n':
printf("\n\nThank you for using the Library System!");
break;
default: printf("\nNot one of the choices.");
break;
}
}
void viewbook()
{
int x, y;
system("cls");
puts("\n\n");
puts("\t\t\t********************************");
printf("%s\n%s\n%s\n%s\n%s", book1.title, book2.title, book3.title, book4.title, book5.title);
for(x = 0; x<booknum; x++)
printf("\n\t\t\t %s",bookx[x].title);
puts("\n");
puts("\t\t\t********************************");
puts("\n");
printf("\t\tDo you want to go back to the option system?<Y/N>");
scanf("%c", &choice);
choice = getch();
switch(choice)
{
case 'Y': case 'y':
return adminact();
break;
case 'N': case 'n':
printf("\n\n\t\tThank you for using the Library System!");
break;
default: printf("\nNot one of the choices.");
break;
}
}
void adminact()
{
int choice;
system("cls");
puts("\n\n");
puts("\t\t\t# **************************** #");
printf("\n\t\t\t# * What do you want to do? * #\n");
puts("\n\t\t\t# **************************** #");
puts("\t\t\t# **************************** #");
puts("\n\t\t\t# * 1. View book(s) * #");
puts("\t\t\t# * 2. Add Book(s) * #");
puts("\t\t\t# * 3. Remove book(s) * #");
puts("\t\t\t# * 4. Return to Log-in * #");
puts("\n\t\t\t# **************************** #");
printf("\t\t\t\t ");
scanf("%d", &choice);
switch(choice)
{
case 1:
viewbook();
break;
case 2:
addbook();
break;
case 3:
removebook();
break;
case 4:
open();
break;
default: printf("Thank you for using the Library System!");
}
}
void removebook(){
int r, r1;
int x, y;
system("cls");
puts("\n\n");
puts("\t\t\t********************************");
printf("%s\n%s\n%s\n%s\n%s", book1.title, book2.title, book3.title, book4.title, book5.title);
for(x = 0; x<booknum; x++)
printf("\n\t\t\t %s",bookx[x].title);
puts("\n");
puts("\t\t\t********************************");
printf("\n\t\t Select the books you want to Remove.");
printf("\n\n\t\t\t\t");
r1 = getch();
scanf("%d", &r1);
switch(r1){
case 1: strcpy(book1.title," ");return adminact();break;
case 2: strcpy(book2.title," ");return adminact();break;
case 3: strcpy(book3.title," ");return adminact();break;
case 4: strcpy(book4.title," ");return adminact();break;
case 5: strcpy(book5.title," ");return adminact();break;
default: for(x = 0; x<booknum; x++)
strcpy(bookx[x].title," ");
return adminact();
}
}
void open()
{
int adminuser, adminpass, memberuser, memberpass, cmpadmin, cmpmember;
char user[20], pass[20];
system("cls");
puts("\n\n");
puts("\t\t\t******************************");
puts("\t\t\t*Don Bosco Technical College*");
puts("\t\t\t******************************");
puts("\n");
puts("\t\t\t******************************");
puts("\t\t\t* Welcome to Library System. *");
puts("\t\t\t******************************");
puts("\t\t\t\tEnter username: ");
printf("\t\t\t\t ");
scanf("%s", user);
puts("\t\t\t\tEnter password: ");
printf("\t\t\t\t ");
scanf("%s", pass);
adminuser = strcmp(user, "admin");
adminpass = strcmp(pass, "dbtcadmin");
memberuser = strcmp(user, "member");
memberpass = strcmp(pass, "dbtcmember");
if(adminuser == 0 && adminpass == 0)
adminact();
else if(memberuser == 0 && memberpass == 0)
printf("Declare function");
else printf("Cannot log in. Username and Password invalid");
}