Somewhat new to c++, adding Prime number and Sqrt featre to program...

Hey there all. I'm taking C++ at school, and I am currently doing a project to calculate perfect numbers and other related things. However, i need really good features to get a good grade on this program (extras not mentioned in the instructions.)

Maybe if you guys could help me out here? I've thought of finding out if the number entered is a Prime number, or maybe if it's a perfect square. Though im not sure how to do this. we may only use for or while loops, if statements, all the basics. And we may only use the basic header files (not even sure what that means), like cstring, iomanip, iostream. if you would like to take a gander at my program, it's here. I like the program simply because the logic was so hard for me to figure out.

/* Variable Definitions
• num is the number entered
• i is used in the first for loop to make the program loop until termination
• add is used to add up all the values divisible by num
• add2 is used to add up all the values divisible by num for a second loop
• j is used similarly to i, just in another loop
• k is used similarly to i and num, just in another loop
• int_minus is the diffence between the integer and the sum of divisors
• ready is used to progress the program
• runagain tests whether user wants to run the program again
• p is used to find a perfect square and a prime number
*/

any help? Btw, for some strange reason my loop for running the program again works SOMETIMES, and other times it just shows up the cout, and just stops the program any. Any ideas on what the prob is? THanks a bunc people.

Re: Somewhat new to c++, adding Prime number and Sqrt featre to program...

Wow, I have a lot to say here.

Originally posted by thynksheraze Hey there all. I'm taking C++ at school, and I am currently doing a project to calculate perfect numbers and other related things. However, i need really good features to get a good grade on this program (extras not mentioned in the instructions.)

Kudos, man, for doing more than required.

Use code tags. It makes your code much easier to read.
Don't ever, ever use void main. Just don't. Please don't argue this. If you do, you will arouse the fury of every other programmer around here. They will come at you quoting programming standards and the creator of C++ himself. If you want to learn the many reasons why void main should never be used, search this board. You'll find tons.
Try to use functions in all of your programs. Modularity is great for many reasons. Functions are your friend.

Code:

// This checks all the values divisible by the number entered
cout << num << "'s divisor(s) is(are) as follows:" << endl;
for(i=1; i<=sqrt(num); i++) // this loop need only go to i <= sqrt(num) cuz that is the largest possible divisor that does not have a lesser pair
{
if(num%i==0)
{
cout << " : " << i << endl;
add = add + i;
}
}
// Check and see if integer is prime, this does not work for now
for (p=1; p < num && prime; p++) // the loop should exit once the first divisor is found
{
if ( num % p == 0 )
{
prime = false;
if (p * p == num ) // this shouldn't be nested in the if statement to test if it is prime
{
cout << num << " is a perfect square. Its square root is " << p << endl<<endl;
}
}
if ( prime )
{
cout << num << " is a prime number."<<endl<<endl;
}
}
if(runagain == "y" || runagain =="Y" || runagain == "Yes") // I kinda like the way you set this up
run = true;
else
run = false;
}
}
}
------ e n d p r o g r a m ----------------------------

[/B][/QUOTE]

I hope that helped. I'm not exactly sure what you wanted to do with square numbers. If you want to know if an inputted number is a perfect square, you could use the square root function in <cmath>. If you need more help, post your revised code and ask another question.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

joshdick, thanks so much for your input. It's very much appreciated. Thanks for adding comments too, i appreciate it.

But one thing im having problems with, is why my whole Prime number/ Perfect square finder loop doesn't even seem to run in the program. It just skips it. I can't figure out what I did wrong. you also commented on how the loop should not be nested, but i can't figure out how else to do it. Maybe if you could help me out here? I don't know what the problem is.

Thanks a lot for your time, and hope to here from you, and hopefully others, soon.

Exiting a Loop

Many times in a program there is a loop that should be exited once a certain condition is met. This can be achieved by declaring a boolean variable. ex:

Code:

bool found;
while(!found)
{
// search for something
}

In a for loop this can be done:

Code:

for(int i = 0; i < n && !found; ++i)
{
// search here
}

I haven't had time to look over all of your code, but here are a few suggestions if a loop seems to not be entered. First, make sure that all of your variables are initialized before being used. Next, ensure that they are within ranges acceptable to your program. This can be done using assertions. Write up pre and post conditions for your functions and loops.

Speaking of functions, use them. By using fuctions, you can test modules of your program independantly. This is very helpful in debugging. Once you get one module working exactly as it should, you can stop worrying about that module not working and can, therefore, spend more time concentrating on problematic chunks of code.

I wish I could take the time to look over your code and give specific help, but school's a *****. I gotta go work on boring history crap. I hope someone else can help you more.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

"If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.