Thread: stuck on array

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    stuck on array

    I need a little guidence with this array problem I am working on. I need to read two positive integers as char arrays and then output the sum of those numbers as another array. I also need to change the characters the user inputs to integers and I am not sure if I even did that right. I have already done the begining of the code but do not know how I would add the the two integers as arrays together and then store them in another array. If I simply added the elements from each array together and stored the numbers one by one that wouldn't work.
    For example if the last number of the first array was 9 and the last number of the second array was 3 then 9 + 3 = 12 but you can't have that because the 1 needs to be caried to the next number and the 2 needs to stay in that slot.
    This is my code so far, it is unfinished!

    Code:
    include # <iostream>
    using namespace std.
    
    int main ()
    {
    
    char FirstNumber [20];
    char SecondNumer [20];
    int Idx;
    int Idx2;
    int Num1 [20];
    int Num2 [20];
    
    for ( Idx = 0; Idx < 20; Idx++)
    {
         cout<<”Enter the first number, less than 20 digits long, please”;
         cin>>FirstNumber [Idx];
         int Num1 [Idx] = char [Idx] – ‘0’;	
    
    }
    
    for (Idx2 = 0; Idx2 < 20; Idx2++)
    {
         cout<<”Enter your second number, less than 20 digits long, please”;
         cin>>SencondNumber [Idx2];
         int Num2 [Idx2] = char [Idx2] – ‘0’;
    }
    Any suggestion would help, thanks!

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Could you perhaps try to explain what you're trying to do a bit more clearly?

    I'm not 100&#37; sure what you are trying to do but i think if i knew i could help ya out


    Good luck! =D
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Ok let me see here...

    You want the user to enter a 20 (or less than that) digit number, 2 of them.

    Okay, and you're storing them in a character array, i'm not really getting this, haha, but i'll work with you.

    Then you want to add the two together?

    Well here's what you need so far, if you're trying to store the numbers in Character Arrays, just 2 numbers, here's how you do it:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	//Variables
    	char firstNumber[20];
    	char secondNumber[20];
    
    	int idx;
    	int idx2;
    
    	int num1[20];
    	int num2[20];
    
    	//Get Information
    	cout<<"Enter the First Number, Less Than 20 Digits Long: ";
    	cin.getline(firstNumber, 20);
    
    	cout<<"Enter the Second Number, Less Than 20 Digits Long: ";
    	cin.getline(secondNumber, 20);
    
    	return 0;
    }
    What you were doing before was storing 20 different things in the char array, instead of just putting the one thing in there, get what i'm saying?
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Have you compiled the code? Always compile as you go, writing a little bit of code at a time.

    >> cin>>FirstNumber [Idx];
    You are running this input code inside the for loop, meaning it will run 20 times. Is that what you want? Or maybe you just want to input the number once as an entire string before the loop?

    >> int Num1 [Idx] = char [Idx] – ‘0’;
    This is almost correct. Here, your use char[Idx] is wrong. You need to use the array name, not the char type.

    >> If I simply added the elements from each array together and stored the numbers one by one that wouldn't work.
    If the result is 10 or more, then subtract 10 and add 1 to the next spot over. If you read both strings in first before adding, then you can do this more easily.

    Finally, in your posted code there are some weird quote characters like you wrote the code in Word or something. If that's the case, you should write the code in a plain text editor or an IDE. Your code might get compile errors due to the weird quote characters. (Instead of ‘ and ’ it should be ' and instead of ” it should be ").

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I'm not too familiar with C++-style strings but I'm guessing that if you used them instead of C-style strings, you could get rid of the 20-digit length limit with no additional difficulty.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    32

    okay

    I do want to have all the numbers seperate so that I can add them one by one. Once I get the numbers as characters and change them to integers how can I add each one together. What if the first one is 125 and the second one is 1235? They are not the same amount of numbers so how can I ensure that they add correctly? I was thinking of reversing the elements so that I can add them together easier starting from the beginning (otherwise the end) of the number. Another question: how do I add the elements together without knowing how many elements the user input, in other words how do I know where in the array to begin adding numbers?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think reversing them might be a good idea. At the very least you need to know how long each number is. Your current code loops through all 20 elements in the character array, but a lot of those won't be used if the number is smaller. Perhaps use strlen or a loop to find out how long the two character arrays are. Then you know your result will be no bigger than 1 bigger than the largest size. If you then start from the end of each string and loop to the front, you can add and carry and match up the correct digits.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-12-2008, 11:25 AM
  2. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 07:14 PM
  3. string array stuck:(
    By mass in forum C Programming
    Replies: 18
    Last Post: 05-22-2006, 04:44 PM
  4. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  5. Replies: 3
    Last Post: 06-11-2002, 12:57 PM