Thread: WTF my while loop is on permenent vacation

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    WTF my while loop is on permenent vacation

    In further pursing my silly solution to lilostitch382's "assign string to int array" I have been hamstrung:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class nlist : public string {
    	public:
    		nlist(string in) {
    			assign(in);
    		}
    		int extract(size_t pos) {
    			int retv = 0, m = 1;
    			size_t end;
    			while (at(pos) < '0'|| at(pos) > '9') {
    				printf("%d %d\n",pos,at(pos)); fflush(stdout);
    				pos++;
    			}
    			cout << "hey";
    			end = pos;
    			while (at(end) > '0'&& at(end) < '9') end++;
    			end--;
    			while (end>=pos) {
    				retv += (at(pos)-48)*m;
    				m*=10;
    			}
    			return retv;
    		}
    };
    
    int main() {
    	string src="1,44,56,33";
    	nlist nums(src);
    
    	cout << nums << "\n" << nums.extract(1);
    
    	return 0;
    }
    Execution pauses indefinitely at pos++.

    WHY????
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Are you sure you're not stuck in:
    Code:
    while (end>=pos) {
    That looks like the loop that will go forever...
    Your "hey" debug may not show up since there is nothing there to flush the output buffer.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bithub View Post
    Are you sure you're not stuck in:
    Code:
    while (end>=pos) {
    That looks like the loop that will go forever...
    Your "hey" debug may not show up since there is nothing there to flush the output buffer.
    Heh heh -- new things make me confused and paranoid.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM