Home > Back-end >  Need Help Displaying Percentage Sign in C
Need Help Displaying Percentage Sign in C

Time:06-26

Essentially, I want the output to look like the first photo, but my output is in the 2nd (both attached). I attached my code snippet as well to show what I am trying.

Any help would be appreciated, I have solved all logical errors in this program and just need help formatting. Also, is there any better way to keep all of the inputs and calculated values on the right side, or do I just need to throw in the \t a couple times like I did? Thanks!

I want my code to look like this:

But keep getting this:

#include <iostream>
#include <cmath>

using namespace std;

int main() {

double loanAmount, interestRate, paymentsNum;

    //Stores the loan amount value into memory
    cout << "Loan Amount: \t\t\t$ ";
    cin >> loanAmount;

    //Stores the monthly interest rate value into memory
    cout << "Monthly Interest Rate: \t\t";
    cin >> interestRate; cout << '%' << endl;
    interestRate = interestRate / 100;

    //Stores the nuumber of payments value into memory
    cout << "Number of Payments: \t\t";
    cin >> paymentsNum;

    //Calculates the payment on a loan
    double payment = (interestRate * pow((1   interestRate), paymentsNum)) / (pow((1   interestRate), paymentsNum) - 1);

    //Calculates the monthly payment on a loan
    double monthlyPayment = payment * loanAmount;
    cout << "Monthly Payment: \t\t$ " << monthlyPayment << endl;

    //Calculates the total amount paid back from the original loan amount
    double paidBack = monthlyPayment * paymentsNum;
    cout << "Amount Paid Back: \t\t$ " << paidBack << endl;

    //Calculates the total interest paid on the loan
    double interestPaid = paidBack - loanAmount;
    cout << "Interest Paid: \t\t\t$ " << interestPaid << endl;

    return 0;
}

CodePudding user response:

cin >> interestRate;

At this point your program stops and waits for you to type a line of text following by the Enter key, which gets echoed back, the Enter key causes the cursor to move to the next line.

cout << '%' << endl;

Only after the cursor moves to the next line does this output take place. The fact that in the source file this statement is on the same line is immaterial. The output of the C program does not depend on how its source code is formatted, C does not work this way. C is whitespace-agnostic, a C program can be indented in any way and its spacing is completely arbitrary, without affecting its functionality.

It is not possible to get the behavior you want from a basic C program that uses basic C input and output, via std::cin and std::cout, in a portable manner. On an outside chance you can consult the documentation for your operating system, to determine if there are special terminal character sequences that can be used to move the cursor to the previous line and position it at a (guessed) column where the % would go. This would be specific not just to your operating system but also to whatever terminal program you are using.

CodePudding user response:

I figured out a workaround to this. To get my desired output I used:

cout << "Loan Amount: " << right << setw(20) << "$ ";
cin << loanAmount;

cout << "Monthly Interest Rate: " << setw(12) << "%\b\b";
cin >> interestRate;
interstRate = interestRate / 100;

Not sure if this is the best idea, but it worked. I'm not too worried about fixing this minor issue, but I ended up just stumbling upon this again while watching some videos online.

  • Related