When I enter the user inputs of “12345” and “0”, I see in the first loop arrOne is being populated the way I’m expecting 1, 2, 3, 4, 5. However after the 2nd loop (which populates arrTwo) when I go to print out the values of arrOne (using the 3rd loop) I see that the values are now 0, 2, 3, 4, 5.
I’m scratching my head as to how arrOne’s initial value has been modified after the initial loop. Appreciate any guidance!
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
// Write your main here
string inputOne = "";
string inputTwo = "";
cout << "Please enter two positive integers up to 20 digits long" << endl;
cin >> inputOne;
cin >> inputTwo;
int arrOne[inputOne.length() - 1];
int arrTwo[inputTwo.length() - 1];
for(int i = 0; i< inputOne.length(); i++){
arrOne[i] = inputOne[i] - '0';
cout<< "arrOne[i] = " << arrOne[i] << endl;
}
for(int i = 0; i< inputTwo.length(); i++){
arrTwo[i] = inputTwo[i] - '0';
cout<< "arrTwo[i] = " << arrTwo[i] << endl;
}
for(int i = 0; i< inputOne.length(); i++){
cout<< "arrOne[i] = " << arrOne[i] << endl;
}
return 0;
}
I am expecting after the population of the arrTwo array, that the arrOne array would continue to store 1, 2, 3, 4, 5.
Austin Wolff is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.