My loop is not so infinite?

This is a discussion on My loop is not so infinite? within the C++ Programming forums, part of the General Programming Boards category; They say the best way to learn programming is to learn by doing, and that's why I've made this : ...

  1. #1
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34

    My loop is not so infinite?

    They say the best way to learn programming is to learn by doing, and that's why I've made this :
    Code:
    #include <iostream>
    using namespace std;
    
    // Prime number function - Tests number for remainder. If none is found, return false. Otherwise, return true.
    
    bool PrimeFunction()
    {
    	int n;
    	cin >> n;
    	cin.ignore();
    	
    	while (n)
    	{
    		
    	if (n % 2 == 0)
    	{
    		cout << "Number is not prime";
    		return false;
    			
    	}
    		
    	else 
    	{
    		cout << "Number is prime";
    		return true;
    			
    	}
    	
    	}
    }
    
    
    int main()
    {
    	cout << "Enter a number and press enter: \n";
    	PrimeFunction();
    	
    	cin.get();
    	
    	return 0; 
    }
    What I'm trying to do is fairly obvious, and it works, just not the way I want it to. The problem is, after you have input the number and it tells you the result, on the next press of enter the program ends. What I want it to do is after you've pressed enter and the result is printed, the program lets you enter another number to be tested, rather than ending. I do apologise for the clear-as-mud description but I can't think of another way of wording it.

    What do I need to change about the program to get it to do that? I also apologise for the indenting, it looks great in SCiTE, but awful here.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    You need another loop, probably in the main() function, that contains the current request for a number, the call to PrimeFunction(), and also another request for the user's choice on whether to continue or end the program.

    I also apologise for the indenting, it looks great in SCiTE, but awful here.
    Mostly it looks okay here, except that the while loop body is not indented.
    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

  3. #3
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34
    Thanks laserlight, I never thought of using another loop.

    I took the indenting out of the While body, because I thought it looked messy in code tags.

    EDIT: laserlight, if you read this, thanks! It works the way I want it to now.
    Last edited by Caduceus; 01-25-2008 at 03:06 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    You're probably mixing tabs n' spaces. The golden rule reads: don't. Use only tabs or spaces.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34
    I'm not mixing, it's just my code doesn't look good with code tags.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    42
    I hope you know that the code won't work in many cases.
    Last edited by Link_26; 01-25-2008 at 04:21 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Quote Originally Posted by Caduceus View Post
    I'm not mixing, it's just my code doesn't look good with code tags.
    I doubt it doesn't. I haven't seen such a thing, unless someone mixed tabs n' spaces. How about posting how it really looks like instead of deleting indentation? It's better that way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34
    Here's a pic of it in SCiTE:

    http://img246.imageshack.us/img246/8821/scitebx0.png

    Here's the same section in tags:

    Code:
    #include <iostream>
    using namespace std;
    
    // Prime number function - Tests number for remainder. If none is found, return false. Otherwise, return true.
    
    bool PrimeFunction()
    {
    	int n;
    	cin >> n;
    	cin.ignore();
    	
    	while (n)
    	{
    		
    		if (n % 2 == 0)
    		{
    			cout << "Number is not prime\n";
    			return false;
    				
    		}
    			
    		else 
    		{
    			cout << "Number is prime\n";
    			return true;
    				
    		}
    	}
    }
    
    int main()
    {
    	cout << "Enter a number to test for prime-ness. \n";
    	
    	int i;
    	
    	while (i)
    	{
    		PrimeFunction();
    	}
    
    	cin.get();
    See what I mean?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Yes, the difference is that the tabs are longer, but that doesn't justify removing indentation. It messes up when others read the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    There isn't any point in that "while (n)" loop, as the code is written such that it will never loop. You have a return statement in the 'if', and the 'else' inside the loop, so no matter what it is going to exit the function without looping.
    You can change it to "if (n)" instead.

    What's worse though is that if you enter zero, then the control reaches the end of the non-void function, and returns an unspecified value. (The compiler will tell you about this bug if you let it do so by using the appropriate warning level). You need to return some value at the end of the function.

    Note that the function "PrimeFunction" doesn't do anything like what its name and comment suggests it does.
    Last edited by iMalc; 01-25-2008 at 09:50 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34
    Ok then, how would you do it? I don't like saying it, but I have only just started and would like to see a program that "works" the way it's supposed to.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Code:
    #include <iostream>
    using namespace std;
    
    // Prime number function - Tests number for remainder. If none is found, return false. Otherwise, return true.
    
    bool PrimeFunction()
    {
    	int n;
    	cin >> n;
    	cin.ignore();
    	
    	// You need to check if n > 0, right? Then we don't need a loop, but a condition.
    	if (n > 0)
    	{
    		if (n &#37; 2 == 0)
    		{
    			cout << "Number is not prime";
    			return false;
    		}
    		else 
    		{
    			cout << "Number is prime";
    			return true;
    		}
    	}
    	// If n == 0, the condition won't execute, right? So we need to make we return something here too! 0 is a prime, I think?
    	return true;
    }
    
    
    int main()
    {
    	cout << "Enter a number and press enter: \n";
    	PrimeFunction();
    	cin.get();
    	return 0; 
    }
    If you want a little more advanced (or how I would do it):

    Code:
    #include <iostream>
    using namespace std;
    
    // Prime number function - Tests number for remainder. If none is found, return false. Otherwise, return true.
    
    bool PrimeFunction()
    {
    	int n;
    	cin >> n;
    	cin.ignore();
    	
    	// If n == 0 OR the number is even dividable by 2, then it's a prime. Otherwise it's not.
    	if (n == 0 || n % 2 == 0)
    	{
    		cout << "Number is not prime";
    		return false;
    	}
    	else 
    	{
    		cout << "Number is prime";
    		return true;
    	}
    }
    
    
    int main()
    {
    	cout << "Enter a number and press enter: \n";
    	PrimeFunction();
    	cin.get();
    	return 0; 
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34
    Aha! I never thought of using logical-or! I tried to implement a break if the user enters zero, but that didn't work :P Sorry to keep asking questions, but I've read the short tutorial, and I can't figure it out, how does break work?

    Thanks for helping.
    Last edited by Caduceus; 01-26-2008 at 07:31 AM.

  14. #14
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Personally I woudl do this ---
    Code:
    #include <iostream>
    using namespace std;
     
    // Prime number function - Tests number for remainder. If none is found, return false. Otherwise, return true.
     
    bool PrimeFunction()
    {
        int n;
        int temp;
     
        cin >> n;
        cin.ignore();
     
        temp = 2;
        while ((temp * temp) < n)
        {
     
        if (((n/temp)*temp) == n){
            cout << "Number is not prime";
            return false;
            }
        temp++; 
        }
     
        cout << "Number is prime";
        return true;
    }
    If you are interested in primes, I have a file posted on my website that contains every 32 bit prime.

    http://www.anthonyqbachler.com/32bit...bin.part01.rar
    http://www.anthonyqbachler.com/32bit...bin.part02.rar
    http://www.anthonyqbachler.com/32bit...bin.part03.rar
    http://www.anthonyqbachler.com/32bit...bin.part04.rar
    http://www.anthonyqbachler.com/32bit...bin.part05.rar
    http://www.anthonyqbachler.com/32bit...bin.part06.rar
    http://www.anthonyqbachler.com/32bit...bin.part07.rar
    http://www.anthonyqbachler.com/32bit...bin.part08.rar
    http://www.anthonyqbachler.com/32bit...bin.part09.rar
    http://www.anthonyqbachler.com/32bit...bin.part10.rar
    http://www.anthonyqbachler.com/32bit...bin.part11.rar
    http://www.anthonyqbachler.com/32bit...bin.part12.rar
    http://www.anthonyqbachler.com/32bit...bin.part13.rar
    http://www.anthonyqbachler.com/32bit...bin.part14.rar
    http://www.anthonyqbachler.com/32bit...bin.part15.rar


    and if you need a recovery volume or 3 -

    http://www.anthonyqbachler.com/32bit...bin.part01.rev
    http://www.anthonyqbachler.com/32bit...bin.part02.rev
    http://www.anthonyqbachler.com/32bit...bin.part03.rev

    just give it an hour or so to finish uploading
    Last edited by abachler; 01-26-2008 at 10:36 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  15. #15
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34
    That seems like an awfully complicated way of doing it...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-15-2009, 12:24 AM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 01:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 12:17 PM

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