Just one word!

Pages: 123
I'm not getting any output I tried that function as int then I changed it to void.
I tried to use the i in main to set the size of the array to it.

1
2
3
4
5
// to fill string array
	 fillString(fin, inputarray, size, wordCount);
	
	
	 arrSize = wordCount;


Then I replaced the size in the functions that took the array "inputarray" with arrSize. I got nothing. hmm
It is used as

wordCount = fillString( fin, inputarray, size );

Now function onlyWords may be removed, because we already have gotten the number of words after executing fillString.
VLAD! it wordked but I have one more issue left. I'm supposed to get 10 words per line I got 11 on the first line.
Try the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Function to print words ten to a line
void printTen( const string inputarray[], const int size )
{
	const int 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.
Last edited on
Why is mine not working thought I got this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/Function to print words ten to a line
void printTen(string inputarray[],const int 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] << ' ';
		}
		counter++ ;
		if(counter % 10 == 0 && counter != 0)
		{
		 fout << inputarray[i] << endl;
		}
	
	}	
}
	
I think that it is due to statement

counter++ ;

that is placed between two if statements.

By the way condition ( counter % 10 != 0 || counter == 0) is negation of condition (counter % 10 == 0 && counter != 0)
That is !(counter % 10 == 0 && counter != 0) is equivalent to
( counter % 10 != 0 || counter == 0) and vice versa.

So you could write

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/Function to print words ten to a line
void printTen(string inputarray[],const int 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.



Last edited on
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..

I think I'm done with this discussion.
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. :)
Last edited on
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' ;
}
Last edited on
For this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 if(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. :)
The simplest realization of the function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void printTen( ostream &out, const string inputarray[], int size )
{

	const int 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 ).
Last edited on
Why did you add that vlad? So that would just be my = to my ofstream &fin?

Cire would the counter be up by one so at 10 it will be 11?

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

printTen( std::cout, inputarray, wordCount );

The same will be outputed in the file.
Last edited on
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

By the way the code can be simplified

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void printTen( ostream &out, const string inputarray[], int size )
{

	const int 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
Topic archived. No new replies allowed.
Pages: 123