prime number generator

Jul 30, 2011 at 4:27pm
I have just started learning c++ in the last week and have been having a ball with it. I am, however, having difficulty with a simple prime number generator that i have been working on. I have worked and reworked the relatively easy logic on it and when I run the program through in my head I don't see where the problem is. After you type in a value for y, instead of it returning that many prime numbers it just returns to the next line and does nothing. I don't understand. I am using visual c++ 2010

//find all prime numbers between 1 and any number

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

using namespace std;

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
int main()
{
	int y;
	int x = 0;
	int b;
	int a;
	bool g;

	cout << "How many Iterations? ";
	cin >> y;

	while (x <= y)
	{
		++x;
		b = 0;
		g = false;

			while ((b <= x) && (g = false))
				{
				++b;
				a = x % b;
				if((b < x) && (b > 1) && (a == 0)) 
					g = true;
				}
		if (g = false)
			cout << x << " ";
	}

	cin.clear();
	cin.ignore(255, '/n');
	cin.get();

	return 0;
}

Jul 30, 2011 at 4:40pm
= is assignment
== is comparison
Jul 30, 2011 at 7:19pm
holy crap... I knew it would be some tiny detail that I mixed up. It works beautifully now... well is outputting the correct results anyways, just not the correct number of primes per iterations selected although that's easy enough to see why.. Thank you
Last edited on Jul 30, 2011 at 7:26pm
Topic archived. No new replies allowed.