如何為多個輸入分配一個整體變量?

用戶17114856

我是 C++ 的新手。我正面臨一個問題。我想要 2 個不同的日期 (DD/MM/YYYY) 如何為第一個塊分配一個整體變量,為第二個塊分配另一個變量?

例如:

First Day of date: 2
First Month ""   : 5
First year ""    : 1985
-------
Second ""
second ""
second ""
--------

如果兩者是相同的日期,則輸出將是“兩者都相等”,否則將提到更大的“日期......更大”

我提前感謝您的幫助。

#include <iostream>
using namespace std;


int main()
{
    //This should be my first chunk
    
    int first_date;
    cout << "first day: ";
    cin >> first_y;


    int first_month;
    cout << "first month: ";
    cin >> first_month;


    int first_year;
    cout << "first year: ";
    cin >> first_year;  
}
林蛙

下面的方案顯示你想要什麼:

#include <iostream>

struct Date
{
    //always always initialize built in type in block/local scope
    int date = 0, month = 0, year = 0; // by default public 
    
    
    //default constructor 
    Date() = default;
    
    //lets overload operator= for comparing two Date types as you desire
    friend bool operator==(const Date &lhs, const Date &rhs);
    friend bool operator!=(const Date &lhs, const Date &rhs);
    
    //overload operator>> for taking input from user 
    friend std::istream& operator>>(std::istream &is, Date &rhs);
};
bool operator==(const Date &lhs, const Date &rhs)
{
    return lhs.date == rhs.date &&
           lhs.month == rhs.month &&
           lhs.year == rhs.year;
}
bool operator!=(const Date &lhs, const Date &rhs)
{
    return !(lhs == rhs);
}
std::istream& operator>>(std::istream &is, Date &rhs)
{
    std::cout<<"Enter date: ";
    //take date as input from user
    std::cin >> rhs.date;
    
    std::cout<<"Enter month: ";
    //take month as input from user
    std::cin >> rhs.month;
    
    std::cout<<"Enter year: ";
    //take year as input from user
    std::cin >> rhs.year;
    
    //check if input succedded
    if(!is)
    {
        rhs = Date();
    }
    return is;
}
int main() 
{
    
    //create first Date 
    struct Date d1;
    
    std::cin >> d1;//take input from user
    
    //create second Date 
    struct Date d2;
    
    std::cin >> d2;//take input from user
    
    //lets check if dates d1 and d2 entered by user are equal or not
    std::cout<<"The dates d1 and d2 are: "<<(d1==d2? "equal": "not equal")<<std::endl;
    
    
    
    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python pandas 為多個輸出變量生成一個表

如何將多個變量重構為一個帶參數的變量?

不能為 3 個變量分配多個值

Terraform 使用輸出變量作為另一個資源的輸入

如何將多個變量分配給固定大小的“表”輸出

在python中將多個變量作為一個變量傳遞

為多個變量創建一個條件 OR 的新變量

如何將多個列表從一個變量轉換為一個列表 (odoov11)?

如何在 Golang 的多個變量中存儲/複製單個用戶輸入?

獲得另一個變量輸入後變量重置

如何在一行中輸入 2 個變量並在輸出中計算它們?

如何在另一個函數中使用提示的輸出作為全局變量

檢查多個變量 angular/ts 中的一個值是否為假

如何檢查多個變量的相同條件並為每個變量返回匹配消息?

如何在 R at nce 中將 tibble 的多個變量突變為相同的值

Postman 響應體變量是一個 ip 地址

如何將一個變量的值分配給另一個變量的名稱?[JavaScript, Vue.js]

如何在一個變量中存儲多行?C# 實體框架

如果它們的數量由輸入而不是由變量決定,我如何加載然後打印多個數字

為單個變量分配不同的生命週期

給同一個變量多個值

std::visit 如何處理多個變體?

如何將“z”存儲為包含多個值的變量?

如何為多個變量多次運行腳本?

如何將多個參數作為變量傳遞給應用程序?

在 Bash 中將一個變量分配給另一個變量

如何通過單擊另一個輸入為輸入添加值

如何使用環境變量從一個階段到下一個階段的輸出?

如何運行一個接受多個用戶完整輸入的 bash 腳本,作為 dockerfile 的一部分