# A Little String Help Needed



## dcf-joe (Dec 16, 2009)

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:



> : error C2664: 'atoi' : cannot convert parameter 1 from 'char' to 'const char *'
> 1>        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast





```
#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;
}
```


----------



## Kreij (Dec 16, 2009)

It's actually easier than that if you limit the input length.


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


----------



## dcf-joe (Feb 9, 2010)

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?


```
#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;
}
```


----------



## Kreij (Feb 9, 2010)

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

```
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


----------



## dcf-joe (Feb 10, 2010)

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 

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


```
#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


----------

