Need Help - Same Prime Number Question.

Hello. I am taking a beginning C++ class and the teacher is terrible (really. I'm not just saying that.) He wants us to write programs based on material we have not covered. Anyway, I am having trouble with an assignment. I am supposed to write a program that prints out the prime numbers between and including 1 and 239. I have written a small program, but I am getting error messages. Any help would be great!

//preprocessor directives

#include <iostream> //preprocessor directive
#include <iomanip> //preprocessor directive
#include <cmath> //preprocessor directive

using namespace std;
using std::setw;

bool prime(int n);

int main ()
{
//declare and initialize variables

int count = 0;

//loop

for (int loop = 1;loop <= 239; ++loop );

if (prime(loop))
(++ count);
cout << setw(3); << loop;


if
(count % 10 == 0)
cout << '\n';


return 0;
}
You haven't defined bool prime(int n) and your for loop does nothing (get rid of that trailing semi-colon.) Also, get rid of the semi-colon here: setw(3); <<

Use code tags. http://www.cplusplus.com/articles/jEywvCM9/

If you have code that won't compile and you're getting error messages, provide the error messages.
Don't forget to add braces where needed (for loop, if-control structures).

*edit* of course control structures can work without braces, it's just that the OP can prevent silly mistakes like some of the ones he/she made if he/she actually used braces like the rest of the world does.
Last edited on
No, it works with out braces, it's just that you can't have a statement inside another statement both without braces.
Also, don't add a semi-colon; after your for loop.
Thank you all! It's working:)
Topic archived. No new replies allowed.