C++ while 循环在自定义文件结构中

C++ while loop in a custom file structure

本文关键字:文件结构 自定义 while 循环 C++      更新时间:2023-10-16

我正在尝试创建一个名为"Players"的文件,一个简单的a.txt文件,我使用的代码如下:-

int main()
{
    ofstream theFile("Players.txt");
    cout << " Enters players ID., Name and Money" << endl;
    cout << " press Ctrl+z to quitn" << endl;
    int idNumber;
    string name;
    int money;
    while (cin >> idNumber **>>** name >>   money) {
                 //       ^^^^^^^^          this
        theFile << idNumber << " " **<<** name << " " << money << endl;
                 //               ^^^^^^^^  and this
        }
    system("pause");
    return 0;
}

在"while"行和"theFile"行上 - 标有**>>**的>>不断抛出错误"没有运算符'<<'和'>>'支持这些操作数" - 尝试了不同的语法组合但没有成功 - 用户输入他们的idNumber,名称和金钱 while 循环的目的是允许用户根据需要进行任意数量的输入,然后输入 Ctrl+z 关闭文件。

似乎运算符 "<<" 和 ">>" 不支持前后的操作数 "**"。

如果您有正确的标头,则可以编译上面的代码。

#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
..... etc.
}