繁体   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