仅使用<iostream> #include <string> #include <cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <cstdio> #include <cerrno> #include <clocale> #include <ctime> #include <cwchar> #include <cwctype> #include <iomanip> #include <numeric> #include <algorithm> #include <unistd.h> #include <sys/wait.h> #include <conio.h>库实现控制台填表式输入
时间: 2025-05-29 13:59:52 浏览: 10
### 控制台表格输入的实现方法
为了在控制台上实现类似于“填表”的功能,可以利用C++的标准库来处理用户的逐行输入并将其存储在一个二维数据结构中。这里推荐使用 `std::vector<std::string>` 来保存每一行的数据条目。以下是具体实现方式:
#### 表格输入的核心逻辑
通过循环不断请求用户输入每列的内容,并将这些内容按照行的形式存入容器之中。当完成所有必要字段的信息采集后,则停止收集流程并将结果打印出来或者继续其他业务逻辑操作。
```cpp
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Function to display table header dynamically based on column names provided by user or predefined ones.
void showTableHeader(const vector<string>& headers) {
cout << "+-----------------------------+\n"; // Fixed width for simplicity
cout << "| ";
for (const auto& hdr : headers) {
cout << setw(15) << left << hdr.substr(0, 15) << " | "; // Limit each field length to 15 chars
}
cout << "\n+-----------------------------+\n";
}
// Collecting data row-by-row from console until sentinel value entered ('done').
vector<vector<string>> collectDataRows(int numCols) {
vector<vector<string>> rows;
string cellValue;
int rowIndex = 0;
while (true) {
vector<string> currentRow(numCols);
cout << "Enter values for Row #" << ++rowIndex << ": \n";
for (int col = 0; col < numCols; ++col) {
cout << "Column [" << col + 1 << "] Value: ";
getline(cin >> ws, cellValue); // Use getline with manipulator to handle spaces correctly
if ("done" == cellValue && !rows.empty()) {
return rows; // If 'done', exit collection only after at least one valid entry exists.
}
currentRow[col] = move(cellValue);
if (!cellValue.empty() && all_of(cellValue.begin(), cellValue.end(), ::isspace)) {
throw invalid_argument("Empty cells not allowed.");
}
}
rows.emplace_back(move(currentRow));
}
}
// Display collected data in tabular format similar to how it was inputted earlier.
void printDataTable(const vector<string>& headers, const vector<vector<string>>& body) {
showTableHeader(headers);
for (const auto& rowData : body) {
cout << "| ";
for (size_t i = 0; i < rowData.size(); ++i) {
cout << setw(15) << left << rowData[i].substr(0, 15) << " | ";
}
cout << endl;
}
cout << "+-----------------------------+" << endl;
}
int main() try {
const int NUM_COLUMNS = 3; // Example number of columns
cout << "Welcome! Let's create a simple table.\nSpecify three-column entries below:\n";
vector<string> columnNames {"Name", "Age", "Occupation"};
showTableHeader(columnNames);
auto dataTable = collectDataRows(NUM_COLUMNS);
printDataTable(columnNames, dataTable);
} catch (exception& e) {
cerr << "Error occurred during execution: " << e.what() << ". Exiting..." << endl;
return EXIT_FAILURE;
}
```
此代码片段展示了如何构建一个基本但灵活的命令行界面用于录入多行列信息[^3]。它首先定义了一些辅助函数用来展示头部和实际记录部分;接着提供了核心算法让用户能够反复提交新的项目直至他们决定终止会话为止。最后还加入了异常处理器以便更好地应对潜在错误状况的发生。
---
###
阅读全文
相关推荐









