1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

A Little String Help Needed

Discussion in 'Programming & Webmastering' started by dcf-joe, Dec 16, 2009.

  1. dcf-joe

    dcf-joe

    Joined:
    Feb 11, 2008
    Messages:
    381 (0.16/day)
    Thanks Received:
    22
    Location:
    Nebraska, USA
    I am trying to write a program to convert binary to decimal. I was thinking that maybe I could read all of the 0's and 1's together as a string, and then convert them to integers during the actual binary to decimal conversion. I keep on getting this error:


    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
       string bin;
       cout << "Give binary code: ";
       cin >> bin;
       for(int i = 0; i < bin.size(); i++)
       {
          cout << atoi(bin[i]) * 2;
       }
       return 0;
    }
     
  2. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.99/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    It's actually easier than that if you limit the input length.

    Code:
    int main ()
    {
        string bin;
        cout << "Enter Binary Number (max 32 digits) : "
        cin >> bin;
        cout << bitset<32>(bin).to_ulong();
    }
    
     
  3. dcf-joe

    dcf-joe

    Joined:
    Feb 11, 2008
    Messages:
    381 (0.16/day)
    Thanks Received:
    22
    Location:
    Nebraska, USA
    I finally accomplished what I wanted to do, with the assistance of vectors. However, I simply cannot understand why the endless for loop fails every time I input the number two. The program works without the endless for loop, but continues to multiply the initial binary to decimal conversion by numbers from 1 to ? whenever the for loop is implemented. How do I fix this?

    Code:
    #include <iostream>
    #include <vector>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	int position, input = 0;
    	double power, sum = 0.0;
    	vector<int> list;
    	
    	cout << "!!! Enter In Binary Code One Interger At A Time !!!" << endl << endl;
    	cout << "Enter In The Number 2 When You Are Finished Entering In Binary Code" << endl << endl;
    	
    	for(;;)
    	{
    		while(input != 2)
    		{
    			cin >> input;
    			list.push_back(input);
    		}
    	
    		for(position = list.size() - 2, power = 0.0; position >= 0; position--, power++)
    		{
    			sum +=  pow(2.0, power) * list[position];
    		}
    	
    		cout << endl << sum;
    	}
    	return 0;
    }
     
  4. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.99/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    You while loop will stop when the input is 2, but your for(;;) runs forever.
    You could add a binary flag to check for an input of two and use it instead of the for look
    Code:
    bool Done = false;
    
    while (!Done)
    {
       while (input != 2)
        {
            ...
            if (input == 2) Done = true;
        }
    
        ....
    }
    
    It's a little early so I haven't had much cofrfee yet. lol
     
    Last edited by a moderator: Feb 19, 2014
  5. dcf-joe

    dcf-joe

    Joined:
    Feb 11, 2008
    Messages:
    381 (0.16/day)
    Thanks Received:
    22
    Location:
    Nebraska, USA
    I would like to thank you Kreij for posting that little snippet, unfortunately it did not work. I incorporated it into my program, removed the for loop, and put code in to replace the "....", but still :confused:

    I then reread your post and you slapped me with common sense as I realized that input will always remain two after the initial calculation is completed:eek:

    Code:
    #include <iostream>
    #include <vector>
    #include <cmath>
    using namespace std;
    
    int main()
    {
    	int position, input = 0;
    	double power, sum = 0.0;
    	vector<int> list;
    	
    	cout << "!!! Enter In Binary Code One Interger At A Time !!!" << endl << endl;
    	cout << "Enter In The Number 2 When You Are Finished Entering In Binary Code" << endl << endl;
    	
    	for(;;)
    	{
    		while (input != 2)
    		{
    			cin >> input;
    			list.push_back(input);
    		}
    		
    		for(position = list.size() - 2, power = 0.0; position >= 0; position--, power++)
    		{
    			sum +=  pow(2.0, power) * list[position];
    		}
    		
    		cout << endl << "\tBinary ";
    		
    		for(position = 0; position < list.size() - 1; position++)
    		{
    			cout << list[position];
    		}
    		
    		cout << " Is Decimal " <<sum << endl;
    		
    		list.clear(); // [B]I need to clear the vector in order to properly reuse it[/B]
    		input = 0, sum = 0.0; // [B]I need to reset input and sum[/B]
    	}
    	return 0;
    }
    
    [B]Now it works properly[/B]
    /*
    !!! Enter In Binary Code One Interger At A Time !!!
    
    Enter In The Number 2 When You Are Finished Entering In Binary Code
    
    1
    0
    0
    1
    1
    1
    1
    0
    1
    0
    2
    
            Binary 1001111010 Is Decimal 634
    1
    1
    1
    1
    2
    
            Binary 1111 Is Decimal 15
    */
    
    Obviously, your statement helped me fix my own problem, which is what I should do as I am trying to learn the language for myself. Thank You:rockout:
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page