Help with Arrays???!!

Hey everyone!

I can NOT seem to get this thing to work correctly. I have tried and tried, but just keep getting errors. Can someone PLEASE help me figure out what I am doing wrong??!!??!!!

Here is what I need to do:

" Hexadecimal numerals are integers written in base 16. The 16 digits used are '0' through '9' plus 'a' for the "digit 10", 'b' for the "digit 11", 'c' for the "digit 12", 'd' for the "digit 13", 'e' for the "digit 14", and 'f' for the "digit 15". For example, the hexadecimal numeral d is the same as base 10 numeral 13 and the hexadecimal numeral 1d is the same as the base 10 numeral 29. Write a C++ program to perform addition of two hexadecimal numerals each with up to 10 digits. If the result of the addition is more than 10 digits long, then simple give the output message 'Addition Overflow" and not the result of the addition. Use arrays to store hexadecimal numerals as arrays of characters. Include a loop to repeat this calculation for new numbers until the user says he or she wants to end the program. "

Here is my current code which is NOT working:


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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
 const int a = 10, A =10, b = 11,B = 11,c = 12, C = 12, d =13, D =13, e = 14, E = 14, f = 15 , F = 15;
 const int size = 10;
 char chose;
 double Decimal,Hex,counter = 0;

 char ar[size];

 for ( counter ; counter < size ; counter ++ )
 {
  cout << "Enter Your Hexa-Decimal Number : ";
  cin >> Hex;
  counter = Hex;
  cout << "\nDo You Want to show the addition\n Hexa-Decimal number in Decimal <y/n> : ";
  cin >> chose;
  if ( chose == 'y' || chose == 'Y' )
  {
   if ( counter == 0 )
   {
    Decimal = ( counter * ( pow(16,0) ) );
   }
   else if ( counter == 1 )
   {
    Decimal = ( ( counter * ( pow(16,1) ) ) + ( counter * ( pow(16,0) ) ) );
   }
   else if ( counter == 2 )
   {
    Decimal = ( ( counter * ( pow(16,2) ) ) + ( counter * ( pow(16,1) ) ) + ( counter * ( pow(16,0) ) ) );
   }
   else if ( counter == 3 )
   {
    Decimal = ( ( counter * ( pow(16,3) ) ) + ( counter * ( pow(16,2) ) ) + ( counter * ( pow(16,1) ) ) + ( counter * ( pow(16,0) ) ) );
   }
   else if ( counter == 4 )
   {
    Decimal = ( ( counter * ( pow(16,4) ) ) + ( counter * ( pow(16,3) ) ) + ( counter * ( pow(16,2) ) ) + ( counter * ( pow(16,1) ) ) + ( counter * ( pow(16,0) ) ) );
   }
   else if ( counter == 5 )
   {
    Decimal = ( ( counter * ( pow(16,5) ) ) + ( counter * ( pow(16,4) ) ) + ( counter * ( pow(16,3) ) ) + ( counter * ( pow(16,2) ) ) + ( counter * ( pow(16,1) ) ) + ( counter * ( pow(16,0) ) ) );
   }
   else if ( counter == 6 )
   {
    Decimal = ( ( counter * ( pow(16,6) ) ) + ( counter * ( pow(16,5) ) ) + ( counter * ( pow(16,4) ) ) + ( counter * ( pow(16,3) ) ) + ( counter * ( pow(16,2) ) ) + ( counter * ( pow(16,1) ) ) + ( counter * ( pow(16,0) ) ) );
   }
   else if ( counter == 7 )
   {
    Decimal = ( ( counter * ( pow(16,7) ) ) + ( counter * ( pow(16,6) ) ) + ( counter * ( pow(16,5) ) ) + ( counter * ( pow(16,4) ) ) + ( counter * ( pow(16,3) ) ) + ( counter * ( pow(16,2) ) ) + ( counter * ( pow(16,1) ) ) + ( counter * ( pow(16,0) ) ) );   }
   else if ( counter == 8 )
   {
    Decimal = ( ( counter * ( pow(16,8) ) ) + ( counter * ( pow(16,7) ) ) + ( counter * ( pow(16,6) ) ) + ( counter * ( pow(16,5) ) ) + ( counter * ( pow(16,4) ) ) + ( counter * ( pow(16,3) ) ) + ( counter * ( pow(16,2) ) ) + ( counter * ( pow(16,1) ) ) + ( counter * ( pow(16,0) ) ) );
   }
   else if ( counter == 9 )
   {
    Decimal = ( ( counter * ( pow(16,9) ) ) + ( counter * ( pow(16,8) ) ) + ( counter * ( pow(16,7) ) ) + ( counter * ( pow(16,6) ) ) + ( counter * ( pow(16,5) ) ) + ( counter * ( pow(16,4) ) ) + ( counter * ( pow(16,3) ) ) + ( counter * ( pow(16,2) ) ) + ( counter * ( pow(16,1) ) ) + ( counter * ( pow(16,0) ) ) );
   }
   else
   {
    cout << "Addition Overflow\n";
    break;
   }
   
  }
  else
  {
   cout << counter << "in Hexa-Decimal =  " << Decimal << "  in Decimal\n";
   break;
  }
 }

 return 0;
 
It would be helpful to know what errors you are experiencing (are they crashes or bad output?) and under what conditions they happen.

From the code, it looks like you are just starting the assignment so I'm not exactly sure what this segment of code is trying to accomplish. But this code snippet does look suspicious:
1
2
3
4
5
for ( counter ; counter < size ; counter ++ )
{
  cout << "Enter Your Hexa-Decimal Number : ";
  cin >> Hex;
  counter = Hex;

The counter variable is used as an end condition for the for loop and it's also being reassigned to the input from the command line while the loop is running...

Also, beware of statements like: double Decimal,Hex,counter = 0;. It doesn't do what you might expect - only counter is set to 0 while Decimal and Hex are uninitialized. Instead initialize each on its own line. But this isn't causing an error in this case because those variable are set from the command line before being used.
Hi Dragonscrapper,

You will never get the code right until you understand what needs to be done. Understanding the text of the question is vital to producing a working program. that is the short answer to " help me figure out what I am doing wrong?"

Lets take a look and break it down...

const int a =10 .... --> you never use any of these variables so ditch them.

for ( counter ; counter < size ; counter ++ ) --> you expect this loop to run for 10 iterations? and ask the user to enter a hex number inside the loop, that's 10 numbers you're asking for, not the 2 that the question requires. Also, counter needs a starting value, like for(counter=0;counter<size;counter++)

cin >> Hex --> The question required you to accept a string of up to 10 chars from the user, you are expecting one double.
counter = Hex; --> counter is supposed to be counting from 1 to 10 (0 to 9), assigning the user input to it will screw the count.

You ask the user if they want results in decimal or hex, thats not part of the question.
The rest of your code is trying to convert a hex number to a decimal one, thats not part of the question either.


I'm afraid its back to the drawing board for you ;)

1. break the question down in to a list of things that need to be done. Most of the question above is waffling on about what hex is, and can be ignored. The real question begins "Write a C++ program to ..."


Write a C++ program to perform addition of two hexadecimal numerals each with up to 10 digits.

1
2
3
4
5
6
cout << "Enter first hex number:";
cin >> firstHex;  // the question said this should be an array of characters
cout << Enter second hex number:";
cin >> secondHex; // the question said this should also be an array of characters

answerHex = MyHexAddingFunc(firstHex,secondHex); 


If the result of the addition is more than 10 digits long, then simple give the output message 'Addition Overflow" and not the result of the addition.

1
2
3
4
if (answerHex.length() <= 10)
    cout << answerHex;
else 
    cout << "Addition Overflow";


Use arrays to store hexadecimal numerals as arrays of characters.

1
2
3
char firstHex[50];
char secondHex[50];
char answerHex[50];


i chose 50 as a large number, just make it bigger than the longest hex that the user could type in.

Include a loop to repeat this calculation for new numbers until the user says he or she wants to end the program.

1
2
3
4
5
6
do
{
    // all of the above
    cout << "do it again?";
    cin >> response;
} while (response=="Y");


When you have got to grips with this initial program flow, and understand how it relates to the actual question, you can get to the nitty gritty of coding MyHexAddingFunc() to get some real results.

Start with pen and paper on the sofa, take it slow and really try to understand what the question is really asking for. Without that intimate knowledge you will never be able to write the code to do it, because you yourself don't know what needs to be done so how are you going to tell the computer?

Resist the temptation to add features, it just adds to the confusion, http://en.wikipedia.org/wiki/KISS_principle is something worth remembering.

It may feel like a chore now, but when it finally works you will feel great. keep at it dude.
You do know anything to the power of 0 is 1 right?
so
2 * ( 2 ^ 1 ) + 2 * ( 2 ^ 0 ) == ( 2 * 2 ) + ( 2 * 1 ) == 4 * 2 == 8
so then it would be 2^2 * 2 ^1 == 4 * 2 == 8.

We can simplify your formula to something like this
x = n^( n + 1 ) + n^( n ) + n^( n - 1 )...( while n > 2 keep taking off 1 )
n = 4.
x = 4^5 + 4^4 + 4^3 + 4^2 + 4^1
x = 1024 + 256 + 64+ 16 + 4
x = 1364

we could then write a loop instead of all the if statements then listing out each step.

*ps I am too lazy to simplify the function anymore.

1
2
3
4
5
6
temp = 0;
for( int i = n + 1; i > 0; --i )
{
    temp += pow( n , i );
}
std::cout << temp << std::endl;


I could be really off though I am extremely tired.
Topic archived. No new replies allowed.