• We've upgraded our forums. Please post any issues/requests in this thread.

A Little String Help Needed

Joined
Feb 11, 2008
Messages
532 (0.15/day)
Likes
162
Location
Lincoln, Nebraska, USA
System Name This PC
Processor Intel Core i7 2600K @ 4.8 GHz
Motherboard ASUS P8Z68 Deluxe
Cooling Custom Water Cooling - CPU Only
Memory 8GB (2 x 4GB) AData DDR3
Video Card(s) GTX 1080 TI - EVGA FTW3 - Slave BIOS
Storage Corsair Force GT 128GB / Samsung 850 Pro 512 GB
Display(s) AOC AGON AG271QG - 165 Hz - AHVA
Case Dimastech v2.5 Easy
Audio Device(s) Onboard
Power Supply Antec 1.2kW HCP
Mouse Logitech G403 - Wired
Keyboard Rosewill - MX Browns
Software Windows 10 Pro x64
#1
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
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#2
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
532 (0.15/day)
Likes
162
Location
Lincoln, Nebraska, USA
System Name This PC
Processor Intel Core i7 2600K @ 4.8 GHz
Motherboard ASUS P8Z68 Deluxe
Cooling Custom Water Cooling - CPU Only
Memory 8GB (2 x 4GB) AData DDR3
Video Card(s) GTX 1080 TI - EVGA FTW3 - Slave BIOS
Storage Corsair Force GT 128GB / Samsung 850 Pro 512 GB
Display(s) AOC AGON AG271QG - 165 Hz - AHVA
Case Dimastech v2.5 Easy
Audio Device(s) Onboard
Power Supply Antec 1.2kW HCP
Mouse Logitech G403 - Wired
Keyboard Rosewill - MX Browns
Software Windows 10 Pro x64
#3
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
Staff member
Joined
Feb 6, 2007
Messages
13,817 (3.48/day)
Likes
5,524
Location
Cheeseland (Wisconsin, USA)
Processor Intel Core 2 Quad QX9650 Extreme @ 3.0 GHz
Motherboard Asus Rampage Formula
Cooling ZeroTherm Nirvana NV120 Premium
Memory 8GB (4 x 2GB) Corsair Dominator PC2-8500
Video Card(s) 2 x Sapphire Radeon HD6970
Storage 2 x Seagate Barracuda 320GB in RAID 0
Display(s) Dell 3007WFP 30" LCD (2560 x 1600)
Case Thermaltake Armor w/ 250mm Side Fan
Audio Device(s) SupremeFX 8ch Audio
Power Supply Thermaltake Toughpower 750W Modular
Software Win8 Pro x64 / Cat 12.10
#4
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
532 (0.15/day)
Likes
162
Location
Lincoln, Nebraska, USA
System Name This PC
Processor Intel Core i7 2600K @ 4.8 GHz
Motherboard ASUS P8Z68 Deluxe
Cooling Custom Water Cooling - CPU Only
Memory 8GB (2 x 4GB) AData DDR3
Video Card(s) GTX 1080 TI - EVGA FTW3 - Slave BIOS
Storage Corsair Force GT 128GB / Samsung 850 Pro 512 GB
Display(s) AOC AGON AG271QG - 165 Hz - AHVA
Case Dimastech v2.5 Easy
Audio Device(s) Onboard
Power Supply Antec 1.2kW HCP
Mouse Logitech G403 - Wired
Keyboard Rosewill - MX Browns
Software Windows 10 Pro x64
#5
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: