简体   繁体   English

c++ 有没有办法根据用户想要的输入数量输出多个输入

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

i'm currently working on a homework that involves if and else , and wondering if it's possible to make a line per line output based on the number of input the user one我目前正在做一项涉及 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++;
   }}

like based on the code i want to make a line by line output only from mark number to mark status (which is from the if statements) and do the input all the same time.就像基于代码,我想逐行输出仅从标记号到标记状态(来自 if 语句)并同时进行输入。

like for example i wanted an output like例如,我想要一个类似的输出

your mark for "subject name" is "mark".你的“主题名称”标记是“标记”。 "mark statement". “标记声明”。 your mark for "subject name" is "mark".你的“主题名称”标记是“标记”。 "mark statement". “标记声明”。

So, is there a way to save the 3 data and make a line by line sentences?那么,有没有办法保存3个数据并逐行造句?

There are a couple ways to do this:有几种方法可以做到这一点:

Tuples元组

these allow you to store multiple pieces of related data together under one object, similar to an array, except that the data types for each variable do not have to be of the same type.这些允许您将多个相关数据一起存储在一个对象下,类似于数组,除了每个变量的数据类型不必是相同的类型。

Structs结构

Structs are the same as classes, except that all their members are public by default.结构与类相同,只是它们的所有成员默认都是公共的。 You could create a struct that houses those specific data types.您可以创建一个包含这些特定数据类型的结构。

regardless of the route you decide to take, you will need to store them in an std::vector .无论您决定采取何种路线,您都需要将它们存储在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