如何存储多个用户输入,然后以C++显示它们
How to store multiple user inputs and then display them all in C++
我是 c++ 的新手,我需要使用我的函数来存储多个用户输入,然后打印多个用户输入。但是,我的代码只执行一个用户输入。我应该将其存储在数组中吗?它是不言自明的代码,因此您可以跳到底部并查找"??存储不变??">
#include <iostream>
using namespace std;
// Will display all bids
void displayBid(string &bidTitle, string &fundPerson, string &vehicleId, float &bidAmount)
{
cout << "Title: " << bidTitle << endl;
cout << "Fund: " << fundPerson << endl;
cout << "Vehicle: " << vehicleId << endl;
cout << "Bid Amount: " << bidAmount << endl;
}
// Ask the user for title, person, vehicleId, amount
void getBid(string &bidTitle, string &fundPerson, string &vehicleId, float &bidAmount)
{
cout << "Enter Title: ";
cin.ignore();
getline(cin, bidTitle);
cout << "Enter Fund";
cin.ignore();
getline(cin, fundPerson);
cout << "Enter Vehicle ID: ";
cin.ignore();
getline(cin, vehicleId);
cout << "Enter Bid Amount: ";
cin.ignore();
cin >> bidAmount;
}
// Loops through adding bids or displaying bids
main(void)
{
string title, person, id;
title = "";
person = "";
id = "";
float amount;
amount = 0.0;
int choice = 0;
while (choice != 9) {
cout << "Menu:" << endl;
cout << " 1. Enter Bid" << endl;
cout << " 2. Display Bid" << endl;
cout << " 9. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
??storeinvariable?? = getBid(title, person, id, amount);
break;
case 2:
// display variable??
displayBid(title, person, id, amount);
break;
}
}
cout << "Good bye." << endl;
return 1;
}
你可以为此使用数组。但是,它必须包含两种不同的数据类型。您可以制作一个void*
s 的数组或向量,但这不是最好的解决方案。
您可以创建自己的类型(可能是struct
(来存储用户输入的数据,如下所示:
typedef struct {
std::string bidTitle;
std::string fundPerson;
std::string vehicleId;
float bidAmount;
} Bid;
一旦你定义了struct
,你就可以让你的函数使用这种数据类型。
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- boost::asio如何生成多个协同程序,然后加入它们
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 在std::thread中,joinable()然后join()线程安全吗
- C++:如何读取分离变量,然后读取向量
- 为什么我的递归函数按降序打印,然后按升序打印?
- 等待整个 omp 块完成,然后再调用第二个函数
- CMake:如何将库 A 链接到库 B,然后将可执行文件链接到库 A
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 如何使变量从 x 到 y,然后从 y 返回到 x 并始终重复该过程
- 我有一个数组,我想输入一个范围,然后找到范围内所有偶数的总和?
- 如何正确地推回然后遍历堆中对象的向量?
- 我似乎无法为指针分配一个数组,然后更改数组的内容
- 如何在字符串中找到字符,然后在C++中提取其余的字符串
- 首先按给定顺序打印所有数字,然后使用 Array 打印所有字符和其他符号
- 将加密消息从 php 发送到 C++ 应用程序,然后使用 CryptoPP 进行解密
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?