Unitialized Local Variables

I am working on a code that draws 4 triangles and keep getting the following warnings:

Warning 2 warning C4700: uninitialized local variable 'column' used c:\documents and settings\regina\my documents\visual studio 2008\projects\project 5 assign 4 file 2\project 5 assign 4 file 2\tuckerr_program5assign4_trianglesfile2.cpp 52

Warning 1 warning C4700: uninitialized local variable 'row' used c:\documents and settings\regina\my documents\visual studio 2008\projects\project 5 assign 4 file 2\project 5 assign 4 file 2\tuckerr_program5assign4_trianglesfile2.cpp 24

Warning 3 warning C4700: uninitialized local variable 'space' used c:\documents and settings\regina\my documents\visual studio 2008\projects\project 5 assign 4 file 2\project 5 assign 4 file 2\tuckerr_program5assign4_trianglesfile2.cpp 66



Here is the code


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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
using std::cout; 
using std::endl; 

// include definition of class Triangles from Triangles.h
#include "Triangles.h"

// function to draw triangles
void Triangles::drawTriangles()
{
   // Variable declarations (no other variables are needed)
   int row; // the row position
   int column; // the column position
   int space; // number of spaces to print

   // first triangle
   for (int column = 0; column <=10; column++)  
   {
	 for (int column = 0; column <= row; row++)
	   {
		   cout << "*";
	   }
   cout << endl;

   } // end for;



   // second triangle
   for (int row=10; row >= 1; row++)
   {
	   for ( int column=1; column <=row-1; column++)

	   {
		   cout << "*";
	   }
	   cout << endl;

   } // end for;




   // third triangle
   for (int row = 9; row < 10; row++)
   {
	   for (int space = 0; column <= row -1; column++)
	   {
		   cout << "*";
	   }
	   {
		   cout << " ";
	   }
	   cout << endl;

   } //end for;



   // fourth triangle
   for (int row = 10; space <= row -1; row++)
   {
	   for (int space = 9; space <= row - 1; row++)
	   {
		   cout << " ";
	   }
	   {
		   for (int column=10; column>= row; row++);
	   }
	   {
		   cout << "*";
	   }
	   {
		   cout << endl;
	   }

   } // end for;

   exit(0); // function ends successfully



} // end function drawTriangles



I didn't look over this fully, but you need to take a close look at your loops:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
   int row; // the row position
   int column; // the column position   <----------------  HERE
   int space; // number of spaces to print

   // first triangle
   for (int column = 0; column <=10; column++)   // <----------------  HERE
   {
	 for (int column = 0; column <= row; row++)  //  <--------------- HERE AGAIN
	   {
		   cout << "*";
	   }
   cout << endl;

   } // end for; 


Every time you do "int column" you're creating a new variable named column. Note that you're creating three separate variables named "column" in the above code. This probably was not your intent. Since you're declaring all of your variables up front at the top of your function, don't put them in every for loop:

1
2
3
int column;

for(column = 0; ... ) // <--- notice, just "column = 0", not "int colunn = 0" 


Also, on the HERE AGAIN line, you're doing the following:

 
 for (int column = 0; column <= row; row++)


it looks like you're counting column, but you're actually counting row. You probably meant to be counting row? I'm not exactly sure what it is you're trying to do here, but this wouldn't work.
Topic archived. No new replies allowed.