0

I want to create a program that asks the user to input values.

The program will ask the user if they want to add more information but when the user wants to add another piece of information the output displays:

First-Question: Answer
Question: Answer
Second-Question:
Question: Answer

Error

This is my code

#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<vector>

using namespace std;

void IssueBooks() {

    vector<string> bookID;
    vector<string> bookTitle;
    vector<string> bookAuthor;
    vector<string> bookIssuer;

    string ID;
    string  Title, Author, Issuer;
    char question;

    //get bookID
    cout << "Enter book ID: "<<endl;
    getline(cin, ID);
    bookID.push_back(ID);

    //get bookTitle
    cout << "Enter book title: "<<endl;
    getline(cin, Title);
    bookTitle.push_back(Title);

    //get bookAuthor
    cout << "Enter book author: "<<endl;
    getline(cin, Author);
    bookAuthor.push_back(Author);

    //get bookIssuer
    cout << "Enter Issuer Name: "<<endl;
    getline(cin, Issuer);
    bookIssuer.push_back(Issuer);

    ofstream out("IssueRecord.txt");

    for (int j = bookID.size() - 1 ; j >= 0 ; j--) {
        out << bookID[j] << " " << bookTitle[j] << " " << bookAuthor[j] 
<< " " << bookIssuer[j]<<endl;
    }

    cout << "Do you want to add more books? <Y/N>" << endl;
    cin >> question;

    switch(question) {
    case 'Y' :
        IssueBooks();
        break;
    case 'y' :
        IssueBooks();
        break;

    case 'N' :
        break;
    case 'n' :
        break;

    default :
        cout << "Invalid input, try again !" << endl;
    }
}

int main() {
    IssueBooks();

   return 0;
}

Thank you

R Sahu
  • 196,807
  • 13
  • 136
  • 247
  • Are you trying to say that an empty string isn't valid input? Then check it – UKMonkey Sep 20 '17 at 16:29
  • 1
    not related to the question, but I don't see a good reason to write this recursively rather than adding a loop inside the main function – msrd0 Sep 20 '17 at 16:30
  • no, at the first time it will be question: answer question: answer but at second time if user want to add another information it will be question: question: answer i want the output like the first – MOHAMAD AZIMUDDIN Sep 20 '17 at 16:31
  • 1
    Read the duplicate question. If you're using formatted extraction (which you *are* with you y/n prompt), your input stream continues to harbor the trailing newline you entered upon hitting thumping on your keyboard following your y/n choice. Consuming the input stream through `'\n'` should handle that, and the duplicate question shows ways to do that. – WhozCraig Sep 20 '17 at 16:44

0 Answers0