1

I am building a citation machine for books. However, I am facing a problem with the citing.Here is my code:

#include <iostream>
using namespace std;
string nameofbook,lauthor,x,fmauthor,year,publisher,citystate;
int main(){
cout<<"Welcome to the citation machine. What do you want to cite?"<<endl;
cin>>x;
if(x == "book"){
cout<<"What is the name of the book?"<<endl;
cin>>nameofbook;
cout<<"What is the last name of the author?"<<endl;
cin>>lauthor;
cout<<"What is the first and middle name of the author in abbreviations?"<<endl;
cin>>fmauthor;
cout<<"Which year is the book published?"<<endl;
cin>>year;
cout<<"Who is the publisher?"<<endl;
cin>>publisher;
cout<<"Which state or city is the publisher located at?"<<endl;
cin>>citystate;
cout<<"You are done!"<<endl;
cout<<"The citation is"<<endl;
cout<<lauthor;
cout<<" ";
cout<<fmauthor;
cout<<" ";
cout<<"(";
cout<<year;
cout<<")" ;
cout<<" ";
cout<<nameofbook;
cout<<" ";
cout<<citystate;
cout<<":";
cout<<" ";
cout<<publisher;

}


return 0; 
}

When I compile and run this in Dev C++, It gives me this:

Welcome to the citation machine. What do you want to cite?
book
What is the name of the book?
Catching Fire
What is the last name of the author?
What is the first and middle name of the author in abbreviations?
Collins S.
Which year is the book published?
Who is the publisher?
Scholastic Corporation
Which state or city is the publisher located at?
You are done!
The citation is
Fire Collins (S.) Catching Corporation: Scholastic
--------------------------------
Process exited after 88.09 seconds with return value 0
Press any key to continue . . .

When I let all details be a single word, it becomes:

Welcome to the citation machine. What do you want to cite?
book
What is the name of the book?
Mockingjay
What is the last name of the author?
Collins
What is the first and middle name of the author in abbreviations?
S.
Which year is the book published?
2009
Who is the publisher?
Scholastic
Which state or city is the publisher located at?
NYC
You are done!
The citation is
Collins S. (2009) Mockingjay NYC: Scholastic
--------------------------------
Process exited after 72.02 seconds with return value 0
Press any key to continue . . .

So when all the details are of a single word, it works perfectly. But when any detail becomes more than one word, it takes the first word to answer the question it was supposed to answer and the second word to answer the next question. So how am I supposed to make both words answer the question it was supposed to answer? I would greatly appreciate if anybody can help me to solve this problem.

YWH
  • 35
  • 4

3 Answers3

4

Instead of doing

cin >> nameofbook;

try

getline(cin, nameofbook);

and similarly for the rest. When you just do cin >> x it will read only up to the next whitespace. If you want to read the whole line you should do getline.

grigor
  • 1,584
  • 1
  • 13
  • 25
4

You may use getline() instead of cin as it gets a token from string.

 cout<<"What is the first and middle name of the author in abbreviations?"<<endl;

 getline (std::cin, fmauthor); // and as for other qsns
Riad
  • 3,822
  • 5
  • 28
  • 39
0

You require to make an input using std::getline(cin,stringName) function which accepts whole line as input until you press a new line character .Getting a string input using cin would break input on occurence of first space character.