A somewhat bizzare problem!!! - WHILE LOOP

This is a discussion on A somewhat bizzare problem!!! - WHILE LOOP within the C Programming forums, part of the General Programming Boards category; OK, I am very confused!!!! ...This simply just doesn't make any sense to me...... NOTE: This is for university coursework, ...

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    121

    Question A somewhat bizzare problem!!! - WHILE LOOP

    OK, I am very confused!!!! ...This simply just doesn't make any sense to me......

    NOTE: This is for university coursework, so please no answers about my code, or how to better code!!! ...All I want to know is why this section of code just "halts".... (it is for a coffeepot client / server application)

    Code:
        for (z = 4; z < 13; z++){
          i = 0;
          while (milkTypes[i] != NULL){ printf("test\n"); i++;}
          i = 0;
          /* DID THE CLIENT SEND A MILK TYPE ADDITION? */
          while (milkTypes[i] != NULL) {
            printf("milk: %s\n", milkTypes[i]);
    	if (strcasecmp(milkTypes[i], inToks[z]) == 0) {
              printf("found millk type\n");
    	  /* found users milk type - Get the qty of milk requested */
    	  for (y = 0; y < sizeof amountNumber / sizeof *amountNumber; y++) {
    	    if ((strcasecmp(amountNumber[y], inToks[z + 1]) == 0) || (strcasecmp(amountVolume[y], inToks[z + 1]) == 0)) {
    	      milkQty = y;
                  printf("milk qty found\n");
    	    }
                else printf("here1\n");
    	  }
    	}
    	/* INCREMENT I READY FOR NEXT ADDITION */
    	i++;
            printf("loop\n");
          }
          printf("finished milk\n");
    output....

    [code]
    ------------------------------------------------------------------
    -- NEW CLIENT (127.0.0.1) HAS CONNECTED
    -------------------------------------------------------------------
    test
    test
    test
    test
    test
    test
    test
    milk: Cream
    found millk type
    here1
    here1
    milk qty found
    loop
    milk: Half-and-Half
    loop
    milk: Whole-Milk
    loop
    milk: Part-Skim
    loop
    milk: Skim
    loop
    milk: Non-Dairy
    loop
    [code]

    OK, there is an array called milkTypes (which stores the different types of milk the client can request)

    The other arrays are amountVolumes and amountNumbers which are just arrays of strings to represent different levels of additions for a cup of coffee!

    The last array is the array containing the tokenised request from the client!

    Basically, this code is searching through the milk types array, then if it finds a milk type that matches the current token, it checks how much milk was requested by comparing the next token with strings in the amount volumes and numbers arrays!!!!

    Basically, it does not exit this looop!!!!! WHY WHY WHY!!!!! - It never prints "finished milk".... this is soo strange, as i have tested that it exits the loop when the milk types array is null, and it does! (as you can see from the test loop and test output!) but it never continues on to the next section of code!!!!!!

    If anyone has any ideas, any help advice is much appriciated!!!! ...Also, if you want to have the complete client and server programs, i can email them to you!!!! (bobthebullet990@hotmail.com)

    Many thanks!!!!

  2. #2
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    121
    OK, My lecturer said to use "goto" command to exit the loop as soon as the milk type and quantity have been found!!! ...BUT, Does anyone know the syntax for using goto and jumping to a labeled point in the code????

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    goto foo;
    
    ...stuff...
    
    foo:
    You mean like that?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Hard to say without seeing
    - how some data is declared and initialised
    - how you call that bit of code.

    > for (y = 0; y < sizeof amountNumber / sizeof *amountNumber
    I hope this is a real array, and not just a pointer to an array.
    Because it doesn't work as expected on pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  2. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  3. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM
  4. Loop problem
    By Tesnik in forum C++ Programming
    Replies: 29
    Last Post: 08-23-2007, 10:24 AM
  5. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21