I just wrote this program. The test case I am using is 541. It is supposed to reverse the 541 into 154, then subtract 541 and 154, to get 396 then reverse that into 693. After that it is supposed to add 396 and 693 to get 1089. In the last three lines of code, I am trying to convert the strings into ints so I can add the last two numbers but I am getting this error. The code has to be done in this specific way for my class.
#include <iostream>
#include <string>
#include <algorithm>
/**
* main application starting point
* @return integer (0=SUCCESS)
*/
int main() {
//prompts user to input a three-digit number
std::string threeDigitNum;
std::cout << "Enter a 3-digit number, where the first and last digit differ by at least one" ;
std::cin >> threeDigitNum;
//uses a for loop to output the reverse of the first number
std::string threeReversed;
int control = 0;
for(int i = 2; i >= 0; i--){
threeReversed[control] = threeDigitNum[i];
control++;
}
std::cout << threeDigitNum[2]+ threeDigitNum[1]+ threeDigitNum[0] << std::endl;
//converts the numbers in the strings to integers
//displays the differnce of the first and second number
int firstNum = std::stoi(threeDigitNum);
int secondNum = std::stoi(threeReversed);
std::cout << firstNum - secondNum << std::endl;
std::string thirdNum;
thirdNum = std::to_string(firstNum - secondNum);
std::string thirdNumReverse;
for(int i = thirdNum.length(); i >= 0; i--){
thirdNumReverse = thirdNumReverse + thirdNum[i];
}
std::cout << thirdNumReverse << std::endl;
int fourthNum = std::stoi(thirdNum);
int fifthNum = std::stoi(thirdNumReverse);
std::cout<< fourthNum + fifthNum;
return 0;
}