Question
C++ help fixing invalid inputs When i use option 1 (add a book) or option 2 (add a user) i cant type a name with
C++ help fixing invalid inputs
When i use option 1 (add a book) or option 2 (add a user) i cant type a name with a space if i do the program fails.
for example test add a book for "the way of kings". what happens is the code keeps looping.
It would be great if you could debug this.
What i have so far (modify this code):
most of it is written just fix up the mistakes
#include
using namespace std;
//structure to store library data
struct library{
int code;
char title[20];
int status;
};
//structure to store user data
struct users{
int id;
char name[20];
int booksNumber;
};
int main()
{
int tempId,i,flag=0,j;
char tempName[20];
int tempCode;
struct users user_details[100];//variable to hold 100 userdetails
struct library book_details[100];//variable to hold 100 book details
int user_count=0,books_count=0;
int choice;
//this loop runs until 7 is entered
do{
cout <<"MENU ";
cout << "1. Add a new book ";
cout << "2. Add a new user ";
cout << "3. Borrow a book ";
cout << "4. Return a book ";
cout << "5. Search a book ";
cout << "6. Displays users and number of books they have ";
cout << "7. Exit ";
cout << "Enter your choice : ";
cin >> choice;
switch(choice){
//Adds a new book in to the database and increase book count by 1
case 1:
cout<<"Bookname : ";
cin >> book_details[books_count].title;
cout <<"Bookcode : ";
cin >> book_details[books_count].code;
book_details[books_count].status = 1;
books_count++;
break;
case 2:
//adds a new user details in to database
cout << "Enter UserId : ";
cin >> tempId;
//flag variable to check whether user exists or not
flag=1;
for(i=0;i
if(tempId==user_details[i].id){
cout << "UserId already taken";
flag=0;
break;
}
}
//if user is not taken creating user id and increase user_count by 1
if(flag){
user_details[user_count].id=tempId;
cout << "Enter Username : ";
cin >> user_details[user_count].name;
user_details[user_count].booksNumber=0;
user_count++;
}
break;
case 3:
cout << "Please enter your userId : ";
cin >> tempId;
flag = 0;
//check whether user exists or not using flag variable
for(i=0;i
if(user_details[i].id==tempId){
flag = 1;
break;
}
}
//if user exists then print his name and check fof the requested book
if(flag){
cout << "Welcome " << user_details[i].name << " ";
//if user has taken less than 5 books
if(user_details[i].booksNumber<=5){
cout << "Please enter book code you want to borrow : ";
cin >> tempCode;
for(j=0;j
if(book_details[j].code==tempCode && book_details[j].status!=0){
book_details[j].status=0;
user_details[i].booksNumber++;
flag = 0;
break;
}
}
if(flag){
cout << "Book not found or it has been currently borrowed ";
}
}
else{
cout << "The user reaches the max. allowed books to borrow ";
}
}
else{
cout << "Username incorrect ;";
}
break;
case 4:
// to return the book issued to a user
cout << "Please enter book code :";
cin >> tempCode;
flag=0;
for(i=0;i
if(book_details[i].code==tempCode){
flag = 1;
break;
}
}
//once book is found then check for user id
if(flag){
if(book_details[i].status==0){
cout << "Enter user id ";
cin >> tempId;
flag = 0;
for(j=0;j
if(user_details[j].id==tempId){
flag =1;
break;
}
}
//if user and book details are found
if(flag){
user_details[j].booksNumber--;
book_details[i].status = 1;
}
else{
cout << "User not found ";
}
}
else{
cout << "Book hasn't been borrowed ";
}
}
else{
cout << "Invalid code ";
}
break;
case 5:
//Searching a book using bookcode
cout << "Enter book code : ";
cin >> tempCode;
for(i=0;i
if(book_details[i].code==tempCode){
cout << book_details[i].code <<"\t"<< book_details[i].title <<"\t"<< book_details[i].status << " ";
break;
}
}
break;
case 6:
//printing details of user using user_details structure
cout << "UserID UserName BorrowerNo ";
for(i=0;i
cout << user_details[i].id <<"\t"<< user_details[i].name <<"\t"<< user_details[i].booksNumber<<" ";
}
cout << " ";
break;
case 7:
cout << "ThankYou";
break;
//if user input doesnot match with any of the case then ask him to enter data again
default:
cout << "Please enter valid input : ";
}
}while(choice!=7);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started