473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Formatting output text file

Sheepman
My quest, to put 100 random numbers in ten rows of ten. Then output the same to the screen and a file. My screen output is working,yeah! Text file, not so much. The data is getting there but not formated. If word wrap is off it's a single row. If word wraps on, well ... it wraps. I've considered sizing the notebook window to wrap at the tenth character but I don't think the TA working on his Phd will buy it. Help please! My code for this portion is below:

Expand|Select|Wrap|Line Numbers
  1. void tenByTen(int firstArray[])
  2. {
  3. char yourFileName [80];
  4. int d1, d2;
  5. //Prompt user for file name to store array
  6. cout << "Enter name of file to store your randomly generated numbers: " << endl;
  7. cin >> yourFileName;
  8. ofstream outfile (yourFileName, ios::out);//create and open file
  9. if (!yourFileName)//errorcheck
  10. {
  11.     cout << "Error opening your file " << endl;
  12. }//close error check
  13. //fomat array output to 10 x 10
  14.  
  15. for (d1 = 0; d1 <= 9; d1++)
  16. {
  17.     for (d2 = 0; d2 <= 9; d2++)
  18.     {
  19.      cout << setw(5) << firstArray[(d1 * 10) + d2];//write to screen
  20.      outfile << setw(4) << firstArray[(d1 * 10) + d2];//write to file     
  21.     }//close nested for
  22.     cout << endl;
  23. }//close for
  24. outfile.close();
  25. }//close tenBy
  26.  
Jul 10 '07 #1
4 3505
scruggsy
147 100+
Look how you are sending a message to the screen to tell it to start a new line after every 10 numbers:
Expand|Select|Wrap|Line Numbers
  1. cout << endl;
It works, right?
All you need to do is send a similar message to your file at the same time, telling it to start a new line as well.
Jul 10 '07 #2
mohsin
19
Look how you are sending a message to the screen to tell it to start a new line after every 10 numbers:
Expand|Select|Wrap|Line Numbers
  1. cout << endl;
It works, right?
All you need to do is send a similar message to your file at the same time, telling it to start a new line as well.
look all u need to so is to write
outfile<<endl;
after cout<<endl;


given below
for (d1 = 0; d1 <= 9; d1++)
{
for (d2 = 0; d2 <= 9; d2++)
{
cout << setw(5) << firstArray[(d1 * 10) + d2];//write to screen
outfile << setw(4) << firstArray[(d1 * 10) + d2];//write to file
}//close nested for
cout << endl;
outfile<<endl;
}//close for
outfile.close();
}//close tenBy
Jul 10 '07 #3
Look how you are sending a message to the screen to tell it to start a new line after every 10 numbers:
Expand|Select|Wrap|Line Numbers
  1. cout << endl;
It works, right?
All you need to do is send a similar message to your file at the same time, telling it to start a new line as well.
I spent 4 or 5 hours trying to figure this out. I kept using
Expand|Select|Wrap|Line Numbers
  1. cout<<endl;
instead of
Expand|Select|Wrap|Line Numbers
  1. outfile<<endl;
. I've only been doing this 4.5 weeks. It's the little things that really seem to kill me.
Jul 10 '07 #4
look all u need to so is to write
outfile<<endl;
after cout<<endl;
Don't I look silly!

My book doesn't say anything about this. I kept trying to use
Expand|Select|Wrap|Line Numbers
  1. cout<<endl;
in every combination imaginable. Inside the brackets, outside the brackets, commenting out the write to screen portion. I'm not to clear on how to debug yet. So I don't know how to use it to my advantage, to see at what point the program stops outputting my expected results.
Jul 10 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Tom Petersen | last post by:
I am using a response.write to test the formatting of the output. I am supposed to get this: BEGIN:VCALENDAR VERSION:1.0 BEGIN:VEVENT DTSTART:20051022T090000Z DTEND:20051022T090000Z...
6
by: Tom Petersen | last post by:
Here is a little more info, sorry should have explained what my final goal was. I am creating a .vcs file from a form to import into Outlook. I was just testing the output on screen then pasting...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
1
by: vbnetrookie | last post by:
Hi all, This is my first batch file and I want to query a database and output it in a textfile. Up to now that works, the only problem is the formatting in the text file. It's all screewed...
1
by: Riko Eksteen | last post by:
Hi I'm reading an xml file into an XmlDocument, adding some nodes, and writing it back out. I would like the nodes I add to assume the same level of indeting as the rest of the document. (I load...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
6
by: shoo | last post by:
Any one know how to do this? thank Write a simple text-formatting program that produces neatly printed output from input text containing embedded command lines that determine how to format the...
2
by: Ken Wilson | last post by:
I am writing and .xml file and am not getting the formatting I would like. The portion of the code that is giving me problems is as follows; XmlTextWriter tw = new XmlTextWriter(filename); ...
6
by: Glen | last post by:
Hello again, I don't blame anyone for not answering my last post, since I obviously hadn't spent much time researching, but I've come a little ways and have another question. How can I better...
10
by: sara | last post by:
Hi - I have a report that is 14 columnar sub-reports (Line up: Position- holders in each of our 14 locations - Manager, Assistant Manager, Receiving, Office, etc). I output directly to PDF...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.