C++ C++;使用二维数组向用户请求两次输入的程序。它需要将这些输入显示为输出
我需要程序获取两次请求的两个输入并显示两次。如果用户输入名称Ben,然后输入bday 11111 995,然后输入Daisy,bday 11111 994,则需要将其显示为user#1 name:Ben,user#1 bday:11111 995,然后user#2 name:Daisy,user#2 bday:11111 994。如果用户可以输入他们的生日为1994年11月11日,并以这种方式显示出来,这将有所帮助。我过一会儿再查C++ C++;使用二维数组向用户请求两次输入的程序。它需要将这些输入显示为输出,c++,C++,我需要程序获取两次请求的两个输入并显示两次。如果用户输入名称Ben,然后输入bday 11111 995,然后输入Daisy,bday 11111 994,则需要将其显示为user#1 name:Ben,user#1 bday:11111 995,然后user#2 name:Daisy,user#2 bday:11111 994。如果用户可以输入他们的生日为1994年11月11日,并以这种方式显示出来,这将有所帮助。我过一会儿再查 #include <iomanip> #includ
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
const int personname = 2;
const int personbday = 2;
double storagelabel[personname][personbday];
int name;
int bday;
string namelabel;
double bdaylabel;
int nameloop;
int loopint;
cout << "Enter the info asked of you. \n";
for (name = 0; name < personname; name++)
{
cout << "Name #" << (name + 1) << ": ";
cin >> namelabel;
for (bday = 1; bday < personbday; bday++)
{
cout << "Bday #" << (name + 1) << ": ";
cin >> bdaylabel;
}
cout << endl;
}
for (int nameloop = 0; nameloop < personname; nameloop++)
{
for (int loopint = 0; loopint < personbday; loopint++)
{
cout << personname << personbday << endl;
}
}
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
const int personname=2;
const int personbday=2;
双存储标签[personname][personbday];
int名称;
国际日;
字符串名称标签;
双标签;
int-nameloop;
int-loopint;
cout让我们来解决这个问题。首先-编写一个函数询问用户信息。然后编写一个函数显示信息。通过分解,它们将更清晰、更容易理解
向用户询问信息:
因为我们需要一个名字和生日,所以我们可以将其作为std::pair
返回
#include <tuple> // This is where std::pair is
#include <string>
#include <iostream>
std::pair<std::string, int> getUserInfo() {
std::pair<std::string, int> info;
std::cout << "Enter name: ";
std::cin >> info.first; // Read in the name
std::cout << "Enter birthday as a number: ";
int month, day, year;
char separator;
// Reads it as MM/DD/YYYY
std::cin >> month >> separator >> day >> separator >> year;
info.second = year * 10000 + month * 100 + day;
return info;
}
现在,我们可以编写函数来打印生日:
// Prints it as MM-DD-YYYY
std::ostream& operator<<(std::ostream& stream, Birthday b) {
stream << b.getMonth() << '/' << b.getDay() << '/' << b.getYear();
return stream;
}
因为我们添加了这些函数,所以我们所要做的就是修改getUserInfo()
和printInfo()
以使用birth
而不是int
:
std::pair<std::string, Birthday> getUserInfo() {
std::pair<std::string, Birthday> info;
std::cout << "Enter name: ";
std::cin >> info.first; // Read in the name
std::cout << "Enter birthday as a number: ";
std::cin >> info.second; // Read in the birthday
return info;
}
void printInfo(std::pair<std::string, Birthday> info) {
auto name = info.first;
auto birthday = info.second;
std::cout << "Name: " << name << "\n";
std::cout << "Birthday: << birthday << "\n";
}
std::pair getUserInfo(){
std::配对信息;
std::cout>info.first;//读入名称
std::cout>info.second;//读入生日
退货信息;
}
无效打印信息(标准::对信息){
自动名称=信息优先;
自动生日=信息秒;
std::cout使用结构来包含数据:
struct Person
{
std::string name;
std::string birthdate;
};
您可以添加提示用户输入数据的方法:
struct Person
{
//...
void ask_user_for_input();
};
void Person::ask_user_for_input()
{
std::cout << "Enter name: ";
std::getline(cin, name);
std::cout << "Enter birthdate: ";
std::getline(cin, birthdate);
}
struct-Person
{
//...
无效向用户请求输入();
};
void Person::向用户请求输入()
{
对不起,你的代码一点意义都没有。
// Reads it as MM-DD-YYYY
std::istream& operator>>(std::istream& stream, Birthday& b) {
char separator;
int year, month, day;
stream >> month >> separator >> day >> separator >> year;
b.setBday(year, month, day);
return stream;
}
std::pair<std::string, Birthday> getUserInfo() {
std::pair<std::string, Birthday> info;
std::cout << "Enter name: ";
std::cin >> info.first; // Read in the name
std::cout << "Enter birthday as a number: ";
std::cin >> info.second; // Read in the birthday
return info;
}
void printInfo(std::pair<std::string, Birthday> info) {
auto name = info.first;
auto birthday = info.second;
std::cout << "Name: " << name << "\n";
std::cout << "Birthday: << birthday << "\n";
}
struct Person
{
std::string name;
std::string birthdate;
};
struct Person
{
//...
void ask_user_for_input();
};
void Person::ask_user_for_input()
{
std::cout << "Enter name: ";
std::getline(cin, name);
std::cout << "Enter birthdate: ";
std::getline(cin, birthdate);
}
struct Person
{
//...
void print(int user_id) const;
};
void Person::print(int user_id) const
{
std::cout << "User #" << user_id << " name: " << name;
std::cout << ", User #" << user_id << " birthdate: " << birthdate;
std::cout << "\n";
}
std::vector<Person> database;
const unsigned int quantity(database.size());
for (unsigned int index = 0; index < quantity; ++index)
{
database[i].print(index + 1);
}