Debugging code

The base of this code was provided by my professor and he simply said to debug the program. I have made many changes already so this is not the original code provided but what I currently have left to fix. Any help/explanation would be appreciated.

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
56
57
58
59
60
61
62
63
// Diamond.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
	cout << "Enter size of  Diamond:  ";

	int size;
	cin >> size;

	int z = 1;
	for (int i = 0; i <= size; i++)
	{
		for (int j = size; j > i; j--);
		{
			cout << " "; // printing space here
		}

		cout << "*";  // printing asterisk here

		if (i>0)
		{
			for (int k = 1; k <= z; k++)
			{
				cout << "*";
			}
			z += 2;
			cout << "*";
		}
		cout << endl; // end line similar  to \n
	}

	z -= 4;

	for (int l = 0; i <= size - 1; i++)
	{
		for (int j = 0 j <= i; j++)
		{
			cout << " ";
		}

		cout << "*";

		for (int k = 1; k <= z; k++)
		{
			cout << "*";
		}
		z -= 2;

		if (i != size - 1);
		{
			cout << " ";
		}
		cout << endl << endl;
	}


	return 0;
}
What does the program do that you wish it did not, or what does it not do that you wish it did?
I just emailed my professor since he unfortunately did not specify. I am not sure if it is my job to figure out what the program is supposed to accomplish but I feel like it should have been stated, otherwise all the students could turn in a drastically different program that accomplishes different things. I will let you know what he replies as soon as I find out. Thank you for your reply.
Hello Peruvianpepper,

It does not matter what the program does or even if it does anything.

When I compiled the program I received 27 errors. All are fixable and should allow the program to run.

The question is what errors are you getting and which ones do you not understand?

I would say this is an assignment in debugging to get use to error messages and how to find the errors.

So, If there is an error that you do not understand just ask.

Hope that helps,

Andy
Hello Peruvianpepper,

When I said I received 27 errors do not let that scare you. It took only two things to fix all those errors.

BTW the program does work some what.

Andy
I am running this code through visual studio and get many syntax errors that look like "missing ')' before '<=' ". But then when I add the character that it says I am missing, that doesn't fix the problem, the red error underlining the words remains in place. I also do not seem to understand what it means by "i/j is unidentified". When I google those error messages, I get hundreds of possible reasons as to why the program might say that. Again, thank you everyone for your time.
Hello Peruvianpepper,

I too us VS. You will find that the line number listed is not always where the error is. Some times a missing ; on one line will affect many lines after the problem or even the rest of the program. Even a problem in a for loop in what is between the (), oops I should not have said that, can, as we see, cause many errors that have nothing to do with the original problem.

There are only two problems that need to be corrected and they are not easy to find. That is why you have to know how to write an if statement, while loop, do/while loop and for loops to spot some of these problems.

If I remember correctly one missing item caused 26 errors and each line and problem listed was not actually a problem because the error was above where the 26 errors started.

I see why this is a useful exercise in debugging.

Hope that helps,

Andy
My professor emailed my back today to and said that the program's output should look like:
*
***
*****
*******
*********
*******
*****
***
*


Something like this or a "hollow" version of this where its just the outline.
(when writing this response the diamond looks entirely different than what it does after its posted. It should have 4 corners, not just 3 like it does when this is posted.)
Last edited on
It should have 4 corners, not just 3 like it does when this is posted


To post intended output, wrap your output in [output][/output] tags. For example, the input
[output]    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
[/output]

Produces
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

See: http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Topic archived. No new replies allowed.