Home > Software design >  How can I add horizontal space between two functions in C ?
How can I add horizontal space between two functions in C ?

Time:11-15

So I want to output a list of the names and randomly generated numbers. I already did everything else it is just that I do not know how to output it the way I want it to.

I want it to output like this:

ID #:       Names: 
1            bob
23           rob
44          kanye

Here is what I have so far:

cout << "Would you like to view the archived names and IDs? (Y/N)" << endl;
        string archiveInput;
        cin >> archiveInput;
        if(tolower(archiveInput[0]) == 'y')
        {
            cout << "ID #:    Names: " << endl;
            
            output(ids, names);
            
        }

Here are my functions I used.

void output(const vector<int>& ids)
{
    for(int i = 0; i < ids.size(); i  )
    {
        cout << ids[i] << endl;
    }
    cout << endl;
}

void output(const vector<string>& names)
{
    for(int i = 0; i < names.size(); i  )
    {
        cout << names[i] << endl; //might have to use endl for list format
    }
    cout << endl;
}

void output(const vector<int>& ids, const vector<string>& names)
{
    cout << output(ids) << "       " << output(names); //I thought this would work, im new :(
}

CodePudding user response:

Try to using this method

#include <iostream>
#include <iomanip>

using namespace std;

class Student
{
public:
    string studentName;
    int studentAge;
    int studentMarks;
    int admissionYear;

    Student(string name, int age, int marks, int year)
    {
        studentName = name;
        studentAge = age;
        studentMarks = marks;
        admissionYear = year;
    }
};

int main()
{
    Student studentArray[4] = {Student("Alex", 20, 80, 2018), Student("Bob", 21, 82, 2018), Student("Chandler", 23, 85, 2017), Student("Rose", 18, 89, 2019)};

    cout
        << left
        << setw(10)
        << "Name"
        << left
        << setw(5)
        << "Age"
        << left
        << setw(8)
        << "Marks"
        << left
        << setw(5)
        << "Year"
        << endl;

    for (int i = 0; i < 4; i  )
    {
        cout
            << left
            << setw(10)
            << studentArray[i].studentName
            << left
            << setw(5)
            << studentArray[i].studentAge
            << left
            << setw(8)
            << studentArray[i].studentMarks
            << left
            << setw(5)
            << studentArray[i].admissionYear
            << endl;
    }
    return 0;
}

It will print the below output :

Name      Age  Marks   Year
Alex      20   80      2018
Bob       21   82      2018
Chandler  23   85      2017
Rose      18   89      2019

We have set different widths for each column. The first column width is 10, the second column width is 5, the third column width is 8, and the last column width is 5.

The width is important here. If it is less than the size of its content, the content will overflow.

CodePudding user response:

cout << output(ids) << "       " << output(names);

First runs the function output overloaded with ids argument, and afterwards runs the version with the names like so:

for(int i = 0; i < ids.size(); i  )
{
    cout << ids[i] << endl;
}
cout << endl;
// returns from first function call
cout << "              ";
// enters second function call
for(int i = 0; i < names.size(); i  )
{
    cout << names[i] << endl; //might have to use endl for list format
}
cout << endl;

Is how the compiler will run it, which is why your output is underneath each other.

This code is what you want inside the overload with the two arguments:

for(int i = 0; i < ids.size(); i  )
    {
        cout << ids[i] << "           " << names[i] << endl;
    }
    cout << endl;

However your output will still not quite look like how you want. You have to apply other tricks for that.

This is the reason why your current code behave like that. Look at M Khaidar's answer for the correct way to solve your problem.

  • Related