Thread: doing loops and it keep returning 0

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    8

    doing loops and it keep returning 0

    I was doing while, do while, and I kept on getting 0 even when the num is greater than 10. If i want to state it to be false do I got to add an if statement so when I execute it , it will show that greater than 10 is false?



    Code:
    
    # include <iostream>
    
     using namespace std;
    
    int main()
    {
    
       int x;
       x=0;
       do {
           cout <<"Hello World";
    }
     while (x != 0);
    
     cin.get();
    
    
    }
    Last edited by abrofunky; 02-14-2012 at 09:18 PM. Reason: add code

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The code bbcode tags should be used like:
    [code]the entire code here[/code]
    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

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    100
    Your code doesn't seem to reflect your question very well. In your question, you're talking about comparing num to 10. In your code, you're comparing x to 0.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    8
    yes, I messed up. Thanks!

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    100
    Now I think I see what you're doing. You may have already fixed this, but just in case:

    Basically it looks like you're trying to print "Hello World" to the screen once, then take user input. If the user input is a number less than or equal to 10, do all this again. If it's more than 10, break out of the loop and kill the program.

    In that case, one of the problems that you're running into is that you're not getting the input inside the loop; you're only getting it once the loop has already terminated. Another issue is that it's not being assigned to x at all. Basically x is being initialized to 0, and then that never changes; that causes an infinite loop. Try the following code instead:

    Code:
    # include <iostream>
     
    using namespace std;
     
    int main()
    { 
       int x;
       do {
           cout <<"Hello World";
           cin >> x;
       }
       while (x <= 10);
       return 0;
    }
    Or if that's not what you were trying to do, maybe this is:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       for (int i = 0; i <=10; i++)
       {
          cout << "Hello World";
       }
       return 0;
    }
    Or if necessary take ideas from both of these and use it to adapt your code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an integer ... not returning; weird error
    By Imanuel in forum C++ Programming
    Replies: 19
    Last Post: 09-25-2011, 01:30 PM
  2. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  3. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  4. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM
  5. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM