Thread: While loop issue

  1. #1
    Destoryer of Worlds
    Join Date
    Jul 2005
    Posts
    17

    While loop issue

    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; 
    }


    problem is that it will terminate the loop after reaching 99 but not
    if the sentinel is entered and i dont know why. If i use a if
    statement with a break in the loop then it works fine but im not
    able to put it all in the while checking any help would be helpfull.
    Windows - The colorfull clownsuit for dos.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Consider the first iteration. count = 0, but then you do count++ before testing memory[count] for the sentinel value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Destoryer of Worlds
    Join Date
    Jul 2005
    Posts
    17
    LOL yea i just realized that
    sorry for the post and thanks
    Windows - The colorfull clownsuit for dos.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 10:09 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. Memory issue with new[] and delete[]
    By Zarkhalar in forum C++ Programming
    Replies: 24
    Last Post: 08-07-2004, 07:45 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM