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

A Little String Help Needed

Joined
Feb 11, 2008
Messages
607 (0.10/day)
Location
Omaha, Nebraska, USA
System Name Built By Me
Processor Intel Core i9 9900K @ 5.1 GHz
Motherboard Gigabyte Z390 Aorus Ultra
Cooling Custom Water Cooling - CPU Only
Memory 32GB (2 x 16) GSkill Ripjaws V DDR4
Video Card(s) RTX 4080 - ASUS ROG Strix 16GB OC - P Mode
Storage 1TB Samsung 970 Evo NVMe
Display(s) Alienware AW2723DF @ 280 Hz @ 1440P
Case Fractal Design Define S2
Audio Device(s) Corsair Virtuoso Pro
Power Supply 850W Seasonic Platinum
Mouse Razer Viper V2 Pro @ 2k Hz
Keyboard Asus ROG Strix Scope II 96 Wireless - ROG NX Snow Switches
Software Windows 11 Pro
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


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

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
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();
}
 
Joined
Feb 11, 2008
Messages
607 (0.10/day)
Location
Omaha, Nebraska, USA
System Name Built By Me
Processor Intel Core i9 9900K @ 5.1 GHz
Motherboard Gigabyte Z390 Aorus Ultra
Cooling Custom Water Cooling - CPU Only
Memory 32GB (2 x 16) GSkill Ripjaws V DDR4
Video Card(s) RTX 4080 - ASUS ROG Strix 16GB OC - P Mode
Storage 1TB Samsung 970 Evo NVMe
Display(s) Alienware AW2723DF @ 280 Hz @ 1440P
Case Fractal Design Define S2
Audio Device(s) Corsair Virtuoso Pro
Power Supply 850W Seasonic Platinum
Mouse Razer Viper V2 Pro @ 2k Hz
Keyboard Asus ROG Strix Scope II 96 Wireless - ROG NX Snow Switches
Software Windows 11 Pro
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;
}
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
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:
Joined
Feb 11, 2008
Messages
607 (0.10/day)
Location
Omaha, Nebraska, USA
System Name Built By Me
Processor Intel Core i9 9900K @ 5.1 GHz
Motherboard Gigabyte Z390 Aorus Ultra
Cooling Custom Water Cooling - CPU Only
Memory 32GB (2 x 16) GSkill Ripjaws V DDR4
Video Card(s) RTX 4080 - ASUS ROG Strix 16GB OC - P Mode
Storage 1TB Samsung 970 Evo NVMe
Display(s) Alienware AW2723DF @ 280 Hz @ 1440P
Case Fractal Design Define S2
Audio Device(s) Corsair Virtuoso Pro
Power Supply 850W Seasonic Platinum
Mouse Razer Viper V2 Pro @ 2k Hz
Keyboard Asus ROG Strix Scope II 96 Wireless - ROG NX Snow Switches
Software Windows 11 Pro
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:
 
Top