+ 5
Is there some limit to the number of letters we can enter in a string?
I tried using a string to store a very large number, around 35660 digits long (Its 10000!, what I wish to store...). Unfortunately, the program displayed a bad_alloc error, when I assigned this value to a string. So, is there a limit to the amount of characters we can store in a string? If there is, Why is it so small? I was only able to enter at max 240 characters per string. Isn't the basic_string class supposed to have a larger size than 240? Or was it the problem of getline(), which I used?
13 odpowiedzi
+ 5
I had a problem storing mine too but figured out that the string has to be on 1 line. That +100,000 characters had to be on 1 line. 😂 1 line, can you believe it.
So try storing it in your code again but on 1 line.
Go take also a look at my code's JS here. You'll definitely get an idea of how I got it right.
https://code.sololearn.com/WLoLoVyZWd2R/?ref=app
+ 7
in c# I'd suggest you to look on BigInteger class, maybe c++ has something like that..
+ 5
@Gavin
It is as I thought, writing the number does not return any error.
This problem is solved, but I still have to find a way how to copy paste data on console easily, without getting that error...
Thank You!
+ 3
@Gavin Christians
So, will this method, max_size, return the number of bytes or the number of characters?
//Well it is the same for both.
// I am getting 1073741820 as the answer, if that is what I assume it is, then I should be able to store my number...
+ 3
@Gavin
I am providing input to the PC in one line, but I think the error is due to the fact that I am copying the data from a file.
After all, I can't simply go enter all the digits of 10000! factorial without copy or paste.
Thus, maybe some unwanted chars came along, and the string crashed.
All my recent inputs were copy pastes...
Ill now try writing a large string, holding a number key for very long...
+ 3
Glad you solved it ☺
+ 2
@Illusive Man
Im actually trying to develop one of my own 😅. Looks like I can't trust strings for this...
+ 2
Correct @Kinshuk Vasisht. My highest character count I had for a string was over 100,000 characters for my Wisdom Calculator Code
+ 2
Then, why can't I store my number?
Some problem with cin?
+ 2
I tried both ways.
Input is where I have the problem, it automatically shows std::bad_alloc after I hit enter...
When I try assigning it in code, it fails as well, but reading from a file is successful...
+ 2
Why did nobody mention these approaches:
std::string str1;
str1.reserve(size);
std::vector<char> str2;
Both give the desired effect because vector should resize before its mem is used.
Using the reserve() member of string allows you to preallocate memory on the other hand and since you know how much you need, allocate enough.
This is why I like C, I know after any *alloc call whether I have enough memory.
+ 1
You can look here, there is a way to check
https://stackoverflow.com/questions/2310971/maximum-number-of-characters-in-a-string
+ 1
How are you storing it? Directly in your code or as an input you have to enter?