vector with 3 elements

May 26, 2014 at 12:18pm
I wonder how does vector with 3 elements, for example:
you type 010110001111
in the code:
you have 3 positions automatically separate each well, ("010", "110", "001", "111") converts to decimal ("2", "4", "1", "7"), done for convert binary to decimal, just missing this, you get the idea? Please help me. Thank you!
May 26, 2014 at 12:42pm
Write a function that reads 3 characters consisting of '0' and '1' and returns the number that they represent in binary, or -1 on error:
1
2
3
4
5
6
7
8
9
10
11
int read3Bits()
{
    int result = 0;
    loop 3 times doing {
	read one character
	if it isn't '0' or '1' then return -1
	convert ASCII character to number 0 or 1
	result = 2*result + (number from previous step)
    }
    return result;
} 


Once you have this function, the rest should be pretty easy.
May 26, 2014 at 1:20pm
dhayden, Please, give me an example for me to understand. Thank you.
May 26, 2014 at 1:30pm
Sorry, I won't write the code for you.
May 26, 2014 at 4:53pm
Also take a look at std::bitset:
http://www.cplusplus.com/reference/bitset/bitset/
May 26, 2014 at 5:03pm
Long double main, spoke that is simple, uses only vector 3 in 3, just do not know how to use string - bitset.
May 26, 2014 at 6:54pm
closed account (2UD8vCM9)
Here is how I would approach it, but everyone has their own ways.

Didn't even user vectors, ask if there's something you don't understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

int main()
{
	while (true) //To loop all this crap
	{
		cout << "Enter a binary number where the digits are evenly divisible by 3." << endl;
		string OriginalUserInput;
		getline(cin,OriginalUserInput); //Place user input into string OriginalUserInput
		cin.clear(); // needed for getline, if you don't understand this don't worry about it. Just do it like nike.
		int SizeOfString = OriginalUserInput.size(); //Get size of original input

		if (SizeOfString % 3 != 0) //If entered number's digits aren't evenly divisible by 3
		{
			cout << "Error: Input's number of digits must be evenly divisible by 3." << endl;
		} else //If entered number's digits are evenly divible by 3
		{
			for (int i=0; i< (SizeOfString/3); i++) //For loop from 0 to digits divided by 3.
													//Ex. if 001001 is entered, this loop will run through twice since 6 digits divided by 3 = 2.
			{
				string ThreeDigitNumberToConvert; //This string will hold the three digit binary number that we will convert
				ThreeDigitNumberToConvert = OriginalUserInput.substr(3*i,3*(i+1)); //Copies 3 of the digits to the string from our input
				int ConvertedBinaryNumber = strtol(ThreeDigitNumberToConvert.c_str(),NULL,2); //Converts string to binary (Base 2)
				cout << "Conversion: " << ThreeDigitNumberToConvert << " = " << ConvertedBinaryNumber << endl; //Display
			}
		}
	}
	return 0; //End of Program
}
Last edited on May 26, 2014 at 6:54pm
Topic archived. No new replies allowed.