Can you tell me what is wrong with this code?

thanks everyone! this problem has been solved
Last edited on
What does it output instead or what errors are you getting?
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
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?
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$ 
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.
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
It seems you dont know what you want.
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.
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;}
thanks everyone!
Please don't erase your post after people help you :)
Topic archived. No new replies allowed.