blionzion blionzion - 3 months ago 11
C++ Question

Why am i not getting the correct output?

I'm new to C++ and having trouble with the io. The output is omitting the line Account Number: accountNumber and I have no idea why.

here is my display function.

void displayColumnTitle()
{
int accountNumber, balanceDue;
string name;

cout << "Enter Account Number(-1 to terminate the input):" << endl;
cin >> accountNumber;
cout << "Enter name: ";
cin >> name;
cout << "Enter balance due:" << endl;
cin >> balanceDue;

cout << "Account Number: " + accountNumber << endl
<< "Name: " + name << endl << "MONTH BALANCE DUE" << endl;
}

Answer
"Account Number: " + accountNumber

does not concatenate accountNumber to the string. Instead performs a pointer arithmetic operation. If accountNumber is 10,

"Account Number: " + accountNumber

is equivalent to

"mber: "

I bet that's not what you expected.

Instead of

cout << "Account Number: " + accountNumber << endl
    << "Name: " + name << endl << "MONTH BALANCE DUE" << endl;

Use

//                         ||
//                         vv  Use << not +
cout << "Account Number: " << accountNumber << endl
    << "Name: " << name << endl << "MONTH BALANCE DUE" << endl;
//              ^^  Use << not +
//              ||