I've been working on this program most of the day. My professor spent a total of 30 minutes discussing arrays, so I'm very unclear on them. Here are the instructions:
You are to write a program that will add two numbers and print out the two numbers and the results. Your program should handle up to 25 digit integer numbers.
Your input for this program is found in BigNumberV2.dat
The format is one number per line. You will have several pairs of numbers to process.
Label your output.
Format of output: When you print out your answers, make sure the three numbers are right hand justified and no leading zeros.
I need help with the very beginning of this. Specifically, how do I take a line from the data file and assign each individual digit as an element of the array? I have been working on this all day (I promise), but I have deleted my code that I was working on because I got overwhelmed. Can someone please help me get started here?
Here is the data from the txt file:
6
9
46123
85321
3872663123
654321
222222222222222222222222222222
555555555556666666666666666666
999999999999999999999999999999
999999999999999999999999999999
999999999999999999999999999999
1
876876876876876876876876876876
58485949392039
I think possibly my instructor made an error in the instructions. He's prone to typos. Doing some research previously, I saw other suggestions for using the string method, but, like the person above, I don't comprehend how you convert it to an int array.
There are several ways to do it. The easiest way in my opinion.
1 2 3 4 5 6 7 8
int number[25];
string sNumber; //string number
cout << "Put in your number: "
cin >> snumber;
for(i = 0; i < sNumber.size(); ++i) {
number[i] = sNumber[i] - '0';
}
A string is basically an array of characters, so you can use string[number] to access that character in a string just like an array, and convert a character to an integer rather easily.
Now, this program assumes the number is 25 digits long. It will not work with a number that is shorter because then it will leave space in the back of the array.
To make it work with any number: Take the length of string after line 4. Then make a variable (let's just call it x) that is 25 minus the string length. In line 7, change number[i] to number[i+x]. It will start inputting the number into the array at the right position so that it lines up in the end. I will leave it up to you to fix that. :)