Thread: Program not working right

  1. #1
    Registered User
    Join Date
    Apr 2005

    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.

    #include <iostream>
    #include "stdafx.h"
    #using <mscorlib.dll>
    using namespace std;
    using namespace System;
    int _tmain()
    	int b,d;
    	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;
    			a = true;
    			cout<< b <<endl;
    	return 0;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Don't try and do too much at once, break the problem down into smaller steps.

    For example
    for ( b ; b > 0 ; b-- ) {
        if ( isPrime(b) ) {
            cout << b << " is prime" << endl;
    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 ( )

    			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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    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

  4. #4
    Registered User
    Join Date
    Apr 2005
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 5
    Last Post: 02-02-2003, 10:56 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. Simple Program not working right (in C)
    By DruzeTito in forum C Programming
    Replies: 5
    Last Post: 06-01-2002, 10:14 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM