Hello, im beginner and i have problem with string and sum of two binary number in string.
I need to change string2(retez2), when user input 2 bin numbers like:
INPUT: 101001 1111
i get
STRING1: 101001
STRING2: _1111
so i must delete this first white space and i have no idea how i can do it.
After this i must do sum of this two binary numbers. How can i do it (some inspiration is enought, i think) ? Thank you :)
Regarding the whitespace. It would have been much simpler to not use getline at all, instead of this:
1 2
cin >> retez1;
getline(cin, retez2);
just do this:
cin >> retez1 >> retez2;
For the rest of the problem, it might be simplest to convert the two strings into ordinary integers, add them, and output the result (converting back to binary if required).
(On the other hand, you could attempt to directly add the contents of the two strings, but that would most likely be more difficult and require great care to avoid errors).
yes i know, but i must load if from one line and check if there is only one whitespace " ", if i do it with this cin >> retez1 >> retez2; i cant check what is between this two number.
Perhaps. I'm not sure what precise requirements you have. The method I suggested will ignore any whitespace.
For the rest of your question, again it depends upon what restrictions there are on the methods you can use. Example of string to integer and back to string, using a bitset:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <string>
#include <bitset>
usingnamespace std;
int main()
{
string input = "101001";
bitset<32> bits(input); // string to bitset
unsignedlong num = bits.to_ulong(); // bitset to number
bitset<32> bits2(num); // number to bitset
string output = bits2.to_string(); // bitset to string
cout << input << " decimal: " << num << " or " << output << endl;
}
Output:
101001 decimal: 41 or 00000000000000000000000000101001
Thats not so simple, i cant use int cause limited value of int or any other datatype. This program should make sum of two bit numbers, and i think largest testes number in bin is
16 000 sing long. I must somehow sum it in this two strings.
Ok, thanks for the clarification, the more detail you can give of the question,the easier it is for people to help you.
One way of looking at this is in terms of simple arithmetic.
You add two binary digits. The result is a digit representing their sum, and a possible carry digit.
1 2 3 4
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 carry 1
Then you start from the right-hand column of each number and add them together according to those rules, remembering to include the carry digit.
sorry, i should tell it sooner but anyway a thinking about this but i have no idea how its should looks like in code. and i dont know how apply carry... because everytime when i try put something in string i get error something like " terminate called after throwing an instance of 'std::out_of_range' what(): basic_string:at" so can you show me some source code how it should work ?
Well, the code you show above is missing a few things. First, and perhaps most important, the loop is processing the strings from left to right. But normal numbers have to be processed from right to left.
You could change it like this: for (int i = retez1.length() - 1; i >= 0; i--)
You also need a separate variable to store the current value of the carry digit. That's because each time the loop executes, it needs to make use of the value of carry from the previous iteration. Something like char carry = '0';
As for the details of the code, there are probably many ways to look at it. I've tried this several different ways, my first attempt was about 130 lines of if/else statements. My second attempt used some different ideas and was much shorter.
The alternative approach which I tried, instead of char carry = '0' used int carry = 0;.
I'd prefer not to post my version of the code at this stage as I think you are making progress and it would be good to achieve this yourself - with some assistance as you go along if necessary.
That is pretty good. The main problem is the use of == instead of = throughout the code where the value of carry is being set.
A smaller point, if a particular condition is true, there's no need to test all the following ones, so elseif is a bit more efficient, but doesn't affect the result.
You may have one remaining problem. After reaching the end of the code, there may still be a non-zero value of carry. In that case, you just need to insert the carry into the result (vysledek) at the left-hand side.
thaaankk youu :) i got it, only one last thing. How can i set size of string vysledek where i put my result ? It shouldnt be fixed size cause i dont know how long it will be. Now i have it like: and my numbers just replacing white space. string vysledek = "________ ";
i think there its something like alloc to solve it but i never work with it and i dont even know witch parametrs i should write there, but anyway thank you so much :) it was really helpfull and i learn something new :)
There is also function resize() which can make a string bigger or smaller. Here you can also specify a character to fill the rest of the string when it is made longer. http://www.cplusplus.com/reference/string/string/resize/