Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 30th, 2005, 4:36 PM   #1
jch02140
Newbie
 
Join Date: Jul 2005
Posts: 15
Rep Power: 0 jch02140 is on a distinguished road
Problem with while loop...

Hi,

I am trying to print out a number entered by user that is positive and multiples of 10... But when I compile and execute the code below, only negative numbers will loop but any positive numbers will print out... Where have I done wrong?....

int main(int argc, char *argv[])
{
  int x;
  printf("Welcome to Carleton Bank.\n");
  printf("Please enter an amount...\n");
  scanf("%d",&x);
 
  while((x < 0) && (x % 10 != 0))
  {
        printf("Illegal number. Try again.\n");
        scanf("%d",&x);
  }
             
  printf("%d",x);
 
  return 0;
}
jch02140 is offline   Reply With Quote
Old Jul 30th, 2005, 4:46 PM   #2
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
  while((x < 0) || (x % 10 != 0))
  {
        printf("Illegal number. Try again.\n");
        scanf("%d",&x);
  }

I think that should fix it.
If you use && in this case, while() is not going to execute unless both coniditons are True.

EDIT: @7LSqr: saw that too
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Jul 30th, 2005, 4:51 PM   #3
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 600
Rep Power: 4 Ancient Dragon is on a distinguished road
in addition to what the others said, your loop is an infinite loop -- if it executes the first time it will just sit there an execute forever because x is never changed.
Ancient Dragon is offline   Reply With Quote
Old Jul 30th, 2005, 5:05 PM   #4
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
Quote:
Originally Posted by Ancient Dragon
in addition to what the others said, your loop is an infinite loop -- if it executes the first time it will just sit there an execute forever because x is never changed.
scanf won't change x? I must be missing something...
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Jul 30th, 2005, 5:22 PM   #5
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Quote:
Originally Posted by stevengs
scanf won't change x? I must be missing something...
Dragon must've missed that line, yes scanf will change x
OpenLoop is offline   Reply With Quote
Old Jul 30th, 2005, 4:47 PM   #6
L7Sqr
Hobbyist Programmer
 
Join Date: Jun 2005
Location: here
Posts: 146
Rep Power: 4 L7Sqr is on a distinguished road
Quote:
(x < 0) && (x % 10 != 0)
I believe what you are looking for is:
(x < 0) || (x % 10 != 0)

That way if either of the conditions is true the loop continues; not only if they both are true...

[edit]oops...Sorry Everlearning... [/edit]
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]
L7Sqr is offline   Reply With Quote
Old Jul 30th, 2005, 5:05 PM   #7
jch02140
Newbie
 
Join Date: Jul 2005
Posts: 15
Rep Power: 0 jch02140 is on a distinguished road
It works now after I change the AND to OR operator. Thank for pointing that out.
jch02140 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:13 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC