Program not working right

This is a discussion on Program not working right within the C++ Programming forums, part of the General Programming Boards category; Hi guys I am really frustrated because it feels like i have tried everything and I still cant get my ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    41

    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;
    	
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    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.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    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
    Posts
    41
    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, 09:56 AM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  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 Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 08:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21