• Program crashing for no apparent reason
    9 replies, posted
Since in school we just had number systems, I thought I'd try to make a program that'd allow me to convert a number into other number systems. While everything's been working fine, I can't seem to get negation working. This is the code I'm using for negation: [code] string operator-( void ) { string pom = binary; ten = -ten; int pom1 = -1; for( int i = 0; i < 31; i++ ) { if( pom.size() == p2[i] ) break; else if( pom.size() < p2[i] ) { pom1 = p2[i]; break; } } if( pom1 != -1 ) { for( int i = pom.size(); i < pom1; i++ ) pom = "0" + pom; } for( uint i = 0; i < pom.size(); i++ ) { if( pom[i] == '1' ) pom[i] = '0'; else if( pom[i] == '0' ) pom[i] = '1'; } int carry = 1; for( uint i = 0; i < pom.size(); i++ ) { if( pom[i] == '1' && carry == 1 ) { pom[i] = '0'; carry = 1; } else if( pom[i] == '0' && carry == 1 ) { pom[i] = '1'; carry = 0; } } } [/code] I have absolutely no idea why it crashes, any debugging didn't help, except for the fact that sometimes when I print pom[i], it prints out as 'a'. This is how I'm calling the function: [code] int main() { int a; scanf( "%d", &a ); bin b( a ); cout << -b << "\n"; } [/code]
Post error log/any messages that come up with a crash. Also post these threads in "what do you need help with" edit: also is this C? edit: nvm saw cout
[QUOTE=proboardslol;46015428]Post error log/any messages that come up with a crash. Also post these threads in "what do you need help with" edit: also is this C?[/QUOTE] C++, and all that comes up is 'example.exe has stopped working..'
[QUOTE=HumbleTH;46015461]C++, and all that comes up is 'example.exe has stopped working..'[/QUOTE] I feel like you haven't posted the entire code. At the top you have "ten" being negated but it's never been initialized
[QUOTE=proboardslol;46015473]I feel like you haven't posted the entire code. At the top you have "ten" being negated but it's never been initialized[/QUOTE] It's just the operator function, this is where ten is initialized: [code] class bin { private: string binary; string base4; string octo; string hex; int ten; int sum_1; public: bin( int dc = 10 ) { ten = dc; string buffer = ""; while( dc > 0 ) { int bit = dc % 2; dc /= 2; ostringstream out; out << bit; buffer = out.str() + buffer; } binary = buffer; sum_1 = sum1(); } } [/code]
Why does the bin() method have dc as a parameter if it's by default set to 10? are you trying to make a constructor?
[QUOTE=proboardslol;46015535]Why does the bin() method have dc as a parameter if it's by default set to 10? are you trying to make a constructor?[/QUOTE] Yeah. I thought that if no value was provided, it'd be 10?
You need a constructor. For a constructor you simply put the method definition twice; first with variable parameters, then with default parameters (for example) [code] foo(int a, int b, int etc){ bar = a + b + etc; } foo(){ /*default values are 12, 43, 762*/ bar = 12 + 43 + 762; } [/code] So in the case of the constructor, you write all code exactly the same, but using dunno if this is the root of your problem, but it's a start [editline]18th September 2014[/editline] Also I took a shot at doing the problem myself: [code] int counter = 0; /*global int used to count the digit place*/ int convertedString[80]; /*global string to put output values into*/ int conversionAlgorithm(int inputNumber, int toBase){ convertedString[counter] = (inputNumber % toBase); /*get the remainder of the input divided by the base to be converted to and add it to the next place in the array */ if(inputNumber == 1) return 1; /*when the algorithm gets to 1 or 0, collapse the recursion*/ else if(inputNumber == 0) return 0; counter++; return conversionAlgorithm(inputNumber / toBase,toBase) % toBase; /*recursive function; repeats the algorithm with the quotient of the input and the base to convert to as the new input*/ } [/code] I wish I could explain why this algorithm works, but I'm not a number theorist. I then use the main method to print out the strings. I have some conditionals added for hex. The program doesn't anticipate any base greater than hex, though. edit: also I did mine in C edit: also I don't know C++ or much about object-oriented programming so sorry if I can't be of more help
@proboardslol, bin(int dc = 10) {...} is the constructor (note the lack of a return value). In C++ you can have default values for parameters, so with that constructor both of these are statements valid and have the same result: [code] bin b1; //Use default value of 10 bin b2(10); //Explicity specify 10 as the parameter[/code] @HumbleTH, what is p2? You use it in the first loop of the function but I never see it declared or initialized and it's not a member of bin. Also you said you debugged it. At what line did it crash?
[QUOTE=Fredo;46030761]@proboardslol, bin(int dc = 10) {...} is the constructor (note the lack of a return value). In C++ you can have default values for parameters, so with that constructor both of these are statements valid and have the same result: [code] bin b1; //Use default value of 10 bin b2(10); //Explicity specify 10 as the parameter[/code] @HumbleTH, what is p2? You use it in the first loop of the function but I never see it declared or initialized and it's not a member of bin. Also you said you debugged it. At what line did it crash?[/QUOTE] I was thinking to myself "Maybe that's an acceptable constructor in C++" but it didnt seem right to me. Oh well my bad.
Sorry, you need to Log In to post a reply to this thread.