do while loop using a for statement

The program compiles and runs, but errors out at the beginning of the do-while loop. Any suggestions are greatly appreciated.
It's possible it's the combination of the do-while and the for loop. Or perhaps my modulus calculation is wrong.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void printNum(int primeNum); 

int main()
{
	int startRange, endRange;
	int count=0;
	int divisor = 2;
	int remainder;

// enter a range of numbers
	cout << "Enter the starting number of your range: \n";
	cin >> startRange;
	cout << "Enter the ending number of your range: \n";
	cin >> endRange;
// startRange
// endRange
// run code to determine which numbers within the range are prime
 //for every number in range, find the remainder
	do
	{for (count = startRange; count<= endRange; count++)
	remainder = count%divisor;
	}while (remainder != 0);
	
	divisor++;

    if (divisor == count){
	printNum(count);
	}
	else{	
    cout << "There are no prime numbers in your range.\n";
	}	//function
	
 system("pause");
 return 0;
}

// prime - run function
// function - prints num

/***************
printNum
***************/
void printNum(int primeNum)
{
cout << primeNum << endl;

}
I do not see any sense in these statements

1
2
3
4
	do
	{for (count = startRange; count<= endRange; count++)
	remainder = count%divisor;
	}while (remainder != 0);


The condition while (remainder != 0) will be always true if endRange is an odd number.
Last edited on
thank you vlad from moscow.
how about this one. i think the looping might be better on this one.
again. it's compiling and running, but errors out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

int calcPrime(int);

int main()
{
	int startRange, endRange;
	int count;

	cout << "Enter the starting number of your range: \n";
	cin >> startRange;
	cout << "Enter the ending number of your range: \n";
	cin >> endRange;

	for (count = startRange; count <= endRange; count++)
	{ calcPrime(count);
	}
		
	system("pause");
	return 0;
}

/****
calcPrime
******/
int calcPrime(int num)

{	int divisor=2;
	int remainder;			
	
			do
			{remainder = num%divisor;
			divisor++;
			} 
			while (remainder !=0);

				if (--divisor == num)
				return num;
}
i think i got it. let me know if you have suggestions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

// more_functions

#include "stdafx.h"
#include <iostream>

using namespace std;

void calcPrime (int);

int main()
{
	int startRange, endRange;
	
	cout << "Enter the first number in your range:\n";
	cin >> startRange;
	cout << "Enter the end number in your range:\n";
	cin >> endRange;

	do
	{calcPrime(startRange);
	startRange++;
	}while (startRange <= endRange);

	system("pause");
	return 0;
}

/*****
calcPrime
*****/
 void calcPrime (int num)
{
	int divisor=2;
	int remainder;
	
			do
			{remainder = num%divisor;
			divisor++;
			} 
			while (remainder !=0);

				if (--divisor == num){
				
				cout << num << endl;
				}

				
}
Topic archived. No new replies allowed.