6 Replies - 441 Views - Last Post: 02 August 2014 - 02:59 PM Rate Topic: -----

#1 vnomics  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-August 14

do while loop does not loop

Posted 02 August 2014 - 11:03 AM

I have just recently started trying to teach myself C programming, and am attempting a programming excersise that I found online. The idea is to print a half pyramid on the screen using # as blocks. First though, the program is supposed to check whether or not the height falls within lower and upper bounds (zero and 23 in this case). If not, the program is supposed to re-prompt for a new height. The code I wrote is below:

#include <stdio.h>

int ht;

int main(void)
{
	do
	{
		printf("Height:");
		scanf("%d",&ht);
	}
	while(ht < 0 && ht > 23);

	for(int i = 0; i < ht; i++)
	{
		for(int j = 0; j < ht - i - 1; j++)
		{
			printf(" ");
		}
		for(int k = 0; k < 2 + i; k++)
		{
		printf("#");
		}
		printf("\n");
	}	
}




The program compiles just fine and it prints the half pyramid just fine, but for some reason it is not checking the input, which is what the initial do...while loop is supposed to do. If I enter a negative number, instead of reprompting, the program just stops, since the condition in the first for loop is true immediatly. If I enter a number above 23 it prints a half pyramid of that height. So, for some reason the do...while loop is not repeating when it should. I would not be suprised if I am missing something rather obvious, but can anyone suggest to me what I am doing wrong? Thanks alot in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: do while loop does not loop

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2115
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: do while loop does not loop

Posted 02 August 2014 - 11:24 AM

ht < 0 && ht > 23


It is not possible that ht is less than 0 and greater than 23 at the same time, so && is not the operator you want to use there.
Was This Post Helpful? 0
  • +
  • -

#3 Blindman67  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 125
  • View blog
  • Posts: 539
  • Joined: 15-March 14

Re: do while loop does not loop

Posted 02 August 2014 - 11:25 AM

The problem is you have set an impossible condition. You want it to loop while ht is less than zero and greater than 23. It can only be one or the other, not both at the same time.. Try while( !(ht >= 0 && ht <24)). While ht is not 0-23, or if you want it to loop while its between the two take the ! (not) out. while(ht >= 0 && ht < 24)
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: do while loop does not loop

Posted 02 August 2014 - 11:25 AM

> 12 while(ht < 0 && ht > 23);
Consider what happens when you enter -1
ht < 0 evaluates to true
ht > 23 evaluates to false
true && false is false - therefore the loop exits.
Was This Post Helpful? 0
  • +
  • -

#5 kutuup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-August 14

Re: do while loop does not loop

Posted 02 August 2014 - 02:11 PM

The operator I think you're looking for is the OR operator, the operator you have here:

while(ht < 0 && ht > 23);


as other people have pointed out, will evaluate as true only when ht is less than zero AND (&&) more than 23, which is impossible. What I think you want to check is whether either of those things is true, not both. The operator you need to use is "||", this is the OR operator, it evaluates as true when either condition is true. If you change the line to:

while(ht < 0 || ht > 23);


the loop will run until neither of those conditions is true, meaning ht is NOT less than 0 and also NOT more than 23.

Another way you could make the same check would be to use the NOT operator (!), in that case your code would look like this:

while(ht !> 0 || ht !< 23);


In that case, the loop would run as long as ht is less than 0 or more than 23.

If you are struggling with logical operators, have a read of this: http://www.cplusplus...rial/operators/

It covers all the different types of operator in C and C++

Happy coding!
Was This Post Helpful? 0
  • +
  • -

#6 Blindman67  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 125
  • View blog
  • Posts: 539
  • Joined: 15-March 14

Re: do while loop does not loop

Posted 02 August 2014 - 02:42 PM

kutuup in post: #5 said:

while(ht !> 0 || ht !< 23);


That is not correct !> and !< are not a C++/C operators. Not greater than is the same as less than or equal and visa versa

It would be written while(ht <=0 || ht >= 23) or while(!(ht>0 && ht <23))
Was This Post Helpful? 0
  • +
  • -

#7 kutuup  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-August 14

Re: do while loop does not loop

Posted 02 August 2014 - 02:59 PM

View PostBlindman67, on 02 August 2014 - 02:42 PM, said:

kutuup in post: #5 said:

while(ht !> 0 || ht !< 23);


That is not correct !> and !< are not a C++/C operators. Not greater than is the same as less than or equal and visa versa

It would be written while(ht <=0 || ht >= 23) or while(!(ht>0 && ht <23))


*facepalm* yeah you're right, sorry, had a brainfart lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1