10 Replies - 576 Views - Last Post: 13 May 2010 - 08:07 PM Rate Topic: -----

#1 college_trained  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 10-January 10

odd output

Posted 10 May 2010 - 12:46 PM

Hey all,

Today I was working on a program I've been working on bit-by-bit for a year or so.
What I am doing is converting a binary number to it's octal counterpart.
I received a strange output [♠♥0011]. I think it is due to the fact that previously the string variable binary was set to [110011].
Here's the code that outputs the octal number and then is supposed to reset the binary string to the octal number so it can be used in another function:
for (unsigned int i = 0; i < result1.size(); i++)
	{
		cout << result1[i];
	}
	cout << endl;
	for (unsigned int i = 0; i < result1.size(); i++)
	{		
		binary[i] = result1[i];
	}


I have tried using
binary = "";


to set the string back to nothing but I get a run-time error when I do that.
Each element in the result1 vector represents an octal number.

What I'm trying to do is set each element in the vector to the same element in the string.

Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: odd output

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: odd output

Posted 10 May 2010 - 12:50 PM

Mind showing us the entire code? It seems that you try to deal with some non-valid characters.
Was This Post Helpful? 0
  • +
  • -

#3 taiku  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 92
  • Joined: 28-October 09

Re: odd output

Posted 10 May 2010 - 12:57 PM

You say result1 is a vector... why!? use a string, man!
Was This Post Helpful? 0
  • +
  • -

#4 college_trained  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 10-January 10

Re: odd output

Posted 10 May 2010 - 02:08 PM

Complete code:
vector<int> result1;
	if((binary.size()) % 3 == 0)
	{
		int i = 0, j = 0; //set variables i = 0 and j = 0
		string *result = new (nothrow) string[binary.size() / 3]; //create new dynamic array and set the size to (binary.size() / 3)
		
		for (unsigned int h = 0; h < ((binary.size())/3); h++)//divide string in sections of 3. outputs each set.
		{
			result[j] = binary[i];
			i++;
			result[j] = result[j] + binary[i];
			i++;
			result[j] = result[j] + binary[i];
			i++;
			cout << result[j];
			j++;
		}
		cout << endl;
		for (unsigned int j = 0; j < ((binary.size())/3); j++)
		{
			if (result[j] == "000")
				result1.push_back(0);
				//cout << "0";
			else if (result[j] == "001")
				result1.push_back(1);
				//cout << "1";
			else if (result[j] == "010")
				result1.push_back(2);
				//cout << "2";
			else if (result[j] == "011")
				result1.push_back(3);
				//cout << "3";
			else if (result[j] == "100")
				result1.push_back(4);
				//cout << "4";
			else if (result[j] == "101")
				result1.push_back(5);
				//cout << "5";
			else if (result[j] == "110")
				result1.push_back(6);
				//cout << "6";
			else if (result[j] == "111")
				result1.push_back(7);
				//cout << "7";
			else
				break;
		}
		cout << endl;
	}
	else if((binary.size()) % 3 == 1)
	{
		binary = "00" + binary;
		int i = 0, j = 0;
		string *result = new (nothrow) string[binary.size() / 3];
		for (unsigned int h = 0; h < ((binary.size())/3); h++)
		{
			result[j] = binary[i];
			i++;
			result[j] = result[j] + binary[i];
			i++;
			result[j] = result[j] + binary[i];
			i++;
			cout << result[j];
			j++;
		}
		cout << endl;
		for (unsigned int j = 0; j < ((binary.size())/3); j++)
		{
			if (result[j] == "000")
				result1.push_back(0);
				//cout << "0";
			else if (result[j] == "001")
				result1.push_back(1);
				//cout << "1";
			else if (result[j] == "010")
				result1.push_back(2);
				//cout << "2";
			else if (result[j] == "011")
				result1.push_back(3);
				//cout << "3";
			else if (result[j] == "100")
				result1.push_back(4);
				//cout << "4";
			else if (result[j] == "101")
				result1.push_back(5);
				//cout << "5";
			else if (result[j] == "110")
				result1.push_back(6);
				//cout << "6";
			else if (result[j] == "111")
				result1.push_back(7);
				//cout << "7";
			else
				break;
		}
		cout << endl;
	}
	else
	{
		int i = 0, j = 0;
		binary = "0" + binary;
		string *result = new (nothrow) string[binary.size() / 3];
		for (unsigned int i = 0; i < binary.size(); i++)
		{
			result[j] = binary[i];
			i++;
			result[j] = result[j] + binary[i];
			i++;
			result[j] = result[j] + binary[i];
			i++;
			cout << result[j];
			j++;
		}
		cout << endl;
		for (unsigned int j = 0; j < ((binary.size())/3); j++)
		{
			if (result[j] == "000")
				result1.push_back(0);
				//cout << "0";
			else if (result[j] == "001")
				result1.push_back(1);
				//cout << "1";
			else if (result[j] == "010")
				result1.push_back(2);
				//cout << "2";
			else if (result[j] == "011")
				result1.push_back(3);
				//cout << "3";
			else if (result[j] == "100")
				result1.push_back(4);
				//cout << "4";
			else if (result[j] == "101")
				result1.push_back(5);
				//cout << "5";
			else if (result[j] == "110")
				result1.push_back(6);
				//cout << "6";
			else if (result[j] == "111")
				result1.push_back(7);
				//cout << "7";
			else
				break;
		}
		cout << endl;
	}
	for (unsigned int i = 0; i < result1.size(); i++)
	{
		cout << result1[i];
	}
	cout << endl;
	for (unsigned int i = 0; i < result1.size(); i++)
	{		
		binary[i] = result1[i];
	}
	cout << binary << endl;//trouble section
	cout << endl;


binary is a global variable. And the input is gotten from a separate function.
Was This Post Helpful? 0
  • +
  • -

#5 college_trained  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 10-January 10

Re: odd output

Posted 11 May 2010 - 08:03 PM

anyone have anything?
Was This Post Helpful? 0
  • +
  • -

#6 Guest_c.user*


Reputation:

Re: odd output

Posted 11 May 2010 - 11:12 PM

for (unsigned int j = 0; j < ((binary.size())/3); j++)
j++ ?
you want to scan every triplet, do j += 3
Was This Post Helpful? 0

#7 college_trained  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 10-January 10

Re: odd output

Posted 13 May 2010 - 09:17 AM

View Postc.user, on 12 May 2010 - 12:12 AM, said:

for (unsigned int j = 0; j < ((binary.size())/3); j++)
j++ ?
you want to scan every triplet, do j += 3


c.user that part is already taken care of in the previous for loop. What I'm looking for is a way to set the output from 'result1' into the string 'binary' so I can reuse the output in some other section of my program.
Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: odd output

Posted 13 May 2010 - 10:39 AM

Quote

What I'm looking for is a way to set the output from 'result1' into the string 'binary' so I can reuse the output in some other section of my program.
Ummm....pass the variable to functions as needed? Why are you trying to move data from a vector to a string? You have a variable, so use that variable directly, or pass it as needed to other functions.
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: odd output

Posted 13 May 2010 - 10:45 AM

Make your life easier. Instead of vector<int>, make result1 a string. And instead of vector::push_back() use string::append().

For example:
string result1;  // automatically initialized to empty string

//...

            if (result[j] == "000")
                result1.append("0");
//...and so on (don't forget the quotation marks)



Then you can eliminate both loops at the end of your code and simply print the string.

This post has been edited by r.stiltskin: 13 May 2010 - 10:46 AM

Was This Post Helpful? 1
  • +
  • -

#10 college_trained  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 10-January 10

Re: odd output

Posted 13 May 2010 - 11:39 AM

THANK YOU r.stiltskin! I have no idea what I was thinking using vector<int>. I changed my code up using your suggestion. I also altered the last loop because I needed 'binary' to use in the rest of my program since 'binary' is a global variable. Thank you so much for your help.

I will no doubt be back for something else that goes wrong with my program.
Was This Post Helpful? 0
  • +
  • -

#11 college_trained  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 10-January 10

Re: odd output

Posted 13 May 2010 - 08:07 PM

this thread can now be closed.

This post has been edited by college_trained: 13 May 2010 - 08:08 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1