Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
   #include<iostream>
#include<stdio.h>
#include<cstring>
#include<string>
using namespace std;
void printBook(struct Books book);
void printBorrower(struct borrower borrow);

struct Books
{
	char title[50];
	char author[50];
	char subject[50];
	int book_id;
};
struct borrower
{
	char name[50];
	char course[50];
	int year;
	int id;
};
int main()
{

 Books b1;
 borrower person;
	char opt;

do
{
	cout<<"Enter opt: ";
	cin>>opt;
	if(opt=='A')
	{
	cout<<"Enter Book title: ";
	cin.getline(b1.title,50);
	cout << "Enter Book author: ";
	cin.getline(b1.author,50);
	cout << "Enter Book subject: ";
	cin.getline(b1.subject,50);
	cout << "Enter Book ID: ";
	cin >> b1.book_id;
	}
	else if(opt=='B')
		{
	cout<<"Enter the borrower's name: ";
	cin.getline(person.name, 50);
	cout<<"Enter the borrower's course: ";
	cin.getline(person.course, 50);
	cout<<"Enter the borrower's year level: ";
	cin>>person.year;
	cout<<"Enter the borrower's ID : ";
	cin>>person.id;
		}
	else
		{
		printBook(b1);
		
	//Print Borrower
		printBorrower(person);
		}
}while(opt!='Y');
system("pause");
}

void printBook(struct Books book)
{
	cout<<"Book title: "<<book.title<<endl;
	cout<<"Book author: "<<book.author<<endl;
	cout<<"Book subject: "<<book.subject<<endl;
	cout<<"Book ID : "<<book.book_id<<endl;
}

void printBorrower(struct borrower borrow)
{
	cout<<"Borrower's name: "<<borrow.name<<endl;
	cout<<"Borrower's course: "<<borrow.course<<endl;
	cout<<"Borrower's year: "<<borrow.year<<endl;
	cout<<"Borrower's ID : "<<borrow.id<<endl;
}      


What I have tried:

I tried executing the code but then I cant seem to enter the first statement in every option, If I enter the option A, it will only show the "Enter book title: Enter book author: " and I cant seem to enter anything for the 1st line, same goes to the option B, is something wrong with my code? Is there a limit as to how many cin and cout statements inside a single selection? Please help me. Thank you.
Posted
Updated 26-Jan-17 5:24am
Comments
[no name] 26-Jan-17 10:43am    
You might want to try clearing your input buffer before trying to get input from the user.
jeron1 26-Jan-17 10:50am    
Try adding a
cin.get();
call after the entry of opt.

1 solution

Using cin>> followed by cin.getline() can lead you to believe getline() is missing things:
Tips and Tricks for Using C++ I/O (input/output)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900