This is what I have so far:
#include <stdio.h>
int main()
{
int i, space, rows, k=0;
printf("Height: ");
scanf("%d",&rows);
for(i=1; i<=rows; ++i, k=0)
{
for(space=1; space<=rows-i; ++space)
{
printf(" ");
}
while(k != i)
{
printf("# ");
++k;
}
printf("\n");
}
return 0;
}
Whenever I try to insert various kinds of loops so that it will prompt the user until the integer is between 1 and 23, the program either doesn't work or works but ignores the loop. Also, whenever I try to use an expression containing an inequality (for almost any program, not specifically this one), I usually get "expression result unused" or "variable expected" with an '^' pointing at the inequality sign.
I'm obviously not asking for a specific solution here but trying to figure out if it is even possible to insert a loop in(or around) this code. Otherwise I will need to start from scratch. Hints, within reason, are welcome.
Also any general insight into what may be going wrong with my conditions containing inequalities would be appreciated.
edit: One of a few things I've tried:
#include <cs50.h>
#include <stdio.h>
int main(void)
{int i, space, rows, k=0;
printf("Height: ");
scanf("%d",&rows);
while(true){
scanf("%d",&rows);
(rows<=0 && rows>=23)
{
printf("Height must be between 1 and 23: ");
continue;
}
else if (rows>=0 || rows<=23)
for(i=1; i<=rows; ++i, k=0)
{
for(space=1; space<=rows-i; ++space)
{
printf(" ");
}
while(k != i)
{
printf("# ");
++k;
}
printf("\n");
}
return 0;
}
}
This was working as a functional program. When I ran it, though, I got this:
~/workspace/pset1/hello/mario/less/ $ ./loop
Height: 7
./loop
#
# #
# # #
# # # #
# # # # #
# # # # # #
# # # # # # #
~/workspace/pset1/hello/mario/less/ $ ./loop
Height: 84
2
#
# #
~/workspace/pset1/hello/mario/less/ $ ./loop
Height: -1
8
#
# #
# # #
# # # #
# # # # #
# # # # # #
# # # # # # #
# # # # # # # #
it wouldn't print a pyramid for anything less than zero or greater than twenty-three but it wouldn't repeat "height" Then I modified only the part of the program where it prompts the user for an integer from "Height must be a number from 0 to 23: " to "Height" and when I ran it again I got three error messages completely unrelated to the part I changed :
loop.c:13:27: error: expected ';' after expression
(rows<=0 && rows>=23)
^
;
loop.c:20:5: error: expected expression
else if (rows>=0 || rows<=23)
^
loop.c:13:15: error: expression result unused [-Werror,-Wunused-value]
(rows<=0 && rows>=23)
~~~~~~~ ^ ~~~~~~~~
3 errors generated.
make: *** [loop] Error 1