I have a problem with reversing my strings of digit 1 and 2.
Anyone can advise accordingly?
I'm trying to reverse using a tempholder1 and tempholder2 as function variables and via for loop.
I cout << digit1 << digit2; // but do not get anything at the compiler.
Edited for better efficiency
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
/* GLOBAL DECLARATIONS */
/* FUNCTIONS */
// Reverse the string
void reverseString(string&);
// Transfer all the digits in a char array to an integer array
// Add two long integers
// Print out the long integers
int main()
{
string digit1, digit2;
char again; // Y for menu
do
{
// Fetch Values
cout << "Enter a string of digits: ";
cin >> digit1;
cout << "Enter a string of digits: ";
cin >> digit2;
// Reverse the string
reverseString(digit1);
reverseString(digit2);
cout << digit1 << digit2;
// Return sum of both values
cout << "The sum is: ";
// Do it again?
cout << "Continue (Y/N)?: ";
cin >> again;
} while ((again=='y')||(again=='Y'));
}
// Reverse the string
void reverseString(string& digit)
{
string tempholder1;
// tempholder1 = reverse of digit1
int k=0;
for(int i=digit.length()-1;i>=0;i--)
{
tempholder1[k]=digit[i];
k++;
}
digit=tempholder1;
}
std::string::operator[]. It "returns a reference to the character at the given position. No bounds checking is performed". Since your strings are empty, it returns a reference to Richie knows where which is undefinied behaviour.