6 Replies - 302 Views - Last Post: 03 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

while loop behaving strangely

Posted 03 August 2014 - 01:59 PM

So I have been given and as part of the solution I need to count the number of digits in a long long variable. To do this I use a while loop, but it is behaving strangely. Here is the code.

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main (void)
{
	printf("What is the card number?\n");
	long long card = GetLongLong();
	if(card <= 0)
	{
		printf("Invalid\n");
		return 0;
	}
	int digits = 0;
	long long card1 = card;
	while(card1 > 0);
	{
		card1 /= 10;
		++digits;
	}
	printf("%i\n", digits);
}


When I execute the program it asked for the number, but then nothing happens. Of course, my first instinct was that the program was caught in an infinite loop somehow, but could not figure out how. I commented out the while loop and the program completed (albeit returning the incorrect value), so I was further convinced that there was an infinite loop that I was not seeing. I ran the program throug gdb. Strangely, the program did not loop infinitely, instead it got to line 16 [while(card1 > 0] and then just stopped, it was not executing the next line of code at all. I am a relative novice, and at a bit of a loss, so I would appreciate any suggestions anyone has for what is going wrong and how to fix it.

Is This A Good Question/Topic? 0
  • +

Replies To: while loop behaving strangely

#2 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 801
  • View blog
  • Posts: 3,501
  • Joined: 03-December 12

Re: while loop behaving strangely

Posted 03 August 2014 - 02:28 PM

Why are you creating a 'copy' of card?

Then, why are you dividing that value by 10 in the loop? At best the loop would only run a couple of times.

And you miss your return statement at the end.

This post has been edited by astonecipher: 03 August 2014 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 801
  • View blog
  • Posts: 3,501
  • Joined: 03-December 12

Re: while loop behaving strangely

Posted 03 August 2014 - 02:45 PM

What is the goal of the program? Is this HW?

This post has been edited by astonecipher: 03 August 2014 - 02:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 vnomics  Icon User is offline

  • New D.I.C Head

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

Re: while loop behaving strangely

Posted 03 August 2014 - 02:56 PM

I create a copy to feed into the while loop because later on I will need to access card as well, though I have not written that code yet. Which is also why there is no return statement, yet.

The rational for dividing by ten is that each time it iterates it will remove a digit from card1, and then increase digit by one. Once the last digit is removed the loop should exit and 'digits' should equal the number of digits in card, car1 will equal zero, and the original number is still saved in card. I could believe there is a better way of implementing this, but that would not explain why the loop is not executing.
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: while loop behaving strangely

Posted 03 August 2014 - 02:59 PM

You have a semicolon after the while loop. A lone semicolon is an empty statement. Since the statement after the while loop is the loop's body, this means that your while loop has an empty body and the block after it does not belong to the loop at all. Remove the semicolon and the code should work fine.

View Postastonecipher, on 03 August 2014 - 11:28 PM, said:

Then, why are you dividing that value by 10 in the loop?


To count the number of digits.

Quote

At best the loop would only run a couple of times.


At best the loop would run exactly as many times as the number has digits, which is the point.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: while loop behaving strangely

Posted 03 August 2014 - 02:59 PM

Hi, it is an infinite loop you are correct, semicolons are powerful things, so remove this one :

16	while(card1 > 0) //; <-- remove


Was This Post Helpful? 0
  • +
  • -

#7 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 801
  • View blog
  • Posts: 3,501
  • Joined: 03-December 12

Re: while loop behaving strangely

Posted 03 August 2014 - 02:59 PM

16	    while(card1 > 0);


See anything wrong with the start of the loop?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1