I have been trying to teach myself C++ lately (because it's so fun to program!) but I'll admit that I'm still a rotten shame at it. I've been trying to use pointers, and my friend who is a professional programmer told me that to edit a variable in a function not declared in that function, I'd have to use pointers, pass the pointer to the function (or something like that, I hope I didn't misunderstand him!)
This is the example he gave:
Code:
int bar( int *ptr )
{
*ptr += 30;
}
int foo()
{
int x = 7;
int *x_ptr = &x;
bar( x_ptr );
cout << x << endl;
}
I have lately been trying to figure out how to make a function that will do prime factorization for me, like breaking 500 down to 2x2x5x5x5. I've figured out that I need to use vectors and I'm going to change my method of testing if the factor can't be divided anymore (I'm using floats and testing if the float equals the int of that variable) and just going to use ints with a different method.
HOWEVER, I want to fix OTHER issues before this. One thing at a time, right?
Right now I have a function blah();, right? (Yes, I had fun naming these XD). I am currently just calling blah(); from int main() just to test this particular piece of code and this will later become a header file for another file, hopefully. So anyway, I have a function, called repeat(), that does all the work for me. However, I have to do a FEW things before I use repeat();, so I made another function, blah(), as I mentioned, where I do these few things and then call repeat(); Repeat then goes on to do its thing and use lots of recursion to solve my factorization for me and print it to screen.
Great.
Problem is that the program compiles fine, but when I try to run it, I get "Segmentation Fault."
Does this mean there is something wrong with my pointers? It seems a little excessive to me to have to declare two sets of pointers for my variables just to use them (they are not declared themselves in repeat(), so without the pointers I get compile errors about scope. But I'm not sure how else to do it.
So thus I have TWO questions.
a: Can anybody figure out where the segmentation fault is? I looked the code over several times and still can't find a single thing wrong, though obviously there is SOMETHING wrong or it'd be working!
and b: Is there any other better way to do this, this being modifying these variables in repeat() without having to use this many pointers to do it? It just seems so excessive, like there must be a better way and I'm just overcomplicating things.
Here is the code:
Code:
#include <iostream>
using namespace std;
int blah();
void repeat(int *p_Count,float *p_Number,int *p_Divisor);
int main()
{
blah();
return 0;
}
int blah()
{
int count = 0; // ignore for now
float number = 0; //number to be factored
int divisor = 2;
/* ^^^^^^^^ This will increase by 1 everytime it can't factor it down.
So for example, it will keep dividing the number by 2 until it is not
evenly divisible anymore and then will move onto three, etc. */
int *p_count = &count; //some pointers to pass the variables later
float *p_number = &number;
int *p_divisor = &divisor;
cout << "What is the number you want to factor?\n";
cin >> number;
cin.ignore();
repeat(p_count,p_number,p_divisor);
cin.get();
return 0;
}
void repeat(int *p_Count,float *p_Number,int *p_Divisor)
{
*p_Number = (*p_Number / *p_Divisor);
/* check if dividing by divisor made a decimal or if the number's
all the way down to 1 */
if ( *p_Number==int(*p_Number) && *p_Number !=1 )
{
cout << *p_Divisor << "x";
repeat(p_Count,p_Number,p_Divisor);
}
else if (*p_Number == 1)
{
cout << *p_Divisor; // we're all done!
}
else
{
*p_Number *= *p_Divisor; // get back to before we had decimal
*p_Divisor++; // add one to the divisor and start testing again!
repeat(p_Count,p_Number,p_Divisor);
}
}
It seems to work in MY mind at least. Someone would put in 500, then 500 would be divided by 2, it would check if it is still an integer, if so, it divides by 2 again, until it is no longer an integer (meaning it's not a factor, obviously).
Then it fails the test, goes to the else, multiplies the number by the divisor so it is back where it started BEFORE it got divided and become a decimal, then adds one to the divisor and starts again with a divisor one higher, say 3, and tries the tests again. It fails with three, goes to four, fails again, then goes to five and succeeds, three times in fact. The last time, when it gets it down to one, the else if is what will end it.
I ran through it with numbers in my mind and it SEEMED logical enough, but I obviously have errors in my code somewhere, because it won't run because of a "Segmentation Fault." Can anybody point out where my problem is? ^_^
Thanks!