Hello, I am doing a file input/output. I wrote 3 fulls names, phone number, balance. etc inside my input file.

Example:

John Doe 555-555-5555 5000.00


In my output file, I am trying to create this output with using setw.

First Name Last Name Phone Number Balance
------------ ------------ ----------------- ---------
John Doe 555-555-5555 5000.00
John Doe 555-555-5555 5000.00


I don't think ^^ will come out correctly on a forum post, but I want the firstname,lastname,phonenumber, and balance to appear underneath each other in a setw format which will look nice and neat. The results I'm getting is that the first name will look nice and neat, but the second name displayed underneath will be spaced out differently.

I don't know if my code is wrong, but I have tried for several hours rearranging code etc. Any help would be appreciated. Thanks

#include <iostream>
#include <fstream>
#include <iomanip>


using namespace std;

int main ()
{
    ifstream inData;
    ofstream outData;
    
    outData<<fixed<<showpoint;
    outData.precision(2);
    
    inData.open ("C++program.txt");
    outData.open ("C++program_output.txt");
    
    string firstname,lastname,phonenumber;
    double balance;
    
    outData<<"Call list for Customers who owe more than $1000"<<endl;
    outData<<endl;
    outData<<setw(1)<<"First Name"<<setw(16)<<"  Last Name  "<<setw(10)<<"   Phone Number  "<<setw(12)<<"     Balance  "<<setw(10)<<endl;
    outData<<setw(1)<<"----------"<<setw(16)<<"  ---------  "<<setw(10)<<"   ------------  "<<setw(12)<<"     -------  "<<setw(10)<<endl;  
    
    
    
    while (inData)
{ 
           
    inData>>firstname>>lastname>>phonenumber>>balance; 
    
    if ( balance > 1000 )
    {  
                   
    outData<<setw(1)<<firstname<<setw(18)<<lastname<<setw(18)<<phonenumber<<" $ "<<setw(5)<<setw(11)<<balance<<endl;   
    
    } 
   
    inData>>firstname>>lastname>>phonenumber>>balance;
     
}
      
    inData.close();
    outData.close();
    
    system ("PAUSE");
    return 0;
    
}

Hello, I am doing a file input/output. I wrote 3 fulls names, phone number, balance. etc inside my input file.

Example:

John Doe 555-555-5555 5000.00


In my output file, I am trying to create this output with using setw.

First Name Last Name Phone Number Balance
------------ ------------ ----------------- ---------
John Doe 555-555-5555 5000.00
John Doe 555-555-5555 5000.00


I don't think ^^ will come out correctly on a forum post, but I want the firstname,lastname,phonenumber, and balance to appear underneath each other in a setw format which will look nice and neat. The results I'm getting is that the first name will look nice and neat, but the second name displayed underneath will be spaced out differently.

I don't know if my code is wrong, but I have tried for several hours rearranging code etc. Any help would be appreciated. Thanks

Even though the output isn't code, code tags will preserve the spacing so I'd use them for the output:

First Name        Last Name     Phone Number       Balance
------------      ------------     -----------------       ---------
John                   Doe          555-555-5555        5000.00
John                   Doe          555-555-5555        5000.00

I see the dollar sign in the code, but not above. I cut and pasted directly from your post, so is that the way it looks (above - if not, please show what yours looks like) and you want it to look like this?

First Name        Last Name        Phone Number            Balance
------------      ------------     -----------------       ---------
John              Doe              555-555-5555            $ 5000.00
John              Doe              555-555-5555            $ 5000.00

But everything else in the program works?

I want it to look like the first copy/paste that you did, so it will look nice and neat (and centered) in each column. My results come out right because the names that appear is what i want to appear. it was three names total, but with the results, 2 of the names will come out (which i want). It is just the spacing is what is messing me up! Mine will come out like:


John-----Doe------Phone Number-------Balance


John---- Doe---Phone Number-----Balance

I want it to look like the first copy/paste that you did, so it will look nice and neat (and centered) in each column. My results come out right because the names that appear is what i want to appear. it was three names total, but with the results, 2 of the names will come out (which i want). It is just the spacing is what is messing me up! Mine will come out like:


John-----Doe------Phone Number-------Balance


John---- Doe---Phone Number-----Balance

So something like this (everything centered along the columns) is what you want?

Bob     Hernandez   $  100.00
Richard     Smith     $ 50000.00
 Keith     Sanders    $   6.00

And your first line comes out correctly (and by first line you are referring to the name rows not to the header rows or the dashes, right), but all subsequent lines have bad spacing? So this is a good line:

John-----Doe------Phone Number-------Balance

and this is a bad line?

John---- Doe---Phone Number-----Balance

Also, what is the input file? Is it the two John Doe lines? You said it had three lines, not two, but only two display, but you don't want the last line to display? Am i getting that right?

[EDIT] Forgot the phone numbers in my example, but those are all the same width. [/EDIT]

Bob     Hernandez   $  100.00
Richard     Smith     $ 50000.00
 Keith     Sanders    $   6.00

^^ That is how I want it to look!


but how it appears with mine is the...

Firstname------LastName----PhoneNumber----Balance
Firstname------LastName--PhoneNumber---Balance


The 2 names are uneven going under each other, so it will look like this with your example.

Bob-----------Hernandez-----555-555-5555----$-----100.00
Richard---------Smith-----555-555-5555----$-----100.00


So it will mess up where the Phone number and balance take place!


If I do it without the setw.... the name will appear has

-------------BobSmith555-555-5555$100.00
RichardSmith555-555-5555$100.00

Bob Hernandez $ 100.00
Richard Smith $ 50000.00
Keith Sanders $ 6.00


^^ That is how I want it to look!

Just want to verify since without code tags or the hyphens that you put in, all the spacing is stripped out. So you want it to look like this:

Bob     Hernandez   $  100.00
Richard     Smith     $ 50000.00
 Keith     Sanders    $   6.00

or like this:

Bob Hernandez $ 100.00
Richard Smith $ 50000.00
Keith Sanders $ 6.00

which is what it looked like in your post. Again, i want to clarify whether you WANT it to be centered, as I had in my example, or you WANT it have a single space between, as it is in your last post (i.e. did you post without the hyphens and code tags on purpose since there is to be only one space or was it an accident and there is spacing that was accidentally stripped out due to the lack of code tags/hypens?). It looks like you WANT the first of my two examples in this post, not the second.

Also note that not only is the spacing stripped out without code tags, but the font size is not constant, so it is best to use code tags when displaying spacing issues. The hyphens help, but still retain the problem of uneven spacing in font size.

Here's a hint, people. Use a fixed width font to set up spacing for your output. The post editor doesn't use a fixed width font, so open up Notepad or something equivalent and get your example looking right before you post it. Otherwise you'll end up with too many or too few spaces in the code display because you got it to "look right" with a proportional font.

My apologizes.

I posted an attachment of how I want the output to look like in .txt. My bad for making things complicated!

Attachments
Call list for customers who owe more than $1000

First Name   Last Name   Phone Number       Balance 
----------   ---------   ------------       -------  
John            Doe      555-555-5555   $   2500.75
John            Doe      555-555-5555   $   3205.99

Here is another attachment of what I am recieving which is NOT what I want!

Attachments
Call list for customers who owe more than $1000

First Name     Last Name    Phone Number   Balance
----------     ---------    ------------   -------
John              Doe      555-555-555   2500.75
John             Doe      555-555-5555   3205.99

Ah, good. Desired output:

Call list for customers who owe more than $1000

First Name   Last Name   Phone Number       Balance 
----------   ---------   ------------       -------  
John            Doe      555-555-5555   $   2500.75
John            Doe      555-555-5555   $   3205.99

Actual output:

Call list for customers who owe more than $1000

First Name     Last Name    Phone Number   Balance
----------     ---------    ------------   -------
John              Doe      555-555-555   2500.75
John             Doe      555-555-5555   3205.99

So it is a little skewed and missing the dollar sign. I'll try looking at/running the code you posted. So the first name is not supposed to centered?

The first name can be centered or all the way to the left (it's an option). I didn't add dollar sign because I didn't want to make things more complicated, but I want the $ sign to appear like I presented in the output though.

As long as the output is neat and centered underneath each other, I am good to go!

The first name can be centered or all the way to the left (it's an option). I didn't add dollar sign because I didn't want to make things more complicated, but I want the $ sign to appear like I presented in the output though.

As long as the output is neat and centered underneath each other, I am good to go!

I don't get different output for the same line like you did. Here's my input and output:

Input

John              Doe      555-555-5555   2500.75
John              Doe      555-555-5555   2500.75
John              Doe      555-555-5555   2500.75
John              Doe      555-555-5555   2500.75
John              Doe      555-555-5555   2500.75
John              Doe      555-555-5555   2500.75
John              Doe      555-555-5555   2500.75
John              Doe      555-555-5555   2500.75
Phillip           Hanover  555-555-5555   3205.99
Steven            Jackson  555-555-5555   3205.99
Bo                Diddley  555-555-5555   3205.99
William           Sully    555-555-5555   4512.12 
Jim               Sully    555-555-5555   4512.12 
Steve             Sully    555-555-5555   4512.12 
Jeff              Sully    555-555-5555   4512.12

Output

Call list for Customers who owe more than $1000

First Name     Last Name     Phone Number       Balance  
----------     ---------     ------------       -------  
John               Doe      555-555-5555 $     2500.75
John               Doe      555-555-5555 $     2500.75
John               Doe      555-555-5555 $     2500.75
John               Doe      555-555-5555 $     2500.75
Phillip           Hanover      555-555-5555 $     3205.99
Bo           Diddley      555-555-5555 $     3205.99
Jim             Sully      555-555-5555 $     4512.12
Jeff             Sully      555-555-5555 $     4512.12

The same line of input results in the same line of output. As you can see, it's skipping every other line. That's because you have this line:

inData>>firstname>>lastname>>phonenumber>>balance;

twice. Delete one of them since you are simply reading data in, then reading it in again over the same variables and thus losing it before displaying it.

I don't see any attempt to center the names in your code. Sure, you can count the number of characters in a line, then figure out a width that will make it centered, but that'll only work for names that have the same length if you put it directly in the code as you have. I'm not too familiar with iomanip. There might be a "center" option in addition to "right" and "left". I would use the same "setw" values in the data output code as you do for the headers. You'll either have to find a "center" option, or you can pad the front of the name with various numbers of spaces to make things center based on the length of the name (shorter name = more spaces in front of it).

The first and last name spaces out fine. It is just the phone number and balance that won't level up evenly. Any advice? Help! =(

The first and last name spaces out fine. It is just the phone number and balance that won't level up evenly. Any advice? Help! =(

How is the output below "spacing out fine" for the names?

Call list for Customers who owe more than $1000

First Name     Last Name     Phone Number       Balance  
----------     ---------     ------------       -------  
John               Doe      555-555-5555 $     2500.75
John               Doe      555-555-5555 $     2500.75
John               Doe      555-555-5555 $     2500.75
John               Doe      555-555-5555 $     2500.75
Phillip           Hanover      555-555-5555 $     3205.99
Bo           Diddley      555-555-5555 $     3205.99
Jim             Sully      555-555-5555 $     4512.12
Jeff             Sully      555-555-5555 $     4512.12

If all you care about is the columns showing up underneath the dashes and you don't care about centering, just make the setw argument the same for the data as you do for the column headers and dash output. I thought you wanted it centered. If you do, read my post above. Unless there is a "center" option in iomanip or somewhere else, pad the data with spaces till it is centered. Regardless, make the setw arguments the same for the data as for the headers and the data will end up underneath the headers.

I finally figured out of the problem after several hours of messing around with setw. I actually have no clue how I figured it out, but atleast I aligned it up the way I want to! Thanks for all the help Vernon. I appreciate it.

This question has already been answered. Start a new discussion instead.