I am having a hard time with loops and nested if statements.

I'm working on an assignment thats been driving me crazy for a couple of days now, I don't want anyone to finish it for me I just hope someone will help me understand why I am so lost. The program should read several inputs then run a loop and print out several geometric shapes based on the input. Actually, the input is supposed to come from a text file I just wrote the program with the cout statements to help me visulaize what was going on. The problem I am having is that no matter what i try the only object the gets printed is the first one. here is a smaller version of my code with only two shapes:

#include <iostream>

using namespace std;
int main()
{
int numofobjects, length, height;
char shape, direction, draw;

cout << "Enter number of objects" << endl;
cin >> numofobjects;

for (int i=0; i<numofobjects; i++)
{
cout << "Enter the object, direction, length, and character "
<< "to be used to display object" << endl;
cin >> shape >> direction >> length >> draw;
if (shape == 'L' || 'l' && direction == 'H' || 'h')
{
cout << "Horizontal Line" << endl;
for (int i=1; i<=length; i++)
cout << draw;
cout << endl;
}
else if (shape == 'L' || 'l' && direction == 'V' || 'v')
{
cout << "Vertical Line" << endl;
for (int i=1; i<=length; i++)
{
cout << draw;
cout << endl;
}
}
}
return 0;
}


If the problem has to do with nested ifs, probably code tags and indentation would be nice.
Right now you have syntax errors in those if conditions. You need to fully quantify them. You can't say "if shape equals l or L". You must say "if shape equals l or shape equals L".
thanks I feel really stupid, that was all that was wrong... shouldn't the compiler catch those syntax errors? I'm pretty embarassed right now.
No because as far as the compiler is concerned you are checking 2 different things, really I think it should give some kind of error with conditionals that only contain literal constants, or whatever they are called (but it doesn't):
1
2
3
4
if (  'M'  )
   cout << "M is still M" << endl;

// this really makes no sense. 
Last edited on
so I guess besides the syntax error I had my logic is still a little off. I can print the first three shapes, which are all lines just fine. When I add a triangle with an else if statement I only get the first horizontal line to print.

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
#include <iostream>

using namespace std;
int main()
{
  int numofobjects, length, height;                                \\ length of line, height of triangle
  char shape, direction, draw;                                       \\ shape of object to print, direction of line (vertical, horizontal, diagonal, char    
                                                                                           \\ to draw shape with
  cin >> numofobjects;[output][/output]
  
  for (int i=0; i<numofobjects; i++)
    {
      cin >> shape;
      if (shape == 'L' || shape == 'L')                              \\ determines shape is a line
	{
	  cin >> direction >> length >> draw;
	  if (direction == 'H' || direction == 'h')
	    {
	      cout << "Horizontal Line" << endl;           
	      for (int i=1; i<=length; i++)                             \\ loop to print horizontal line
		cout << draw;
	      cout << endl;
	    }
	  else if (direction == 'V' || direction == 'v')
	    {
	      cout << "Vertical Line" << endl;
	      for (int i=1; i<=length; i++)                             \\ loop to print vertical line
		{
		  cout << draw;
		  cout << endl;
		}
	    }
	  else
	    {
	      cout << "Diagonal Line" << endl;
	      for (int j=length; i<=length; i++)
		{
		  for (int j=length; j>i; j--)                                 \\ loop to print diagonal line
		    cout <<" ";
		  cout << draw;
		  cout << endl;
		}
	    }
	}
      else if (shape == 'T' || shape == 't')                         \\ determines shape is a triangle
	{
	  cin >> height >> draw;
	  cout << "Triangle" << endl;
	  for (int i=0; i<height; i++)
	    {
	      for (int j=1; j<=height; j++)
		{
		  if (j<=i)
		    cout << " ";
		  else 
		    cout << draw;
		}
	      cout << endl;
	    }
	}
      cout << endl;
    }
  return 0;
}


my inputs are:
3
L H 10 &
l v 3 +
T 4 %
If you have a debugger, it would be a good time to learn how to use it (Microsoft Visual C++'s is very good and pretty easy to use IMO).

What do you mean by you are only getting the first line? Could you copy the output exactly (use [output][/output] tags)

Btw, THANKS for actually using code tags and having good indentation! It helps a lot.
i was trying to copy the output but it doesnt seem to be working for me?

the first three nested if statements print a horizontal, vertical, and diagonal line. all three print out fine.... when i add the else if for the triangle the program then only prints the horizontal line. this whole linux redirection is kinda throwing me off so i even tried inserting the cout statements to visualize what was going on in the loop but im still getting no where. basically the program has to print one or more of the following shapes" horizontal line, vertical line, diagonal line, triangle, diamond.... i figured out how to draw each shape speerately now i cant put it all together
It's peculiar. I tried your code and drawing a triangle works well for me.

Input: T 4 %

Output:
Triangle
%%%%
 %%%
  %%
   %


The output tags are meant to be used for formatting of your posts not to be included in your code.
Last edited on
thats so weird, because to me, logically the code seem fine, I still get just the horizontal line to print, and what looks like a bunch of blank spaces????
and the triangle prints fine if i run in a program by itself.... i was thinking i go lost somewhere in the nested if statements or my input was not being read correctly somehow??
Topic archived. No new replies allowed.