-2

I am facing a problem while doing a chart. I would want to output the chart in a same row without changing the code and without making it horizontal. I would wish to use for loop to solve this problem because I can iterate over everything because I have the same elements.


The code is displayed below:

# include <iostream>
using namespace std;

class InterestCalculator
{
protected:
    float principal_amount = 320.8;
    float interest_rate = 60.7;
    float interest = interest_rate/100 * principal_amount; 
public:
    void printInterest()
    {
    cout<<"Principal Amount: RM "<<principal_amount<<endl;
    cout<<"Interest Rate(%): "<<interest_rate<<endl;
    cout<<"Interest: RM"<<interest<<endl;
    }
};

class LoanCalculator : public InterestCalculator
{
private:
    int loan_term;
    int month;
    float month_payment;
public:

void displayVariable()
{
    cout<<"Enter loan amount (RM): ";
    cin>>principal_amount;
    cout<<"\n\nEnter annual interest rate(%): ";
    cin>>interest_rate;
    interest_rate = interest_rate / 100;
    cout<<"\n\nEnter loan term in years: ";
    cin>>loan_term;
    month = loan_term*12;
    month_payment = (principal_amount*interest_rate + principal_amount) / month;
    cout<<endl<<endl;

}

 void outputStatistics()
 {
      cout<<"Month\tPayment(RM)\tPrincipal(RM)\tInterest(RM)\tBalance(RM)\n";
      for(int i = 1; i <=month; i++)
      {
          cout<<i<<endl;
      }

      for(int j = 0; j <=month; j++)
      {
          cout<<"\t"<<month_payment<<endl;
      }
 }
 };

 int main()
{
    LoanCalculator obj;
    obj.displayVariable();
    obj.outputStatistics();
    return 0;
}

The output of the abovementioned code:

Enter loan amount (RM): 120


Enter annual interest rate(%): 1.2


Enter loan term in years: 1


Month   Payment(RM)     Principal(RM)   Interest(RM)    Balance(RM)
1
2
3
4
5
6
7
8
9
10
11
12  
    10.12
    10.12
    10.12
    10.12
    10.12
    10.12
    10.12
    10.12
    10.12
    10.12
    10.12
    10.12
    10.12

Process returned 0 (0x0)   execution time : 3.940 s
Press any key to continue.

The desired output:

Enter loan amount (RM): 120


Enter annual interest rate(%): 1.2


Enter loan term in years: 1


Month   Payment(RM)     Principal(RM)   Interest(RM)    Balance(RM)
1       10.12
2       10.12
3       10.12
4       10.12
5       10.12
6       10.12
7       10.12
8       10.12
9       10.12
10      10.12
11      10.12
12      10.12

Process returned 0 (0x0)   execution time : 3.940 s
Press any key to continue.
  • 4
    If you have the same number of elements (months), you can iterate over all the data with just one for loop and output it the way you want. – YesThatIsMyName Jun 27 '22 at 06:26
  • 2
    For screen output, you could use a library like ncurses to manipulate the cursor position. But it's better to change your program so that output goe into an array of strings or something similar, which can be manipulated again and again, and output them when finished. Even better is creating an array of objects which resemble the columns in your output lines. – Erich Kitzmueller Jun 27 '22 at 06:27
  • What you need is [`fmt`](https://github.com/fmtlib/fmt) or output row by row instead of column by column. – Louis Go Jun 27 '22 at 06:30

2 Answers2

5

You don't need to undo endl you just need to reorganise your code so that you do things in the correct order in the first place, like this

void outputStatistics()
{
    cout<<"Month\tPayment(RM)\tPrincipal(RM)\tInterest(RM)\tBalance(RM)\n";
    for(int i = 1; i <=month; i++)
    {
        // output one row at a time
        cout<<i<<"\t"<<month_payment<<endl;
    }
}

This code outputs one row at a time, your code outputted one column first and the next column second.

john
  • 85,011
  • 4
  • 57
  • 81
  • 2
    What if there are multiple columns? Will it affect the code? – CPP_is_no_STANDARD Jun 27 '22 at 06:39
  • 3
    @CPP_is_no_STANDARD Yes of course. But the solution is the same, you should write your code in such a way that you can output one row of data at a time with an `endl` at the end of each row. – john Jun 27 '22 at 06:41
  • Also instead of `endl` you should use `\n`. https://stackoverflow.com/questions/213907/stdendl-vs-n – CompuChip Jun 30 '22 at 08:13
1

Another option is to use setw manipulator but the better option is to use \t for this column based chart.

The setw is more flexible because you could change the spaces number.

But the \t will space 4 spaces every time you use it, that's why I mentioned that the \t is for this column based chart.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 02 '22 at 01:20