How can I increase the char capacity?

Pages: 12
This program is supposed to convert a 1-10000 characters line to binary code.
When I enter more than 4571 characters the program doesn't output anything, but when I enter less characters the program works well!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 #include <iostream>

int main()
{
	const int size = 10000;
	char array[size+1]; //cstrings end in null terminators
	int arraydec [size+1], arraybin [size*10], binordr [size+1];
	binordr [0]=0;
	std::cin.getline( array , size , '\n' ); //read only 1 line of 10000 characters
	for (int arraydecvl=0, i=0; i<(size+1); i++) {
		arraydec [i]=array[i];
		arraydecvl=(arraydec [i]/2);
		for(int j=binordr [i];;) {
			if(arraydecvl==0){
				arraybin[j]=(arraydec[i]%2);
				binordr [j+1]=0;
				break;
			}
			else {
				arraybin[j]=(arraydec[i]%2);
				for(int k=(binordr [i]+1);;k++) {
					arraybin[k]=arraydecvl%2;
					arraydecvl=(arraydecvl/2);
					if(arraydecvl==0){
						binordr [i+1]=k+1;
						break;
					}
				}
				break;
			}
		}
		if(array[i+1]==0) {
			for (int j=0;j<binordr [i+1];j++) {
				for (int k=(binordr [j+1]-1);k>(binordr [j]-1);k--){
					std::cout << arraybin[k];
				}
			}
			break;
		}
	}
	return 0;
}

SOLUTION:
The problem is in the input method not in the char capacity!!
When the input method is the keyboard and the output is the screen, the program doesn't perform well. But, when the input and output method is a .txt file the program performs well.
I really don't know what actually causes that, But if any knows please tell us :)

Thanks for all who helped me!
Last edited on
The best way to do this would be to use an std::string instead of a char array and if you need to use a c-string you can just call stringName.c_str(). http://www.cplusplus.com/reference/string/string/

You could also dynamically allocate a new char array of a larger size when the max size is reached and transfer each element over, but that can lead to memory leaks if you are not careful and I would not recommend it.
Allocate the array dynamically at run-time instead.
 
    char * a = new char[10000];


and when you're done, release the memory by
 
    delete [] a;


http://www.cplusplus.com/doc/tutorial/dynamic/
what code should I type in order to have input (10000 characters in the same line) to be accepted and stored in the a [10000] array?

For more clarification:
1
2
3
4
5
wchar_t array [10000];

int main() {
	std::wcin >> array;
}

when I input 10000 characters in only one line and press enter program doesn't accept my input
Last edited on
cin.getline(a,10000,'\n') which will store the characters in cin in the char array a until either 10000 characters or a newline character.

http://www.cplusplus.com/reference/istream/istream/getline/
In order to store a c-string of length 10000, you need storage for at least 10001 chars.

In order to store a c-string of length 10000, you need storage for at least 10001 chars.

@cire:
How can I make this? knowing that with my code I can maximum input about 3000 characters
When I make a = 'line_containing_10000_characters', I get
- character constant too long for its type
Read the error. You're defining a character constant, not a string. Strings are enclosed with double quotes, not single ones.
@cire:
When I enclose with double quotes I get
incompatible types in assignment of 'const char [10000]' to 'char [10000]'
char a[10001] = "line_containing_10000_characters..." ;

You must use initialization or copy the string literal to the array. Assignment may not be used with array types.
@cire:
I did that but also the same result
1
2
3
string astr;
std::cin>>astr;
std::copy(astr.begin(), astr.end(), a);
Last edited on
You post a snippet of code that doesn't illustrate your problem and cannot cause the same error message.

I'm beginning to think you're trolling.
That's what I tried to do in the previous snippet :/
I have asked the user to input into the string astr, then I copied the string to the a [10000] which is the array I am going to do the process on it.
I am not trolling :/
Why can't you just do something like this:

1
2
3
const int size = 10000;
char array[size+1]; //cstrings end in null terminators
cin.getline( array , size + 1 , '\n' ); //read only 1 line of 10000 characters 

http://ideone.com/cMlYqz

*edit

This is exactly what the others mentioned too and should have no problems
Last edited on
I don't know what your confusion is, but let me try to clear something up.
I have an array
char a[10000]

which should have 10000 elements stored in it.

Yes, it stores 10000 elements, but remember that strings end with a null character ('\0'), so the most you can "actually" fit is 9999 characters + the null character at the end.
when I input 10000 characters line it isn't accepted.

What do you mean by "it isn't accepted"?
If it crashes or gives weird behavior, it's probably because you're going past the 9999+null limit.
How can I increase the char input capacity??

If you want anything up to 10000 characters, use
char a[10001];.
Otherwise, the easiest way is to use std::string instead:
1
2
3
4
string a;
cin >> a; // You can enter as much as you want here
// Or, if you have spaces in your input:
getline(cin, a);

(In fact, using std::string is probably just easier in general. If you ever find yourself needing a C string, use the .c_str() member function.)
@cire:
I applied yours on my code, but when I input 10000 characters, the program doesn't output anything also.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

int main()
{
	const int size = 10000;
	char array[size+1]; //cstrings end in null terminators
	int arraydec [size+1], arraybin [size*10], binordr [size+1];
	binordr [0]=0;
	std::cin.getline( array , size , '\n' ); //read only 1 line of 10000 characters
	for (int arraydecvl=0, i=0; i<(size+1); i++) {
		arraydec [i]=array[i];
		arraydecvl=(arraydec [i]/2);
		for(int j=binordr [i];;) {
			if(arraydecvl==0){
				arraybin[j]=(arraydec[i]%2);
				binordr [j+1]=0;
				break;
			}
			else {
				arraybin[j]=(arraydec[i]%2);
				for(int k=(binordr [i]+1);;k++) {
					arraybin[k]=arraydecvl%2;
					arraydecvl=(arraydecvl/2);
					if(arraydecvl==0){
						binordr [i+1]=k+1;
						break;
					}
				}
				break;
			}
		}
		if(array[i+1]==0) {
			for (int j=0;j<binordr [i+1];j++) {
				for (int k=(binordr [j+1]-1);k>(binordr [j]-1);k--){
					std::cout << arraybin[k];
				}
			}
			break;
		}
	}
	return 0;
}

Thanks in advance!

@long double main:
What do you mean by "it isn't accepted"?
I mean that the program doesn't output anything.
My full code is above, Please check it!
Thanks in advance!
Last edited on
You should have pasted the above code in the first place and offered an explanation as to what it does rather than claiming that the program is not printing the string.

Make sure that the condition on line 32 is being met, because after that, the values in the array should print.

Come to think of it, there is no way that condition is being met because that array is a char array not integer array. Maybe you wanted to do:

if(array[i+1]=='0')? Can't tell as you have not said what your program is supposed to do
On line 9:
std::cin.getline( array , size , '\n' ); //read only 1 line of 10000 characters

The size for getline includes the null terminator, so it should be
std::cin.getline( array , size+1 , '\n' ); //read only 1 line of 10000 characters .

Also, did you want the input to be exactly 10000 characters?
I reduced the size to 80 (for testing purposes), and it works for me if I enter exactly 80 characters (though I'm not sure what the meaning of the output is), but it crashes for anything less than that.
I would either add an extra check to make sure that the input is indeed 10000 characters long or initialize your binordr array to all zeros at first.
@Smac89:
Done, sorry for that!
Make sure that the condition on line 32 is being met
Yes, the condition on line 32 meets as when the user inputs 5 characters line, the array [5] is empty and it also equals zero.
The program works when I input less than 4571 characters, when I input more characters it doesn't output anything.
Thanks in advance!

@long double main:
When I edited to
std::cin.getline( array , size+1 , '\n' ); //read only 1 line of 10000 characters
The number of maximum characters that the program can process became 4178 not 4571!!!
Also, did you want the input to be exactly 10000 characters?
No, the input should be 1-10000 characters, but with my original code when I try more than 4571 characters the program doesn't output anything.
Thanks in advance!
Since you are looking to read multi line strings into a 1D string.
Recommendations:

1
2
std::istream::read
std::istream::getline // with null character as delimiting character 


istream::read
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;

int main() {
	string in(100, ' ');
	cin.read(&in[0], 100);
	cout << in << endl;
	return 0;                
}
Hello this
is a test string
to test cin.read
Hello this
is a test string
to test cin.read


istream::getline(istream&, string &, ...) // with null character as delimiting character
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;

int main() {
	string in(100, ' ');
	getline(cin, in, '\0');
	cout << in << endl;
	return 0;                
}
Hello this
is a test string
to test cin.getline(istream&, string&, ...)
Hello this
is a test string
to test cin.getline(istream&, string&, ...)


istream::getline(char *, ...) // with null character as delimiting character
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>
using namespace std;

int main() {
	string in(100, ' ');
	cin.getline(&in[0], 100, '\0');
	cout << in << endl;
	return 0;  
}
Hello this
is a test string
to test cin.getline(char *)
Hello this
is a test string
to test cin.getline(char *)
Last edited on
Pages: 12