Can you tell me what is wrong with this code?

Dec 10, 2014 at 3:52am
thanks everyone! this problem has been solved
Last edited on Dec 11, 2014 at 4:04am
Dec 10, 2014 at 4:33am
What does it output instead or what errors are you getting?
Dec 10, 2014 at 6:10am
1
2
3
4
	do {
		cout << "How many prime numbers do you want?  ";
		cin >> input;
	} while(input < 0);

this will loop forever until the user enters a negative number. I'm assuming that's probably not what you want.


My mistake. It's fine. I wrote that at midnight, after being up for 20 hours.
Last edited on Dec 10, 2014 at 10:05pm
Dec 10, 2014 at 4:40pm
it outputs nothing. it's literally blank when i try to input something. style-wise is it ok? is that do-while loop the only issue?
Dec 10, 2014 at 4:47pm
This is my output, and it is what i expected. Is it not what you want??
jafar@shadow:~/Desktop$ ./test
How many prime numbers do you want?  7
1:  2
2:  3
3:  5
4:  7
5:  11
6:  13
7:  17
jafar@shadow:~/Desktop$ 
Dec 10, 2014 at 4:57pm
1
2
3
4
	do {
		cout << "How many prime numbers do you want?  ";
		cin >> input;
	} while(input < 0);


this will loop forever until the user enters a negative number. I'm assuming that's probably not what you want.


Better look again. . .it will continue to loop as long as input is negative.
Dec 10, 2014 at 6:25pm
The program is supposed to read a value between 1 and 5000 -called N. The program should then print out the first N prime numbers
Dec 10, 2014 at 9:13pm
It seems you dont know what you want.
Dec 10, 2014 at 9:24pm
I agree with shadowCODE. It looks to me like the code is working correctly. In his example he asked for 7 prime numbers and it printed out 7 prime numbers. If this is wrong then please explain what output you expect when the input is 7.
Dec 11, 2014 at 2:07am
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
#include <iostream>
using namespace std;

int getInput() {

	int input;
		cout << "How many prime numbers do you want?  ";
		cin >> input;
	return input;

}

bool isPrime(int x) {

	for (int i = 2; i <= x/2; i++) {
		if (x % i == 0) {
			return false;
		}
	}
	return true;
}

int main() {

	int N = getInput();
	int count = 1;
	int num = 2;

    if(N<=0)
    {
    N=N*-1;
    while (count <= N) {
		if (isPrime(num)) {
			cout << count << ": -" << num << endl;
			count++;}num++;}
    }else{
	while (count <= N) {
		if (isPrime(num)) {
			cout << count << ":  " << num << endl;
			count++;}num++;}}
return 0;}
Dec 11, 2014 at 4:03am
thanks everyone!
Dec 11, 2014 at 4:07am
Please don't erase your post after people help you :)
Topic archived. No new replies allowed.