Question:

Infinite loop problem in program in C++?

by Guest34087  |  earlier

0 LIKES UnLike

include <cstdio>

#include <iostream>

#include <cstdlib>

#include <math.h>

using namespace std;

void check(int min, int max);

void display (int prime);

void again ();

void body();

void main ()

{

body();

}

void body()

{

//initialize variables

int min = 0;

int max = 0;

printf("\n This program will find prime numbers starting from the minimum and maximum numbers that you enter.\n");

printf ("Please input minimum value to start from. Must be greater than 1.\n ");

//while minimum is less than 1 keep on repeating

while (min < 1)

{

scanf ("%d", &min);

if (min < 1)

{

printf ("\nPlease enter the right number.\n");

}

}

printf ("\nPlease input maximum value to end it at. Must be less than 15000\n");

while (max < min)

{

scanf ("%d", &max);

if (max < min)

{

printf ("Your max is smaller than your min! \n");

}

}

check (min,max);

}

void check(int min, int max)

{

bool prime;

int primetotal = 0;

printf ("Prime Numbers: \n");

for (int x=min; x<= max; x++)

{

prime = true;

//assume it's a prime to begin with

for (int y=2; y<x; y++)

{

//break if it is divisible

if ((x%y)==0)

{

prime = false;

}

}

//display if prime

if (prime == true)

{

display (x);

primetotal++;

}

}

printf ("\nTotal number of prime: %d", primetotal);

again();

}

void display (int prime)

{

printf ("%d,\t", prime);

}

void again()

{

int finished = 0;

printf ("\nFinished? Type 0 to continue and 1 to exit.\n");

scanf ("%d", &finished);

if (finished == 0)

{

body();

}

}

This is basically a program to find prime numbers within the given range. HOWEVER when I type letters such as "a" or "b" it just goes haywire and loops forever. When i put in negative number, the flow is controlled regularly.

Is there some problem with this?

 Tags:

   Report

3 ANSWERS


  1. //break if it is divisible

    if ((x%y)==0)

    {

    prime = false;

    }

    You need to *actually break*

    right after prime = false;

    do a break;

    BTW, I can&#039;t guarantee your code actually works.  For the love of all that is holy use some indentation man.

    -my boyfriend mumbled something about a double loop before he headed to bed.  It&#039;s all nonsense to me...


  2. A couple of thoughts... have you considered using the sieve of Eratosthenes?  Wikipedia might be able to illuminate that topic a bit more.  Just a thought.

    Secondly, your inner for() loop only needs to go to the square root of x:

    for (int y=2; y&lt;x; y++)

    could be

    for (int y=2; y&lt;sqrt(x); y++)

    It would save some processing time for you.

    As for the infinite loop, you&#039;ll need to use a char[] to store the results from the scanf.  If you give it a number, it won&#039;t be able to pull in ascii characters and therefore will continue to loop until it hits a scanf that can read the ascii.

    Try this:

    char temp_val[100];  //new variable

    while (min &lt; 1)

    {

    scanf (&quot;%d&quot;, &amp;min);

    if (min &lt; 1)

    {

    printf (&quot;\nPlease enter the right number.\n&quot;);

    scanf(&quot;%s&quot;, temp_val);

    }

    }

  3. you should check the result returned from scanf to know if variable was assigned.  also print the values assign while you debug. any characters not taken from stream first time  is also picked off by next scanf.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.