Formatting output text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sheepman
    New Member
    • Jun 2007
    • 24

    Formatting output text file

    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:

    Code:
    void tenByTen(int firstArray[])
    {
    char yourFileName [80];
    int d1, d2;
    //Prompt user for file name to store array
    cout << "Enter name of file to store your randomly generated numbers: " << endl;
    cin >> yourFileName;
    ofstream outfile (yourFileName, ios::out);//create and open file
    if (!yourFileName)//errorcheck
    {
    	cout << "Error opening your file " << endl;
    }//close error check
    //fomat array output to 10 x 10
     
    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;
    }//close for
    outfile.close();
    }//close tenBy
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Look how you are sending a message to the screen to tell it to start a new line after every 10 numbers:
    Code:
    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.

    Comment

    • mohsin
      New Member
      • Nov 2006
      • 19

      #3
      Originally posted by scruggsy
      Look how you are sending a message to the screen to tell it to start a new line after every 10 numbers:
      Code:
      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

      Comment

      • Sheepman
        New Member
        • Jun 2007
        • 24

        #4
        Originally posted by scruggsy
        Look how you are sending a message to the screen to tell it to start a new line after every 10 numbers:
        Code:
        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
        Code:
        cout<<endl;
        instead of
        Code:
        outfile<<endl;
        . I've only been doing this 4.5 weeks. It's the little things that really seem to kill me.

        Comment

        • Sheepman
          New Member
          • Jun 2007
          • 24

          #5
          Originally posted by mohsin
          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
          Code:
          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.

          Comment

          Working...