C++使用二维数组的程序,该数组要求用户输入两次.它需要将这些输入显示为输出



我需要程序接受两次询问的两个输入并显示两次。如果用户输入名称 Ben,然后输入 bday 11111995,然后用户输入 Daisy,bday 11111994,则需要将这些显示为用户 #1 名称:Ben,用户 #1 bday:11111995,然后用户 #2 名称:Daisy,用户 #2 bday:11111994。如果用户可以将他们的生日输入为 11/11/1994 并以这种方式显示,这将有所帮助。我一会儿再回来检查。

#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;
}

使用结构来包含数据:

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);
}

让我们分解这个问题。首先 - 编写一个函数来向用户询问信息。然后编写一个函数来显示信息。通过分解,它们会更干净,更容易理解。

向用户询问信息:

由于我们需要一个名字和一个生日,我们可以将其作为 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;
}

打印信息:

我们只是将这对作为输入,并打印出来:

void printInfo(std::pair<std::string, int> info) {
    auto name = info.first;
    auto birthday = info.second;
    std::cout << "Name: " << name << "n";
    // Prints it all as YYYYMMDD
    std::cout << "Birthday: << birthday << "n";
}

使用函数

现在,我们可以要求两个用户,并打印它们:

int main() {
    auto user1 = getUserInfo(); 
    auto user2 = getUserInfo();
    std::cout << "User 1:n";
    printInfo(user1);
    std::cout << "User 2:n";
    printInfo(user2); 
}

使用生日类改进代码

现在我们已经完成了这部分,我们可以通过创建一个类来存储生日来改进代码。这将使允许用户以他们想要的格式输入生日变得更加容易。

class Birthday {
    int bday;
   public:
    // Uses the first two digits of bday
    int getDay() {
        return bday % 100;
    }
    // Uses the next two digits of bday
    int getMonth() {
        return (bday / 100) % 100;
    }
    // Uses the remaining digits of bday
    int getYear() {
        return bday / 10000; 
    }
    void setBday(int year, int month, int day) {
        bday = year * 10000 + month * 100 + day;
    }
};

现在,我们可以编写函数来打印生日:

// Prints it as MM-DD-YYYY
std::ostream& operator<<(std::ostream& stream, Birthday b) {
    stream << b.getMonth() << '/' << b.getDay() << '/' << b.getYear();
    return stream;
}

并阅读它:

// 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;
}

因为我们添加了这些函数,所以我们所要做的就是修改getUserInfo()printInfo()以使用Birthday而不是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";
}

最新更新