我将字符串写入文件的方式有什么问题?

时间:2014-02-13 01:04:32

标签: c++ file cin getline

以下代码不会为itemId输入内容并直接跳过项目描述。我不知道为什么我一直想要查看几个小时的东西并且没有运气。

void AddRecord(fstream &file, string name)
{
    file.open(name, ios::app);

    int  quanity;
    string itemId;
    string itemDes;
    float wholeCost, retailCost;

    cout << "What is the ID of this item? ";
    getline(cin, itemId);
    file << itemId << endl;\
    cin.clear();

    cout << "\nInsert an item description. ";
    getline(cin, itemDes);
    cin.clear();
    file << itemDes << endl;

    cout << "\nHow many do you have? ";
    cin >> quanity;
    file << setprecision(2) << quanity << endl;

    cout << "\nWhat is the whole sale cost? ";
    cin >> wholeCost;   
    while ( wholeCost > 57.14 )
    {
        cout << "\nWhat is the wholesale cost? ";
        cin >> wholeCost;
    }
    file << setprecision(2) << wholeCost << endl;

    cout << "\nWhat is the retail cost? ";
    cin >> retailCost;
    while ( retailCost > (1.75 * wholeCost) )
    {
        cout << "\nWhat is the retail cost? ";
        cin >> retailCost;
    }
    file << setprecision(2) << retailCost << endl << endl;

    file.close();
}

0 个答案:

没有答案