I need help with my formatting

Hello Everyone,

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.





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    


     //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
------------------------------------------------------------

Last edited on
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.
Topic archived. No new replies allowed.