Strings and binary

This is a discussion on Strings and binary within the C++ Programming forums, part of the General Programming Boards category; Ok im kinda new to C++ How can i increment a string by adding an integer value onto the end? ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    14

    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

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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;
    }

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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;
    }

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    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.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    return 0;
    That should be the last statement in main()--not inside your while loop.

    LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
    Debug/binary.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    Have you ever compiled a C++ program before?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.
    Last edited by 7stud; 12-02-2005 at 07:27 AM.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    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++
    Last edited by Flip; 12-02-2005 at 07:47 AM.

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    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

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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";
    Last edited by 7stud; 12-02-2005 at 08:25 AM.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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:
    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. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.
    Last edited by 7stud; 12-02-2005 at 05:47 PM.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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.
    Last edited by 7stud; 12-02-2005 at 11:11 AM.

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    14
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary search on strings
    By ckathy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2007, 05:25 PM
  2. Running the 'strings' command on my binary executable
    By beginner.c in forum Tech Board
    Replies: 2
    Last Post: 07-16-2007, 03:55 AM
  3. help with binary strings?
    By warhero in forum C Programming
    Replies: 5
    Last Post: 06-21-2007, 10:49 PM
  4. reading strings from a binary .txt file
    By rwmarsh in forum C++ Programming
    Replies: 8
    Last Post: 03-05-2006, 08:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21