6 Replies - 287 Views - Last Post: 10 May 2013 - 05:04 PM Rate Topic: -----

#1 bytemyzetta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-March 13

Hexadecimal to Decimal program giving...unexpected results.

Posted 09 May 2013 - 09:23 PM

Hey, I'm in an intro to C++ class and the last chapter we're covering is all about arrays. We have to make some sort of hexadecimal converter and I have no clue what I'm doing wrong.

 	for (int i = numberUsed1; i >= 0; i--)
{
     
   	   int h;
   	   int power = 0;
   	   int result = 0;

   	   h = a[i];
   	   a[i] = h;  	   
   	   h =  (pow(16, power)) * h;
   	   result += h;
   	   power++;
   



This is the code I use to fill the array

void number (char a[], int size, int& numberUsed)
{
	using namespace std;
	
	cout << "Enter up to " << size << " characters. Mark the end with a G \n";
	cout << "if you do not intend to use 10 characters in the hexadecimal numeral \n";
	cout << endl;
	cout << endl;
	
	char next;
	int index = 0;
	cin >> next;
	
	while ((next != 'G') && (index < size))
	{
		a[index] = next;
		index++;
		cin >> next;
		
	}

	numberUsed = index;
	
	
}




When I used a simpler version of the first piece with a self-initialized array, it seems to work fine. I have no idea what I'm doing wrong at all.
  for (int i = 0; i < 1; i++)
   {
   	   int y;
   	   int power = 1;
   	   y = a[i];
   	   a[i] = y;
   	   y = y * pow(16, power);
   	  result = y;
   	   
   	
   }
  


Is This A Good Question/Topic? 0
  • +

Replies To: Hexadecimal to Decimal program giving...unexpected results.

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Hexadecimal to Decimal program giving...unexpected results.

Posted 09 May 2013 - 09:25 PM

Move your variable declarations out of the for loop. You are resetting your variables to zero after each iteration.
Was This Post Helpful? 0
  • +
  • -

#3 bytemyzetta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 20-March 13

Re: Hexadecimal to Decimal program giving...unexpected results.

Posted 10 May 2013 - 02:12 PM

Okay, so I noticed if I initialize my array with B3AD and use this

  numberUsed1 = 4;
  power = numberUsed1 - 1;
 
 
 do
{
	int h;
	int result = hexResult1;
	
	h = hex1[i];
	hex1[i] = h;
	h = h * pow(16, power);
	result = h;
	
	hexResult1 += result;
	
	power --;
	i ++;
	
	
	
	
} while (i < 10);



I get 45997, which I think is the right decimal equivalent. I guess my problem is when the array isn't filled all the way. Any ideas how to get around that? When I input the characters, it all goes crazy.
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,227
  • Joined: 20-March 10

Re: Hexadecimal to Decimal program giving...unexpected results.

Posted 10 May 2013 - 04:14 PM

Hi,

Line 14

while ((next != 'G') && (index < size))
{


should be an OR not an AND comparison there

you are basically saying while next is not a G and index<size in other words both these condtions need to be met to exit the loop.

what you want to say is while next is not a G or index<size then exit this loop.

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,512
  • Joined: 19-February 09

Re: Hexadecimal to Decimal program giving...unexpected results.

Posted 10 May 2013 - 04:27 PM

Sorry snoopy11, I think the loop expression is ok. While next is not 'G' and index is less than size then continue with the loop.


In the other loop, you only need to loop over the number of characters present :

23	} while (i < 10);


Was This Post Helpful? 1
  • +
  • -

#6 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 765
  • View blog
  • Posts: 2,227
  • Joined: 20-March 10

Re: Hexadecimal to Decimal program giving...unexpected results.

Posted 10 May 2013 - 04:31 PM

View Post#define, on 10 May 2013 - 11:27 PM, said:

Sorry snoopy11, I think the loop expression is ok. While next is not 'G' and index is less than size then continue with the loop.


In the other loop, you only need to loop over the number of characters present :

23	} while (i < 10);



Damn,

you are right I have a heavy cold....

I have not been posting because of it I should definitely go to bed now...

Thanks

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1312
  • View blog
  • Posts: 4,512
  • Joined: 19-February 09

Re: Hexadecimal to Decimal program giving...unexpected results.

Posted 10 May 2013 - 05:04 PM

These expressions can be confusing at the best of times, get well soon.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1