Arrays, Functions, Prime Numbers

I need help with this program. Nothing I've done seems to be working. What am i doing wrong?
Convert the program we did in class to use functions wherever possible. Also, save all prime numbers found into an array to be output to the screen when the program is finished. This should be in a function as well. I will cover arrays in functions on Monday in class.
the program should do the following:

Prompt a user for a number, then generated a list of all prime numbers onto the screen from 2 to the number entered (if is prime).

Examples:

Enter 10

Output: 2, 3, 5, 7

Enter 11

Output: 2, 3, 5, 7, 11



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<iostream>
#include<string>
 
using namespace std;
 
bool primeCheck(bool prime,int number, int &a, int &b)
{
	for(int a=2;a<=number;a++)
	{
		for(int b=2;b<a;b++)
		{
			if(a%b == 0)
			{

				prime = false; // we found a value that can evenly divide into the number entered
				break; //stops the loop
			}
		}
		if(prime == true)
			cout << "Prime number: " << a << endl;
		else
			prime = true; //reset prome back to true so we can continue to look for prime numbers
	}
	return true;
}
int getNumber()
{
	int number;
    cout << "Enter a number: ";
    cin >> number;
    return number;
}
 
               
int input(int counter,int number, int a)
{
	cout << "Enter a number: ";
    number[counter];
 
    counter++;
               
    return counter;
}
 
 
 
 
 
 
 
int main()
{
/*
write a program which will prompt the user for a number. then determine if that number is prime or not prime
 
a prime number is a just a number, nut it can only be evenly divisable by itself and the number 1
 
modulus is the math symbol we use to determine if one number can evenly divide into another
 
modulus % returns the remainder form a division operation

 
*/
	int number;
	int a,b;
	bool prime = true;
	int counter=0;
 
 
 
 //bool variable holds true or false nothing else
	number = getNumber();
    prime = primeCheck(prime,number,a,b);
    counter = input(counter, number);

	system("pause");
    return 0;
}
 
 
 
/*
 
for the assignment i asked you all to modify the above program so that it will list all prime numbers
from 2 to the number entered in by the user
 
25
2,3,5,7,11,13,17,19,23
 
7
2,3,5,7
 
 
 
 
*/

errors:
error C2109: subscript requires array or pointer type
error C2660: 'input' : function does not take 2 arguments
Last edited on
The error C2660 is because you only passed in two arguments to input(). C2109 is because you tried to use the subscript operator ("[]") on number, which is not an array.

Aside from these, there is also a program logic error. primeCheck() will return true no matter what you pass in. I'm not sure how you intend to do it, but if I was you I'd do this:
1
2
3
4
5
6
7
8
9
10
11
12
bool primeCheck(bool prime,int number, int &a, int &b)
{
	for(int a=2;a<=number;a++)
	{
		if(number % a == 0)
                {
                        prime = false;
                        return false; // we found a value that can evenly divide into the number entered
                 }
	}
	return true;
}


And what do you wish to do with input()?
Topic archived. No new replies allowed.