为什么我的程序没有按正确的顺序显示输出?

时间:2017-09-20 16:27:27

标签: c++ cin cout

我想创建一个程序,要求用户输入值。

程序会询问用户是否要添加更多信息,但当用户想要添加另一条信息时,输出会显示:

第一个问题:答案
问题:答案
第二个问题:
问题:答案

Error

这是我的代码

#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;
}

谢谢

0 个答案:

没有答案