Thread: Help with tutorial on C++

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    25

    Help with tutorial on C++

    Ok so i'm trying to now figure our while, for, do.
    Its not explaning one thing:
    Can while be done "while" for is happening?
    How can you use all 3 in 1 code.
    Heres what i tried to write:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int x=1;
    
      
      for (int x =1;x>=0||x<=10; x=x+2)
      { cout<<x<<endl;
    }
    cin.get();
    while (x==9) {cout<<x-9; }
      do {
        cout<<"Learning to count!\n";
      } while ( x !=0 || x !=1 ||x !=2 );
      cin.get();
    }

  2. #2
    Registered User
    Join Date
    Jun 2008
    Posts
    25
    Anyone, i can't understand.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How can you use all 3 in 1 code.
    Your example is such an example, but the problem is that all your loops are infinite loops (once they start running, they never terminate), so actually running your code could pose a problem.

    Incidentally, you should format your code to make it more readable, e.g.,
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x = 1;
    
        for (int x = 1; x >= 0 || x <= 10; x = x + 2)
        {
            cout << x << endl;
        }
    
        cin.get();
    
        while (x == 9)
        {
            cout << x - 9;
        }
    
        do
        {
            cout << "Learning to count!\n";
        } while (x != 0 || x != 1 || x != 2);
    
        cin.get();
    }
    Incidentally, I note that you have two variables named x in main(). One is in the for loop.
    Last edited by laserlight; 06-20-2008 at 01:31 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Can while be done "while" for is happening?
    If i understand your question correctly, you want to enter a while loop inside a for loop, this is known as nested loops, and the answer is yes, it can be done

    Code:
    for(unsigned int i = 0; i < 10; i++)
    {
         unsigned int j = 0;
         while(j < 10)
         {
              j++;
         }
    }
    This means, that each time the for loop, loops 1 time, the while loop is looped 10 times (from 0 to 9).

    Is this what you meant?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    25
    Yes You Are A Genius!!!!!!!!!!!!!

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    you can also do a nested for loop inside with j.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 07:38 PM
  2. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Problem with tutorial (Vector class)
    By OdyTHeBear in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 02:49 PM
  5. My DirectInput tutorial....
    By jdinger in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-18-2002, 11:32 PM