User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,537 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,159 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 400 | Replies: 1
Reply
Join Date: Oct 2007
Posts: 7
Reputation: eddy518 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
eddy518 eddy518 is offline Offline
Newbie Poster

Program not executing properly..assistance needed!

  #1  
Oct 13th, 2007
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.
  1. // This program allows the user to enter an integer greater than 1
  2. // and tests to see if this integer is a prime number.
  3. // This program also allows the user to do a few repetitions as possible.
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. int num, div=2, result=0;
  11. char answer;
  12.  
  13. // Get number from user
  14. cout << " Enter a number: ";
  15. cin >> num;
  16. cout << " The number is " << num << endl;
  17.  
  18. if ( num <= 1 )
  19. cout << "Please Enter another number: " <<endl;
  20. return 0;
  21.  
  22. while ( div < num && num % div != 0 )
  23. div++;
  24.  
  25. // number must be a prime
  26. if ( div == num )
  27. return 1;
  28. else
  29. // number is not a prime
  30. return 0;
  31. {
  32. if (num % div == 0)
  33. cout << div << " is a factor of " << num << endl;
  34. result++;
  35.  
  36. if (result == 1)
  37. cout << "The number " << num <<" is a prime\n";
  38. else
  39. cout << "The number " << num <<" is not a prime\n";
  40. return 0;
  41. }
  42. do
  43. {
  44. cout << "Do you want to continue?(Enter Yes or No)"<<endl;
  45. cin >> answer;
  46. num=answer;
  47. }
  48. while (answer != 'n' && answer !='N');
  49. return 0;
  50. }
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: Program not executing properly..assistance needed!

  #2  
Oct 13th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:51 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC