Do { } Until Loop Continues Infinitly. Moronic Question (I Think)

This is a discussion on Do { } Until Loop Continues Infinitly. Moronic Question (I Think) within the C++ Programming forums, part of the General Programming Boards category; Leaning C++, It's a very pathetically reincarnated BruteForce =[ It just continues forever, and ignores the cout << ", "; ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    2

    Do { } Until Loop Continues Infinitly. Moronic Question (I Think)

    Leaning C++, It's a very pathetically reincarnated BruteForce =[ It just continues forever, and ignores the cout << ", ";

    Thanks for any help,
    <3 Jasio

    Edit<-- Please don't give me a 5 line code that does the same thing, that's not learning to me. Just help me correct the issue, thanks.

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
       int n;
       int a;
       int r;
      cout << "how many attempts ";
      cin >> n;
      cout << "how many letters ";
      cin >> a;
        do {
           do {
              r = (rand()%26);
              if (r == 0)
              cout << "a";
              if (r == 1)
              cout << "b";
              if (r == 2)
              cout << "c";
              if (r == 3)
              cout << "d";
              if (r == 4)
              cout << "e";
              if (r == 5)
              cout << "f";
              if (r == 6)
              cout << "g";
              if (r == 7)
              cout << "h";
              if (r == 8)
              cout << "i";
              if (r == 9)
              cout << "j";
              if (r == 10)
              cout << "k";
              if (r == 11)
              cout << "l";
              if (r == 12)
              cout << "m";
              if (r == 13)
              cout << "n";
              if (r == 14)
              cout << "o";
              if (r == 15)
              cout << "p";
              if (r == 16)
              cout << "q";
              if (r == 17)
              cout << "r";
              if (r == 18)
              cout << "s";
              if (r == 19)
              cout << "t";
              if (r == 20)
              cout << "u";
              if (r == 21)
              cout << "v";
              if (r == 22)
              cout << "w";
              if (r == 23)
              cout << "x";
              if (r == 24)
              cout << "y";
              if (r == 25)
              cout << "z";
              --a;
              } while (a != 0);
              cout << ", ";
              --n;
      } while (n != 0);
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Perhaps you mean to reset a after the inner loop exits? Also, use >= instead of !=, lesson 1.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    2
    Thanks.. Worked *smacks forehead*

    <3 Jasio

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    > Please don't give me a 5 line code that does the same thing, that's not learning to me.

    I hope that means you're going to do something about all those if statements; they're pretty ugly. You could at least use "else if"
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, I won't give you the 5 line version, but 2 hints so you can figure out how to code the 5 line version yourself:

    1. You can output a single character as well as a string:

    cout << 'a';

    2. You can do arithmetic on characters:

    char ch = 'a' + 4; // Same as char ch = 'e'.

    Oh, as a bonus tip, I recommend learning about for loops -- they usually are a better choice than do...while or while loops. For example, they avoid the common problem with nested loops that you had with not resetting a.
    Last edited by Cat; 09-08-2006 at 08:29 PM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    You have the wrong attitude. Your gross-misuse of copy and paste is a much bigger problem than your code simply not working properly. So here's my best effort to give you that 5-line version.
    Code:
    #include <iostream>
    int main () {
       int n, a;
       std::cout << "how many attempts ";
       std::cin >> n;
       std::cout << "how many letters ";
       std::cin >> a;
       for (int i=0; i<n; i++) {
           for (int j=0; j<a; j++)
               std::cout << char('a' + (rand()%26));
           std::cout << ", ";
       }
    }
    Okay, well I ended up with 13 lines...
    Any sensible beginner would learn an important lesson from this.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by iMalc
    Any sensible beginner would learn an important lesson from this.
    And a non-beginner might point out which part(s) as non-portable assumptions?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Incidentally, rand() is from <cstdlib>, and that header was not included. Also, the PRNG was not seeded with srand().

    One way to condense the selection of a character is to use a string:
    Code:
    const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
    
    // ...
    
    std::cout << alphabet[rand() % 26];
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Prelude won't be happy to see we're only using the low-order bits of rand()

    Jasio, you can't just write some lines and expect it to work, adding cumbersome fixes along the way. Programming isn't just brute-forcing your way around and hoping it works.

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
        //get size of rand() output in bits
        size_t shift_rand_size;
        for(size_t i = RAND_MAX; i; i >>= 1, ++shift_rand_size);
        
        //shift it by 5 bits because 26 < 2^5
        if(shift_rand_size  >= 5) shift_rand_size -= 5;
        else shift_rand_size = 0;
        
        //seed rand()
        srand(time(NULL));
        
        //get user input
        size_t a, c;
        std::cout << "Attempts: ";
        std::cin >> a;
        std::cout << "Characters per attempt: ";
        std::cin >> c;
        
        //loop and print
        for(size_t i = 0; i < a; ++i)
        {
            for(size_t j = 0; j < c; ++j)
            {
                std::cout << char('a' + (rand() >> shift_rand_size) % 26);
            }
            std::cout << '\n';
        }
        return 0;
    }
    Last edited by jafet; 09-08-2006 at 11:31 PM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308

    Red face Poor misguided fool...

    jafet, you've taken this "(rand() % N) isn't good enough" approach way too far.

    ((rand() >> shift_rand_size) % 26) actually is MUCH MUCH worse than (rand() % 26)!

    Sure, the higher bits are theoretically slightly more random. But by throwing many lower order bits away you've increased the bias by many orders of magnitude. With your code, the letter 'a' is far far more likely to occur than 'z'.
    If you're (rand() >> shift_rand_size) were to give you a number between 0 and 31 for example, then 'a' to 'f' would occur twice as often as 'g' to 'z'.

    It goes to show that unless you truly intimately understand the inner workings of a PRNG, and know exactly in what circumstances you really do need to do something about it, then don't mess with it!

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    It would also do you good to learn the switch statement.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Sure, the higher bits are theoretically slightly more random.
    No, it is not that the lower order bits are less random (though they may be, for a given poor implementation). Using modulo m for a range that is not evenly divisible by m leads to a (slight) change in the distribution.

    But by throwing many lower order bits away you've increased the bias by many orders of magnitude. With your code, the letter 'a' is far far more likely to occur than 'z'.
    I agree. I think something like this would be better:
    Code:
    int max = RAND_MAX - (RAND_MAX % 26) - 1;
    int num; // random number generated
    do {
    	num = rand();
    } while (num > max);
    num %= 26; // change range to [0, 25] without altering distribution
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  2. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  3. simple for loop function question
    By felicityxiv in forum C Programming
    Replies: 7
    Last Post: 05-06-2006, 11:43 PM
  4. Please don't laugh...SIMPLE loop question!
    By the_lumin8or in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2006, 12:08 PM
  5. KEYSCANNING within a LOOP, Newbie Question
    By Robert_Ingleby in forum C++ Programming
    Replies: 8
    Last Post: 11-23-2001, 05:49 AM

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