# Program not working right

• 04-16-2005
blindman858
Program not working right
Hi guys I am really frustrated because it feels like i have tried everything and I still cant get my darn assignment to run. The program is supposed to find the largest prime number between 2 and some finite value a user inputs. For example if i input 9 the program should return 7 as the answer. The answer can also include the inputted number and 2. I am bascially attempting this problem with two for loops. One that will decrement the inputted number when certain conditions are met. The second for loop checks divisors and verifies primality. I cant seem to get the two loops to work together. That is break at appropriate times. I am really frustrated because i have been at this for hours with little success. Plz any help is appreciated.

Code:

``` #include <iostream> #include "stdafx.h" #using <mscorlib.dll> using namespace std; using namespace System; int _tmain() {             int b,d;         d=1;                 bool  a = false;                 cout << "INPUT B: "; cin >>b;         for (b; ( a ==false && b>2  ); b--){                                 for (int i=2; (i<=b-1 && d!=0); i++){                         d = b %i;                         }                                        if(d == 0)                         a = false;                 else                         a = true;                         cout<< b <<endl;         }                        return 0;         }```
• 04-17-2005
Salem
Don't try and do too much at once, break the problem down into smaller steps.

For example
Code:

```for ( b ; b > 0 ; b-- ) {     if ( isPrime(b) ) {         cout << b << " is prime" << endl;         break;     } }```
Now write a function
bool isPrime ( int n );
which tests the primality of a single number and returns true or false.

> #using <mscorlib.dll>
> using namespace System;
> int _tmain()
Try and stick to ANSI-C++ here and avoid all these microsoft extensions
main is declared as
int main ( )

Code:

```                else                         a = true;                         cout<< b <<endl;```
Always use braces, even around single statements.
Your indentation suggests the cout is part of the else, but it isn't.
• 04-17-2005
treenef
1.Try implementing a flow chart, if you haven't already done so.
2.Get a c++ for beginners and learn the simple stuff first. Your syntax seems a bit confusing- like salem said what's all the microsoft extensions for?
3. Put lots of *couts* statements, to help you debug the final code.

good luck

:confused:
• 04-17-2005
blindman858
Hey guys thanks for your help. 'I managed to figure it out. I needed to reset done by placing it outside the inner for loop. I then made the first for loop into a while loop because it seemed to print one less than the right answer if i left it that way. The reason why I am doing it this way and not with functions is because i simply have to. My teacher at school has not begun to lecture on functions and he expects us to follow along with the pace of the class. Clearly using a function is a better choice. All the extensions you see at the top are just standard in visual studios. The only ones i would need are of course <iostream> and using namespace std. Anyways thanks again for your help really appreciate it.