//Function to print words ten to a line
void printTen( const string inputarray[], constint size )
{
constint N = 10;
fout << "The file sorted alphabetically: " << endl;
// This prints ten words per line
for( int i = 0 ; i < size; i++ )
{
if ( i % ( N + 1 ) == N ) fout << endl;
fout << inputarray[i] << ' ';
}
fout << endl;
}
You should call it as
printTen( inputarray, wordCount );
Though it would be much better if it would have one more parameter of type output stream.
/Function to print words ten to a line
void printTen(string inputarray[],constint size)
{
fout << "The file sorted alphabetically: " << endl;
int counter = 0;
// This prints ten words per line
for(int i = 0 ; i < size; i++)
{
if( counter % 10 != 0 || counter == 0)
{
fout << inputarray[i] << ' ';
}
else
{
fout << inputarray[i] << endl;
}
counter++ ;
}
}
The problem is that you shall also place the new line character after the last line.
actually when I compiled the program it did the same thing it had 11 words in the first line. I hope that this is actually with the print function and not something with my damn array again. Which I have suspicion might be. I'm not sure thought I thought that we solved that.
So this really makes 0 sense
Thanks for the code vlad
I had a case insensitive comparison in the original function. Also my sort function appears to not differ except for the case insensitive comparison to the sort function you
directed me to.
Your original sort function differed quite a bit in function if not in appearance. But, because you can't see the difference (or, failing that, note the correct alphabetical order of words in the new output versus the old output which certainly would suggest some functional difference in the code other than case insensitivity.)
You tell me it's wrong my sort function and you give no evidence.
You had all the evidence you needed. Your print function prints them in the order they occur. Your sort function puts them in that order. The order was wrong. Ergo, problem is in the sort function.
Except tell me to do something that is already there this is why I think you are messing with me.
I told you to do something that you were not currently doing. The fact that you had been doing it before on the former sort code is irrelevant.
But what is the evidence for you thinking that the sort is off.
You were supplied that evidence at the time I told you..
OK dude if you have it figured out then what is the solution? Because as of now I"m getting 11 words on the first line and the rest are all lines of correct size. If you so say it is in the sort then what is the solution to the problem?
Because saying it doesn't really help unless you have a suggestion on what to look at. I cannot see the issue in it. :)
OK dude if you have it figured out then what is the solution? Because as of now I"m getting 11 words on the first line and the rest are all lines of correct size. If you so say it is in the sort then what is the solution to the problem?
That issue is not related to the sort issue. That issue is clearly related to the printing issue. You had 11 words in the original, but one of the words happened to be empty so it wasn't obvious.
The reason you have 11 words in the first line is the same reason that you require || count == 0 in the last snippet you posted.
It really doesn't take that much effort to trace through the logic of the code. Sit your ass down, grab a pen and a piece of paper. Work out what count will be on the 10th time through the loop. Hint: it won't be 10.
1 2 3 4 5 6 7 8 9 10 11 12 13
void printTen(const string inputarray[], int size)
{
fout << "The file sorted alphabetically: " << endl;
// This prints ten words per line
int i ;
for(i = 0 ; i < size; i++)
fout << inputarray[i] << ((i+1)%10 ? '' : '\n') ;
// terminate with a newline.
if ( i % 10 )
fout << '\n' ;
}
fout << "The file sorted alphabetically: " << endl;
int counter = 0;
// This prints ten words per line
for(int i = 0 ; i < size; i++)
{
if( counter % 10 != 0 || counter == 0)
{
fout << inputarray[i] << ' ';
}
elseif(counter % 10 == 0 && counter != 0)
{
fout << inputarray[i] << endl;
}
counter++ ;
}
What is this you posted?
1 2 3 4 5 6 7 8
void printTen(const string inputarray[], int size)
{
fout << "The file sorted alphabetically: " << endl;
// This prints ten words per line
for(int i = 0 ; i < size; i++)
fout << inputarray[i] << ((i+1)%10 ? '' : '\n') ;
}
And maybe it is not hard for you but keep in that in mind smarty pants. :)
void printTen( ostream &out, const string inputarray[], int size )
{
constint N = 10;
out << "The file sorted alphabetically: " << endl;
int i = 0;
for ( ; i < size ; i++ )
{
out << inputarray[i];
if ( i % N == N - 1 ) out << endl;
else out << ' ';
}
if ( i % N != 0 ) out << endl;
}
Take into account that I added one parameter (the first one ).
I added one parameter that you could test the function passing to it either std::cout or your file stream. For example to see what will be outputed on the console you can call it as
Vlad when you program how do you do it. Do you work things out with a pencil and paper first? Or no? I'm trying to think of ways to make the programming process easier on myself
Thanks
void printTen( ostream &out, const string inputarray[], int size )
{
constint N = 10;
out << "The file sorted alphabetically: " << endl;
int i = 0;
for ( ; i < size ; i++ )
{
out << inputarray[i];
if ( i % N == N - 1 || i == size - 1 ) out << endl;
else out << ' ';
}
}
Usually I write code here immediately on the screen and only after that I think whether it is correct.:)
Yeah I figured that out after I was going back over the logic of it. I was like whats up with this if statement. Thanks though dude. And it must be nice. I spend 5 hours looking at one function. ha