Can't stop endless loop?...

This is a discussion on Can't stop endless loop?... within the C++ Programming forums, part of the General Programming Boards category; Hello, I have this problem and I can't stop it looping only 'A'...it won't print 'A' through 'Z'?... Code: #include<iostream> ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Can't stop endless loop?...

    Hello, I have this problem and I can't stop it looping only 'A'...it won't print 'A' through 'Z'?...

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    
    
    int main()
    {
        cout << "here is a list" << endl;
        char letter = 'A';
    
        while (letter <= 'Z')
        {
            cout << letter << '\t' << static_cast<int>(letter) << endl;
        }
    
        return 0;
    
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    You need to increase the letter in the loop.
    Woop?

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    I am not sure how to do that?

  4. #4
    Registered User
    Join Date
    Mar 2010
    Location
    Norway
    Posts
    25
    while "letter" is less than Z, which it will be at least the first time, because you've just set it to 'A'. what about the second time the while loop runs?

    Next time the loop goes on, It is still A. So A is still less than Z... This goes on and on, because you never change the variable "letter".

    A for loop:
    Code:
    for(int i = 0; i < 10; )
    {
      cout << i;//Endless loop, goes on forever, because i is ALWAYS less then 10.
    }
    Code:
    for (int i = 0; i < 10;)
    {
       cout << i;
       i = i + 1; // We increase 1 to i, every time, so variable "i" will sooner or later be greater than 10!
    }
    Same goes for your while loop, with char data type.
    Change the "letter" inside the loop...

    Can you manage to change a while loop with a variable of type char, now I've explained how you do it with for-loops and int variables?

    PS: You want to print out a list of all characters in the alphabet?
    I would have done it like this:
    Code:
      char letters[26];
        letters[0] = 'A';
    
        for(int i=0; i < 26; i++)
        {
                cout << letters[i] << " ";
                letters[i+1] = letters[i] + 1;
        }
    After this is ran, letters[0] = A, letters[1] = B, letters[2] = C, letters[3] = D...so on. If you want to store the characters, as in; if you dont need the characters for anything later on, I would not use an array here ofc... JUst printed the variable letter + i...
    Last edited by ManyTimes; 03-24-2010 at 06:56 PM.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74
    You didn't change the value of 'letter', so it is always 'A'.
    Your can try the following code.
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
        cout << "here is a list" << endl;
        char letter = 'A';
    
        while (letter <= 'Z')
        {
            cout << letter << '\t' << static_cast<int>(letter) << endl;
            letter++;
        }
    
        return 0;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with this endless loop
    By xkohtax in forum C Programming
    Replies: 4
    Last Post: 05-01-2009, 03:32 PM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  3. cin >> buf endless loop
    By cfriend in forum C++ Programming
    Replies: 2
    Last Post: 10-07-2005, 05:01 PM
  4. Endless loop!
    By Paninaro in forum C Programming
    Replies: 3
    Last Post: 06-23-2002, 09:15 PM
  5. 2 largest elements; -1 to stop loop
    By Peachy in forum C Programming
    Replies: 4
    Last Post: 09-16-2001, 06:16 AM

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