954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with reading and writing to output files

Im brand new to C++ & Im having trouble with this program im writing i got all the way to the end of it working im just having problems using the setw function in reading and outputting from a file. I have my code attatched for what mine looks like, input file, and another screen shot for what it should look like. If you can give me any pointers in where i went wrong id appreciate it. Thanks.

Attachments Mine.cpp (3.94KB) input.txt (0.24KB) what_it_should_look_like_Screenshot.doc (45.5KB)
hgill006
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

youll get more responses if you post the actual code instead of the file people are to skeptical download anything in the first place and most people don't want to download source code that can easily fit on the webpage :P

Anarionist
Junior Poster in Training
61 posts since Jan 2010
Reputation Points: 11
Solved Threads: 2
 

First of all instead of writing out all those numbers on top i would put it in a loop by using a variable increment that variable and so forth Also to create a book of * use \n and . Make your life a lot easier. Don't have to type as much :). Easy to reuse you code.

Instead of using all those char arrays use string and getline() which should allow you to put in a delimiter to stop at.


setw() sets the field width of the next item only. So I don't think you should be having problems. Maybe you could explain the problem.

#include <iostream>

using namespace std;

int main()
{
	int j;
	for(int i=0; i<=5; i++)
	{
		for(j=0; j<=9; j++)
		{
			cout<<j;
		}
			j=0;
	}

	return 0;
}

Do something like this with the remainder of the *. Make your code look less clutter and easier to read :).

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

thanks for the for loop idea i implemented that, but basically for the getline i want to use that and the string just read like 20 spaces including blankspace after the word if thats possible. Because right now my input file has names of different sizes, so when i go to setw the next word they end up not equal in spaceing like:

Student name: Total Points: Program Avg:
Snow White 600 82.00
Sleeping Beauty 590 81.00

and i cant setw different on each line because it's in a loop.

hgill006
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

use getline till end of line using \n and then you can create substrings with delimeters.
maybe use getline() in conjunction with stream operators such as >> since you want to read in integers or numbers.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: