Thread: flag-controlled while loop

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    43

    Unhappy flag-controlled while loop

    hi,
    i have quite big program, with respect to my beginning skill, that contains a while loop. This loop, or the program itself, can only be terminated by pressing Ctrl-C.

    I'd like to change that by adding a flag into the condition so that whenever I entered -1, the loop should be terminated. I made this a small program to test thing out.

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	bool running = true;
    	int q=0;
    	int i=0;
    	while (running)
    	{
    		
    		cout << i++ << endl;
    		cout << "enter -1 to quit: ";
    		cin >> q;
    		cout << endl;
    		if (q < 0)
    		{
    			running = false;
    		}
    	}
    	printf("\n EXIT LOOP");
    	return 0;
    }
    However, as I run the exe, the program stopped at first iteration and output 0. If I entered any number that was greater than 0, then the nex iteration would come, so on and so forth. If entered -1 < 0, then the loop would terminate.

    Am I missing anything here? The program should keep showing i++ (0 1 2 3 4 5 6 7.......) until I hit -1.

    Any insight will be so great!!1
    thanks!!!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Are you expecting the loop to skip the input unless you input something?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    43
    thanks!
    i'd like to have the out to be
    1
    2
    3
    4
    5
    6
    7
    ......
    n (some number)

    until I hit "-1" the loop should terminate

    Hopefully that addresses your question!

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The program is working just as you coded it. The loop will stop to accept input (cin) and then keep going when you press enter, and will exit only when the value you enter is < 0.

    If you want an asynchronous event to cause the loop to exit, you'll need a more sophisticated program for that. You could use fork() to spin off the while loop while the main process waits for user input.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Worked for me...g++ 3.4.5.

  6. #6
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    If I understand correctly what you are trying to do, a popular (but nonstandard) method is to use kbhit() and getch().
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    43
    thanks!!!
    can you please abit more specific?

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here's an example in C using fork().
    Code:
    #include <sys/types.h> 
    #include <sys/wait.h> 
    #include "errors.h"
    
    int main (int argc, const char * argv[]) {
    	int exit_code ; 
    	pid_t pid ; 
    	
    	
    	while(1) { 
    		printf("After you press ENTER to start the loop, enter -1 at anytime to quit...\n") ; 
    		getchar() ; 
    		int counter = 0 ; 
    		pid = fork() ; 
    
    		if (pid == (pid_t)-1) errno_abort("Fork") ; 
    
    		if (pid == (pid_t) 0) {  // in the child 
    			while(1) {
    				counter++ ; 
    				if ((counter &#37; 1000)==0) printf("%d\n", counter) ; 
    			}
    		} 
    		else {   // in the parent, wait for user to enter -1.
    			do { 
    				scanf( "%d", &exit_code) ; 
    			} while (exit_code != -1 ) ; 
    			// Kill the child process 
    			printf("Killing child process %d...\n", (int) pid) ; 
    			kill(pid , SIGTERM) ; 
    			sleep(1) ; 
    			printf("Child killed.  Exiting.\n") ; 
    			exit(0) ; 
    		}
    	}
    	return 0;
    }
    errors.h
    Code:
    /*
     *  errors.h
     *
     *
     */
    
    #ifndef __errors_h
    #define __errors_h
    
    #include <unistd.h> 
    #include <errno.h> 
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    
    #ifdef DEBUG 
    #define DPRINTF(arg) printf arg 
    #else 
    #define DPRINTF(arg) 
    #endif 
    
    #define err_abort(code,text) \
    	do { \
    	fprintf(stderr, "%s at \"%s\":%d: %s\n", \
    	abort() ;  \
    	} while (0) 
    #define errno_abort(text) \
    	do { \
    	fprintf(stderr, "%s at \"%s\":%d: %s\n", \
    	text, __FILE__, __LINE__, strerror(errno)) ; \
    	abort() ; \
    	} while (0) 
    	
    #endif
    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int main (int argc, const char * argv[]) {
    I think main() is not supposed to have a const argv. There was a thread about it, and Dave_Sinkula pulled up a reference that suggested that this was the case. <deja-vu>Aha, found it.</deja-vu> http://cboard.cprogramming.com/showthread.php?t=99764
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by dwks View Post
    Code:
    int main (int argc, const char * argv[]) {
    I think main() is not supposed to have a const argv.
    Ha! Good one!

    Tell the Apple Xcode IDE development team.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Dino View Post
    Ha! Good one!

    Tell the Apple Xcode IDE development team.

    Todd
    If they listened to intelligent advice they wouldn't be working for the fruit company to begin with.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  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. loop debug
    By stanlvw in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2004, 02:59 PM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM