簡體   English   中英

c++ 有沒有辦法根據用戶想要的輸入數量輸出多個輸入

[英]c++ Is there a way to output the multiple input based on the number of input the user wants

我目前正在做一項涉及 if 和 else 的作業,想知道是否可以根據用戶輸入的數量對每行輸出一行

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    string name ,ID ,subjectname;
    int no1, no2, no3 ,no4, mark; 

    no3 = 1;
    cout << "    Welcome to Management And Science University ";
    cout << "\n";
    cout << "\nEnter your name - ";
    cin >> name;
    cout << "Enter your ID Number - ";
    cin >> ID;
    cout << "Enter your number of subject - ";
    cin >> no2;

    while (no3 <= no2)
   {
       cout << "\nEnter your subject name - ";
       cin >> subjectname;
       cout << "Enter your mark - ";
       cin >> mark;

       cout << "\n your mark for " << subjectname << " is " << mark;

       if (mark < 40)
       cout << "\nSee you in next semester";

        else if (mark < 60)
        cout << "\nTry Harder";

        else if (mark < 70 )
        cout << "\nAverage Performance";

        else if (mark < 80)
        cout << "\nGood!";

        else if (mark >=80)
        cout <<"\nExcellent!";

        else
        cout << "\nYour input is wrong";

        no3++;
   }}

就像基於代碼,我想逐行輸出僅從標記號到標記狀態(來自 if 語句)並同時進行輸入。

例如,我想要一個類似的輸出

你的“主題名稱”標記是“標記”。 “標記聲明”。 你的“主題名稱”標記是“標記”。 “標記聲明”。

那么,有沒有辦法保存3個數據並逐行造句?

有幾種方法可以做到這一點:

元組

這些允許您將多個相關數據一起存儲在一個對象下,類似於數組,除了每個變量的數據類型不必是相同的類型。

結構

結構與類相同,只是它們的所有成員默認都是公共的。 您可以創建一個包含這些特定數據類型的結構。

無論您決定采取何種路線,您都需要將它們存儲在std::vector

struct subject
{
    std::string name;
    std::string ID;
    std::string subject_name;

    int mark;
};

int main()
{

    // this is also an option
    // std::tuple<std::string, std::string, std::string, int> subject;
    // std::vector<std::tuple<std::string, std::string, std::string, int>> subject_list;

    std::vector<subject> subject_list;

    // rest of your program
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM