LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2005, 10:45 AM   #1
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
C while loop issue


In the process of builing a computer simulator for a book im reading ive
come to an issue but im not sure exactly why its not working.


here is the code
Code:
 

/* A simple computer simulator */ 

#include<stdio.h> 
#define SIZE 100

int main()
{ 
   int memory[SIZE];  //simulated memory  
   int count = 0; 
   //print welcome message first 
   printf("\n\n"); 
   
   printf("***Welcome to Simcom!***\n***Please enter your program one 
instruction word at a time**\n***I will type the location number and a 
question mark***\n***You then type the word for that 
location***\n***Type the sentinal -99999 to stop entering your 
program***\n");

  
  do 
   {   
        
       printf("%02d ?",count); 
       scanf("%d",&memory[count]); 
       printf("memory%02d contains %04d\n",count,memory[count]); 
       count++; 
       
    } 
    while (count < 100 && memory[count] != -99999);   

return 0; 
}

as you can see the loop should terminate if ither count is more then 100 or is 100 and if memory[count] is -99999

the issue is that when you type -99999 it does not terminate the loop.


am i doing something wrong ?
 
Old 09-17-2005, 10:55 AM   #2
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
bump

Last edited by exvor; 09-17-2005 at 10:56 AM.
 
Old 09-17-2005, 12:01 PM   #3
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Well, since you increment count after writing to the memory array the while condition will never look at what you just wrote but on the next element.
 
Old 09-17-2005, 12:07 PM   #4
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
LOL jebus i never even thought of that.


totaly my bad.

thanks dude
 
Old 09-17-2005, 02:25 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,642
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
Don't worry. The first program I ever wrote took weeks to write and it was eight lines long and it had a bug in it.
 
Old 09-17-2005, 03:11 PM   #6
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Original Poster
Rep: Reputation: 87
This computer simulator is making my brain hurt
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
while loop or for loop? mijohnst Programming 18 11-21-2005 04:48 PM
Binutils 2.14 loop trouble (different issue) VillageIdiot Linux From Scratch 5 12-26-2004 03:41 PM
WHILE LOOP in 'C' ']['HeBroken Programming 4 10-29-2004 01:42 AM
while-loop Thomas23 Programming 4 05-24-2004 03:35 PM
LI Loop ! What is this ? HELP DiamondViper Linux - Software 8 02-14-2002 07:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration