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".
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.
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.
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
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??