Archived

This topic is now archived and is closed to further replies.

My program is crashing (possibly easy C++)

This topic is 5118 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here's my function as it is right now:

#include <iostream>
#include <math.h>
#include <string>

using namespace std;

void BinaryToDecimal()
{
	char * binary;
	int    i, j;
	int    decimal;
	int    length;
	
	std::cout << "Please input a binary string of 0's and 1's to convert to a decimal number:	";
	std::cin >> binary;

	length = strlen(binary);

	for (i = 0; i < length; i++)
	{
		if (binary[i] < 1)
			std::cout << "Invalid binary string! It must contain only 0's and 1's!\n";
		else
			std::cout << "Good job!\n";
	}	
}

int main()
{
	BinaryToDecimal();
	return 0;
}
 
That crashes, why?
R.I.P. Mark Osback Solo Pa Mi Gente VG-Force [edited by - Ekim_Gram on December 13, 2003 2:11:06 PM]

Share this post


Link to post
Share on other sites
You''re reading data into a char buffer that you never allocated. Since you are already using C++, it would in any case be better to use std::strings rather than char*''s.

Share this post


Link to post
Share on other sites
firstly, if you are "using namespace std" you dont need to type std:: in front of cin and cout, secondly should''nt it be "if (binary > 1)"? The only reason i can find that it wont work is that you''re trying to compare an int(1) with a char(binary)
there you go...

Share this post


Link to post
Share on other sites
quote:
Original post by Ekim_Gram
The Stroustrup book says you can''t use strlen with strings though, he says use char*''s.

The std::string has no need of strlen() as its size can be retrieved through the std::string::size() member function.

Share this post


Link to post
Share on other sites
*AMAZINGLY FRUSTRATED*

Can somebody please show me what they would do? I'm so damn confused right now cause I got 2 different people saying to different things. Here's what I have now:


void BinaryToDecimal()
{
char * binary;
int i, j;
int decimal;
int length;

cout << "Please input a binary string of 0's and 1's to convert to a decimal number: ";
cin >> binary;

length = strlen(binary);

for (i = 0; i < length; i++)
{
if (binary[i] < 1)
cout << "Invalid binary string! It must contain only 0's and 1's!\n";
else
cout << "Good job!\n";
}
}

int main()
{
BinaryToDecimal();
return 0;
}


Please help me find sancuary.



R.I.P. Mark Osback
Solo Pa Mi Gente
VG-Force

[edited by - Ekim_Gram on December 13, 2003 2:11:30 PM]

Share this post


Link to post
Share on other sites
The STL string class can expose the C string pointer. But, if you are only going to use it to get the length, then simply use the class''s length function instead.

Share this post


Link to post
Share on other sites
There is one major flaw in that function that is causing your program top crash: You have not allocated any memory for the variable binary. When you create a pointer, it is inititalized to point at garbage. You must allocate space by using the new keyword. Also, when you create memory this way, it is your responsibility to delete the memory when you are done with it.
Ex:
// Allocates space for 80 characters and the null terminator.
char *binary = new char[81];

.. later, when you no longer need binary...

// Give the memory back to the system.
delete [] binary;

If you are not aware, every character array(when you use strlen or cout on it or any other string operations on it) must have a "null terminator" to indicate the end of the string.

You may want to consider using the built in string class
#include <string>

as it will relieve you of the hassles of dynamic memory.
All you have to do is:
string binary;
cin >> binary;

and you will not have to worry about dynamic memory. Additionally, you can still treat the variable as an array of chars, accessing each individual char like so: binary[0]. Which brings up another minor problem in you code, the line:
if (binary < 1)
this line of code is checking that the memory address of binary is less than 1, which I do not think is your intention.

If you want to learn more about using character string input without using the string class, let me know.
I hope that helps!

[edited by - cecelski on December 13, 2003 2:08:34 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by cecelski
Which brings up another minor problem in you code, the line:
if (binary < 1)
this line of code is checking that the memory address of binary is less than 1, which I do not think is your intention.

The code certainly has problems, but I doubt this is one: Notice how all the code after this part is in italics? This is a typical case of [ i ] (without spaces) being eaten by the forum software and interpreted as an italics forum tag ...

Share this post


Link to post
Share on other sites

int Bin2Dec(std::string binary)
{
int ret = 0;
for(int i = 0;i < binary.length();i++)
{
if(binary[i] == ''1'')
ret++;
else if(binary[i] != ''0'')
std::cout << "Invalid binary string! It must contain only 0''s and 1''s!\n";
ret <<= 1;
}
return ret;
}


I think this should work

My Site

Share this post


Link to post
Share on other sites
quote:
Original post by Miserable
quote:
Original post by cecelski
Which brings up another minor problem in you code, the line:
if (binary < 1)
this line of code is checking that the memory address of binary is less than 1, which I do not think is your intention.

The code certainly has problems, but I doubt this is one: Notice how all the code after this part is in italics? This is a typical case of [ i ] (without spaces) being eaten by the forum software and interpreted as an italics forum tag ...



Ah, good call. I am not too familiar with the behavior of the forums.

[edited by - cecelski on December 13, 2003 2:13:43 PM]

Share this post


Link to post
Share on other sites
Now I have a new problem:


void BinaryToDecimal()
{
string binary = " ";
int i, j;
int decimal;
int length;

cout << "Please input a binary string of 0's and 1's to convert to a decimal number: ";
cin >> binary;

length = (binary.length());

for (i = 0; i < length; i++)
{
if (binary[i] > 1)
cout << "Invalid binary string! It must contain only 0's and 1's!\n";
else
cout << "Good job!\n";
}
}


No matter what I input it gives me the error message that's supposed to ONLY if the number is greater then 1.



R.I.P. Mark Osback
Solo Pa Mi Gente
VG-Force

[edited by - Ekim_Gram on December 13, 2003 2:19:59 PM]

Share this post


Link to post
Share on other sites
You must remember that you are storing the string as characters, and that the input you recieve is the ascii code for 1(which I believe, is numerically 41). A simple way(and ugly way) to convert it to a proper numerical value, so you can check it against 1, is do to the following:

char numerical = binary[0] - ''0'';

Here, you are subtracting the numerical value of 0 from the character, and when binary[0] id ''0'', numerical will contain 0; when binary[0] contains ''1'', numerical will contain 1 and so on.(the danger here, is that when binary goes beyond ''9'', you will have to think of sonething else). However, you are only concerned with 1 or 0, so that might not be a problem.
Now, "numerical" will contain a numerical value equal to the value you want.

Share this post


Link to post
Share on other sites
wow, I was under the assumption that when you call a function that will use cin and cout you should pass them as references within the function parameter list...

lemme just check that for a minute.


nevermind.

[edited by - nervo on December 13, 2003 2:47:30 PM]

Share this post


Link to post
Share on other sites
I changed the syntax to this:


for (i = 0; i < length; i++)
{
if (binary[i] != ''0'' || binary[i] != ''1'')
cout << "Invalid binary string! It must contain only 0''s and 1''s!\n";
else
cout << "Good job!\n";
}


But I still get the same problem.



R.I.P. Mark Osback
Solo Pa Mi Gente
VG-Force

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This not perfect, but it does compile and run. try it out.


#include <iostream>
#include <math.h>
#include <string>
using namespace std;

int Bin2Dec(string binary)
{
int ret = 0;
for(int i = 0;i < binary.length();i++)
{
if(binary == ''1''){
ret++;
}
else if(binary[i] != ''0''){
std::cout << "Invalid binary string! It must contain only 0''s and 1''s!\n";
}
ret <<= 1;
}
return ret;
}


int main()
{
string binary;
cout << "Please input a binary string of 0''s and 1''s to convert to a decimal number: ";
cin >> binary;
cout << Bin2Dec(binary);
return 0;
}

Share this post


Link to post
Share on other sites
>> << are bit shift operators, so if you have some value:
010101 >> 1 = 001010
Note that it just adds zeros whereever it knocks out a number. You can use these to perform power of two computations incredibly quickly.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
ret <<= 1;

is a bit shift operation.

value << shift

so

ret <<= 1;

is the same as:

ret = ret << 1;




Share this post


Link to post
Share on other sites
quote:
Original post by Ekim_Gram

if (binary[i] != '0' || binary[i] != '1')
cout << "Invalid binary string! It must contain



This will always result in true, since if one condition is false, the other one will automatically be true, and so the || operator will always result in true.
What you want is:


if(binary[i] != '0' && binary[i] != '1')


My Site

[edited by - Quasar3D on December 13, 2003 4:43:43 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Ekim_Gram
ret <<= 1;

What''s that do?

I never understood << and >> as mathmatical operators.



R.I.P. Mark Osback
Solo Pa Mi Gente
VG-Force


We humans do that too, when we multiply or divide by 10, or 100, or any power of ten. We just add, or remove a zero at the end (or shift the comma, as we called it at school). It''s the same principle.

My Site

Share this post


Link to post
Share on other sites