•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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.
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.
language Syntax (Toggle Plain Text)
// 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; }
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.
You might think the return 0 is part of the if, but it isn't.
Whereas you may have meant
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.
if ( num <= 1 ) cout << "Please Enter another number: " <<endl; return 0;
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Variable scope problem (C++)
- executing the normal program from linux.rc - different bahaviour (Kernels and Modules)
- Using Crontab? (*nix Software)
- aurora virus assistance needed! (Viruses, Spyware and other Nasties)
- a problem wilth C program (C)
- 'c:\windows\explorer.exe is attempting to change internet protection settings'! (Viruses, Spyware and other Nasties)
- Looking for an "Alert" Program (Geeks' Lounge)
- Help with compression program (C)
- what exactly is instaling/uninstaling a program. (Windows NT / 2000 / XP / 2003)
Other Threads in the C++ Forum
- Previous Thread: c++ games ...
- Next Thread: Case Changing Program - Need help on last bit



Linear Mode