Output Formatting Question

Greetings!

I am nearly finished with a program that I am writing for a class, and can't for the life of me figure out why the output of this function isn't displayed properly. This function should display up to 10 terms per line before beginning a new line. However, the output seems to be random. Can anyone tell me why this is displaying incorrectly? Please see code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
void ulamFunc(int first, int last, int inc)
{
	int num;
	int count1;
	int count2;
	
	count1 = 1;
	count2 = 1;
	
	do
	{
		cout << "Series generated for " << first << endl;
		
		cout << setw(7) << first;
		
		num = first;
		
		do
		{	
			if (num % EVEN == ZERO)
			{
				num /= EVEN;
				
				count1++;
				count2++;
			}
			else if (num % EVEN == LOW)
			{
				num = num * ODD + LOW;
				
				count1++;
				count2++;
			}
			
			cout << setw(7) << num;
			
			if (count1 == TEN)
			{
				cout << endl;
				
				count1 /= count1;
			}
		}
		while (num > LOW);
		
		cout << endl;
		cout << "Total number of terms: " << count2 << endl << endl;
		
		count2 /= count2;
		first = first + inc;
	}
	while (first <= last);
	
	return;
}


Note* All of the generated values are correct, the only issue is with the way the output is displayed.
Last edited on
Could you give an example of the 'random' output?

Also, what's with those weird EVEN, ZERO, LOW, ODD, TEN macros? That's not a good way to write code.
The weirdness is due to my professor generally not accepting "hard coded" values. So, I made global constants for the values that I needed.

As for a brief example of the output I'm getting:

Series generated for 100
    100     50     25     76     38     19     58     29     88     44
     22     11     34     17     52     26     13     40     20
     10      5     16      8      4      2      1
Total number of terms: 26

Series generated for 300
       300       150       75 
       226       113 ... continues for a total of 9 terms on this line.

When it should be:

Series generated for 100
    100     50     25     76     38     19     58     29     88     44
     22     11     34     17     52     26     13     40     20     10
      5     16      8      4      2      1
Total number of terms: 26

Series generated for 300
    300    150     75    226    113 ... continues for a total of 10 terms on this line.


Hopefully that doesn't raise further questions!
Last edited on
I typed out the comment above to show how it's all formatted, but it looks like most of that was ignored when I submitted the comment.
You can edit your post and wrap it all between [output] tags ;)
Edited. Thanks! :)
I was able to fix the formatting issue by changing around the order of a couple pieces of code, as well as a change to the last if statement.

Before:
1
2
3
4
5
6
7
8
cout << setw(7) << num;
			
if (count1 == TEN)
{
     cout << endl;
				
     count1 /= count1;
}


After:
1
2
3
4
5
6
7
8
if (count1 == ELEVEN)
{
     cout << endl;
				
     count1 /= count1;
}
		
cout << setw(7) << num;


Still not sure why it has to be 11 instead of 10, but it finally displays correctly!
count1 /= count1; This sets count1 to 1 instead of 0, so of course you have to add one to 10 to get 11.
Last edited on
Topic archived. No new replies allowed.