954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Program not executing properly..assistance needed!

Hey Fam,
I’m working on this ..so far this is what I have..(after almost 2 1/2 frustrated hours)
As a newbie, I figure out somehow the remainder and the prime concepts. I’m finally happy that I have 0 error but the program doesn’t do what I expected! Eventhough I tested w/t a prime number, a non-prime number, and an integer less than 2...
I’m struggling with my loop because I think the program performs the loop somewhere but doesn’t give the user the option to continue many times! where did I go wrong?
I’m attempted to use a do while loop for that. Will that works?
I think that I have use the % to find my remainder properly.
I also think that I have place my 2 integer variables num (use by the user) and div (to hold the remainder) in the right place..
As I put more effort to try to fix, the more problem I'm created and getting more confused. Any assistance is appreciated.
Thanks Eddy.

// This program allows the user to enter an integer greater than 1
// and tests to see if this integer is a prime number.
// This program also allows the user to do a few repetitions as possible.

#include <iostream>
using namespace std;

int main()
{
	int	num, div=2, result=0;
	char answer;
	
	// Get number from user
	cout << " Enter a number: ";
	cin >> num;
	cout << " The number is " << num << endl;
	
	if ( num <= 1 )
		cout << "Please Enter another number: " <<endl;
	return 0;
		
	while ( div < num && num % div != 0 )
		div++;
	
	// number must be a prime
	if ( div == num )
		return 1;
	else 
	// number is not a prime
		return 0;
		{
		if (num % div == 0)
		cout << div << " is a factor of " << num << endl;
		result++;
     	    	
     	if (result == 1)
     		cout << "The number " << num <<" is a prime\n";
     	else
     		cout << "The number " << num <<" is not a prime\n";
		return 0;
		}
		do
		{
		cout << "Do you want to continue?(Enter Yes or No)"<<endl;
		cin >> answer;
		num=answer;
		}
		while (answer != 'n' && answer !='N');
		return 0;
}
eddy518
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Well your haphazard approach to using { } to mark blocks of code (and general poor indentation to start with) means that your code returns almost immediately without doing anything.

if ( num <= 1 )
		cout << "Please Enter another number: " <<endl;
	return 0;

You might think the return 0 is part of the if, but it isn't.

Whereas you may have meant

if ( num <= 1 ) {
		cout << "Please Enter another number: " <<endl;
		return 0;
	}


If you always use { } even in the places where they're not strictly necessary, you'll save a hell of a lot of time (and confusion) when the code starts doing weird stuff because you forgot the single statement rule.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You