Home > Back-end >  Can I "undo" endl?
Can I "undo" endl?

Time:06-28

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.


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;
}

And the output is:

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 output that I want:

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.

So is there any solution to this problem? I've searched for other solutions at stackoverflow but did not found one.

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related