Jump to content

Stuck in Loop

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Willum

Willum

    Newbie

  • Members
  • PipPip
  • 10 posts
Hey guys, I have a simple program that does one of two things. It either tells you if a number is prime or not or it tells you what the NEXT prime number is. However the while loop in the beginning, which checks to make sure you typed in the correct string or not won't exit out if you typed in the correct string. It compiles fine, but as soon as it hits the loop its screwy.

Here's the code:
#include <iostream>

#include <cmath>

#include <string>


using namespace std;

//function is being prototyped!

bool prime(int n);


int main(){


int i;

string selection;

cout<<"This program features two different programs, a prime number checker and a next prime number finder.\n";

cout<<"What program would you like to use?";

cout<<"\nType checker for the prime number checker or finder for the next prime number finder: ";

cin>>selection;


//makes sure you typed the right thing

while((selection != "finder") || (selection != "checker")){

    cout<<"Can you not read? Type checker or finder!";

    cin>>selection;

}


//Checker Program

if(selection == "checker"){

cout<<"Prime Number Checker: V1.0\nWritten by Willum.";

cout<<"\n\nEnter a number to see if its prime or composite: ";

cin>>i;

if(prime(i)){

    cout<<"\n"<< i <<" is prime!";

    }else{

        cout<<"\n"<<i<<" is composite!";

        }


    //Finder Program

    }else{

        cout<<"Next Prime Number Finder: V1.0\nWritten by Willum.";

        cout<<"\n\n Enter a number to find the next prime number: ";

        cin>>i;

        for(i; ; i++){

            if(prime(i)){

            cout<< i << " is the next prime number!";

            break;

            }

        }


    }

}

//n = i in the function. what goes in it, basically.

//the actual prime number function

bool prime(int n){

    int i;

    //prime number formula in a loop

    for (i=2; i<= sqrt(n); i++){

        //if there is no remainder

        if (n % i == 0){

            //its composite

            return false;

        }

        //can't find one, returns prime

    }

    return true;

}



#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

while((selection != "finder") || (selection != "checker")){


You used a logical 'OR' when you should have used a logical 'AND'.

These types of things can easily be debugged by building a truth table:


|  selection value  |  selection != "finder"  |  selection != "checker"  |  LOGICAL OR  |

|-------------------+-------------------------+--------------------------+--------------+

|  finder           |  FALSE                  |  TRUE                    |  TRUE        |

|  checker          |  TRUE                   |  FALSE                   |  TRUE        |

|  (anything else)  |  TRUE                   |  TRUE                    |  TRUE        |


From this you can see your logic error: Your WHILE condition never evaluates to anything other than TRUE, causing it to never escape the body of the loop.

Edit: In logic, we call this a "tautology": a condition that always evaluates to true, no matter what the variables are.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
Willum

Willum

    Newbie

  • Members
  • PipPip
  • 10 posts
Thank you very much! I can't believe I didn't think about that. I'm relatively new to programming, as the only thing I've done in the past is PHP (which most people consider a scripting language), so debugging is not one of my strong suits. I'll be sure to remember the truth table though. Thanks again!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users