简体   繁体   中英

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

#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.

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?

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 .

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM