Check string, sum of bin

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 :)

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
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
    string retez1, retez2;
    string zbytek;

    cin >> retez1;
    getline(cin, retez2);


    while (retez1.size() > retez2.size())
    {
    retez2 = "0" + retez2;
    }
    while (retez1.size() < retez2.size())
    {
    retez1 = "0" + retez1;
    }

    cout << retez1 <<'\n' << retez2;
    return 0;

}
Last edited on
EDIT: So i solved this deleting white space in string 2, so now i just must solved how to make binary sum of string 1 and string 2. Any ideas ?

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
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int main()
{
    string retez1, retez2;
    string zbytek;

    cin >> retez1;
    getline(cin, retez2);


    if (retez2.find(' ') != string::npos) {
    cout << "found!" << '\n';
    retez2.erase (retez2.begin());

    }

    while (retez1.size() > retez2.size())
    {
    retez2 = "0" + retez2;
    }
    while (retez1.size() < retez2.size())
    {
    retez1 = "0" + retez1;
    }



    cout << retez1 <<'\n' << retez2;
    return 0;

}
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).
Last edited on
just do this:

cin >> retez1 >> retez2;


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>

using namespace std;

int main()
{
    string input = "101001";
    bitset<32> bits(input);                // string to bitset
    unsigned long 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
Last edited on
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 ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    for(int i = 0; i < retez1.length() ; i++)
                {
                    if(retez1.at(i)== '0' && retez2.at(i) =='0')
                    {
                    vysledek.at(i) =  '0';
                    }

                    if(retez1.at(i)== '1' && retez2.at(i) =='0')
                    {
                    vysledek.at(i) =  '1';
                    }

                    if(retez1.at(i)== '0' && retez2.at(i) =='1')
                    {
                    vysledek.at(i) =  '1';
                    }

                    if(retez1.at(i)== '1' && retez2.at(i) =='1')
                    {
                    vysledek.at(i) =  '1';
                    }

                }
Last edited on
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.
so next try, i use your carry methot and do if else but it still calculate wrong like without carry, idk what is wrong :/

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
   for (int i = retez1.length() - 1; i >= 0; i--)
                {
                    if(carry == '0')
                    {

                                if(retez1[i]== '0' && retez2[i] =='0')
                                {
                                vysledek[i] =  '0';
                                carry == '0';
                                }

                                if(retez1[i]== '1' && retez2[i] =='0')
                                {
                                vysledek[i] =  '1';
                                carry == '0';
                                }

                                if(retez1[i]== '0' && retez2[i] =='1')
                                {
                                vysledek[i] =  '1';
                                carry == '0';
                                }

                                if(retez1[i]== '1' && retez2[i] =='1')
                                {
                                vysledek[i] =  '0';
                                carry == '1';


                                }
                    }


                    else
                    {

                                if(retez1[i]== '0' && retez2[i] =='0')
                                {
                                vysledek[i] =  '1';
                                carry == '0';
                                }

                                if(retez1[i]== '1' && retez2[i] =='0')
                                {
                                vysledek[i] =  '0';
                                carry == '1';
                                }

                                if(retez1[i]== '0' && retez2[i] =='1')
                                {
                                vysledek[i] =  '0';
                                carry == '1';
                                }

                                if(retez1[i]== '1' && retez2[i] =='1')
                                {
                                vysledek[i] =  '1';
                                carry == '1';
                                }




                    }

            }
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 else if is a bit more efficient, but doesn't affect the result.
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
    for (int i = retez1.length() - 1; i >= 0; i--)
    {
        if (carry == '0')
        {
            if (retez1[i] == '0' && retez2[i] == '0')
            {
                vysledek[i] = '0';
                carry       = '0';
            }
            else if (retez1[i] == '1' && retez2[i] == '0')
            {
                vysledek[i] = '1';
                carry       = '0';
            }
            else if (retez1[i] == '0' && retez2[i] == '1')
            {
                vysledek[i] = '1';
                carry       = '0';
            }
            else if (retez1[i] == '1' && retez2[i] == '1')
            {
                vysledek[i] = '0';
                carry       = '1';
            }
        }
        else
        {
            if (retez1[i] == '0' && retez2[i] == '0')
            {
                vysledek[i] = '1';
                carry       = '0';
            }
            else if (retez1[i] == '1' && retez2[i] == '0')
            {
                vysledek[i] = '0';
                carry       = '1';
            }
            else if (retez1[i] == '0' && retez2[i] == '1')
            {
                vysledek[i] = '0';
                carry       = '1';
            }
            else if (retez1[i] == '1' && retez2[i] == '1')
            {
                vysledek[i] = '1';
                carry       = '1';
            }
        }
    }


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.
Last edited on
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 :)
Last edited on
You're welcome, glad you are making progress.

In my code I used string result(len, '0'); which constructs a new string with '0' repeated len times.
See http://www.cplusplus.com/reference/string/string/string/ for details and examples.

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/
Last edited on
Topic archived. No new replies allowed.