Thank you for taking the time to help me out with this problem. I am taking my first C++ class and I am struggling a little bit.
This is my homework assignment and I cant figure out how to get the format correct. I am able to get most of it correct but the 3 lines with the longer names are not coming out right.
I have used the tutorials on this site, google, my book, etc and i still cant figure it out.
I know that it is a really simple problem but any help would really be appreciated.
//block in main
ifstream fin;
ofstream fout; // step 2
fin.open("input3.txt");
fout.open("output3.txt"); // step 3
if(!fin){
cout<<"Input Failure\n";
system("pause");
return 1;
} // end of fin error check
if(!fout){
cout<<"Output Failure\n";
system("pause");
return 1;
} // end of fout error check
// end of step 3b EXECUTE AT THIS STAGE
string firstName,lastName;
float netBalance,avgdb,interest,apr,ccNumber,payment,d1,d2;
while(fin){ //fin controlled while loop
fin>>firstName;
fin>>lastName;
fin>>ccNumber;
fin>>netBalance;
fin>>payment;
fin>>d1;
fin>>d2;
avgdb=(netBalance*d1-payment*d2)/d1;
interest=avgdb*apr/(100*12);
if(avgdb<100)
{
apr=.05*100;
}
else if(avgdb > 100 && avgdb <1000)
{
apr=.10*100;
}
else if(avgdb>1000)
{
apr=.15*100;
}
cout<<left;
cout<<firstName<<" "<<setw(15)<<lastName;
cout<<setw(10)<<ccNumber;
cout<<right;
cout<<fixed<<showpoint<<setprecision(2);
cout<<setw(10)<<netBalance<<setw(10);
cout<<setw(10)<<apr<<setw(10)<<interest<<endl;
fout<<setw(5)<<left<<firstName<<setw(5)<<lastName<<setw(5);
fout<<setw(5)<<left<<ccNumber<<setw(5)<<netBalance<<setw(5);
fout<<setw(5)<<left<<apr<<setw(5)<<interest<<endl;
fout<<fixed<<showpoint<<setprecision(2);
if(fin.peek()=='\n')fin.ignore();
// peek ignores the value of the next byte in the stream
// without moving the input marker
// ignore discards the next byte in the stream
}// end of while loop
fin.close ();
fout.close ();
This is what i am getting:
Snow White 1234 100.10 5.00 0.00
Jack Frost 2345.00 500.90 10.00 1.58
Sleeping Beauty 3456.00 600.70 10.00 4.97
Prince Charming 6789.00 55.56 5.00 0.45
Rapunzel Repunzel 7890.00 350.07 10.00 1.21
Santa Claus 8901.00 5456.22 15.00 35.87
Miss Horner 9012.00 9800.23 15.00 121.46
This is what it is supposed to look like:
************************************************************
123456789012345678901234567890123456789012345678901234567890
************************************************************
FULL NAME CARD # BALANCE APR (%) INTEREST
------------------------------------------------------------
Snow White 1234 100.10 5.00 0.34
Jack Frost 2345 500.90 10.00 3.16
Sleeping Beauty 3456 600.70 10.00 4.97
Prince Charming 6789 55.56 5.00 0.22
Rapunzel Repunzel 7890 350.07 10.00 2.41
Santa Claus 8901 5456.22 15.00 53.81
Miss Horner 9012 9800.23 15.00 121.46
------------------------------------------------------------
Your problem you're having is the expected output has the first and last name combined as the full name and takes up a width of 20. You're first name, and last name, take up 5 each. That is about the only problem I see with your code however.