My program is crashing (possibly easy C++)

Started by
20 comments, last by Ekim_Gram 20 years, 4 months ago
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]
Advertisement
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.
The Stroustrup book says you can''t use strlen with strings though, he says use char*''s.


R.I.P. Mark Osback
Solo Pa Mi Gente
VG-Force
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...
If I use strings I can''t use the strlen function. How to I get around that then?


R.I.P. Mark Osback
Solo Pa Mi Gente
VG-Force
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.
*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]
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.
.
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]
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 (without spaces) being eaten by the forum software and interpreted as an italics forum tag …

This topic is closed to new replies.

Advertisement