# Strings and binary

• 12-02-2005
Flip
Strings and binary
Ok im kinda new to C++

How can i increment a string by adding an integer value onto the end?

i am converting a decimal number to binary using the remainder method
in other words divide x by 2 and modulate x by two so im grabbing the remainder each time

12 in binary is 1100

wen i run the program if i include "cout << modx" in the loop i get a result of

0011

which is the reverse of what i need.

when i implemented this in VB6 i was able to have a string a increment it by addding the result of the MOD onto the end. Then after that i was able to reverse the string using another algorithm

Here is my code using C++

Code:

```#include <iostream> #include <string> using namespace std; int main (){ int x; int modx; string Reslt; cout << "Enter a Decimal"; cin >> x; while (x > 0){ modx = x % 2; Reslt = Reslt + modx; <----- This is my problem area x = x / 2; } cout << Reslt return 0; }```
People on other forums have been getting confused about what i am trying to achieve. So i have implented this in VB6

VB6 snippit
Code:

```Do Until x = 0 intMod = x Mod 2 Result = Result & intMod x = x / 2 x = x - 0.49 x = CLng(x) Loop and i could then reverse the string y = Len(Result) Do Until y = 0 Rev = Mid(Result, y, 1) k = k + Rev y = y - 1 Loop```
and my result will then be k
• 12-02-2005
7stud
Code:

```#include <iostream> #include <string> #include <algorithm> //reverse() using namespace std; int main() {         string str = "abc";         reverse(str.begin(), str.end() );         cout<<str<<endl;         return 0; }```
• 12-02-2005
7stud
It would be more efficient to use a deque(double ended que) and add the new elements on the front, but the notation is a little more frightening:
Code:

```#include <iostream> #include <deque> using namespace std; int main() {         deque<int> myBinary;                 myBinary.push_front(1);         myBinary.push_front(1);         myBinary.push_front(0);         //copy the elements to cout:         copy(myBinary.begin(), myBinary.end(), ostream_iterator<int>(cout, ""));                 cout<<endl;         return 0; }```
• 12-02-2005
Flip
Ok i've tried using your suggested solution..which looks good....although i cannot compile it due to the following erros:

here is the code

Code:

```#include <iostream> #include <string> #include <algorithm> using namespace std; int main (){ int x; int modx; string str; cout << "Enter a Decimal"; cin >> x;         while (x > 0){                 modx = x % 2;                 str = modx;                 reverse(str.begin(), str.end() );                 cout << str << endl;                 x = x / 2;                 return 0;         } }```
compiler returns

Code:

```E:\C++\binary\binary.cpp(23) : warning C4715: 'main' : not all control paths return a value Linking... LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16 Debug/binary.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe.```
• 12-02-2005
7stud
Quote:

return 0;
That should be the last statement in main()--not inside your while loop.

Quote:

LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
Debug/binary.exe : fatal error LNK1120: 1 unresolved externals
Have you ever compiled a C++ program before?
• 12-02-2005
7stud
Quote:

i implemented this in VB6 i was able to have a string a increment it by addding the result of the MOD onto the end. Then after that i was able to reverse the string using another algorithm
When is the code in your last post reversing the string? Compare that to your VB program. When does your VB program reverse the string?

Look at this line:

str = modx;

Does that add characters to str? You've made some changes to your original code that are puzzling.
• 12-02-2005
Flip
I think wat im really aiming for is to add an integer onto the end of a string

like....

suppose (psuedo code)

x is an integer

rslt is a string

for x=0 To 9

rslt = rslt + x

x++
• 12-02-2005
Flip
oh and yes i have comiled with C++ before im just strugglin to get used to the environment and the debugging

in the VB code the last loop takes the last character of the string and places it first in a new string
• 12-02-2005
hk_mp5kpdw
Quote:

Originally Posted by Flip
I think wat im really aiming for is to add an integer onto the end of a string

like....

suppose (psuedo code)

x is an integer

rslt is a string

for x=0 To 9

rslt = rslt + x

x++

One way that can be accomplished is to use a stringstream:
Code:

```#include <sstream> #include <string> using namespace std; ... string result; stringstream sstr; for(int i = 0; i < 10; ++i) {     sstr << i; } result = sstr.str();  // Convert information in stringstream into a string```
Outputtting the above string will print 0123456789.
• 12-02-2005
7stud
Quote:

I think wat im really aiming for is to add an integer onto the end of a string
In your case, there is an easy way to do that with the ternary operator:
Code:

```#include <iostream> #include <string> using namespace std; int main() {                 string str = "11";         int num = 0;         str += (num == 1)?  "1" : "0";         cout<<str<<endl;                 return 0; }```
You can do the same thing but not as concisely with an if statement:
Code:

```if(num == 1)     str = str + "1"; else     str = str + "0";```
• 12-02-2005
7stud
Quote:

oh and yes i have comiled with C++ before im just strugglin to get used to the environment and the debugging
To me it looks like this error:
Quote:

LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
is saying that you compiled your code as a windows program instead of a console program.
• 12-02-2005
7stud
Note that with a deque, you don't have to worry about converting from an int to a string: all the digits are stored as integers, like in an array. The advantage of a deque is that it's size grows automatically as you add elements, and you can add each element to the front of the deque, which means they will be in the proper order when you are done. Then, to display a deque or an array, you just output each element. That's what this cryptic line does:
Code:

`copy(myBinary.begin(), myBinary.end(), ostream_iterator<int>(cout, ""));`
It takes each element of the deque between the start of the deque and the end of the deque:

myBinary.begin(), myBinary.end()

and copies it as an <int> to cout:

ostream_iterator<int>(cout, "")

where 'cout' is the output to the screen, and it uses "" as the seperator, i.e. nothing. If that's too confusing for you, don't worry about it. Use one of the previous methods instead.
• 12-02-2005
7stud
By the way, if the code is for your own use and it's not homework where you have to demonstrate how to convert a decimal to binary, you can do this:
Code:

```#include <iostream> #include <string> #include <bitset> using namespace std; int main() {         int num = 12;         bitset<8> myBinary(num);         cout<<myBinary<<endl;         string str = myBinary.to_string();         cout<<str<<endl;                 return 0; }```
"bitset<8> myBinary" creates a variable called myBinary that is 8 bits long. You can make the number of bits anything you like. The number in parentheses is the decimal number that is to be converted to binary.
• 12-04-2005
Flip
Thank you i did a bit of thinking and got everything solved! no im just trying to learn C++ and decided to set myself a task of using the remainder method for number conversion.