C++ string confusion - c++

I would like to say thanks for your patience to before hand. I am just starting to learn how to code with some background knowledge from a 101 cs course I took beforehand, so I may be making some fundamental mistakes. Here is the question I am trying to solve:
Find the greatest product of five consecutive digits in the 1000-digit number.
73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450
I will write the error I am receiving first:
I get the following error when I call the findmax function that I've created:
error: could not conver '(std::string*)(&arr)' from 'std::string* {aka
std::basic_st...}
And here is my code I've written so far:
#include <iostream>
#include <string>
using namespace std;
void findmax(string test);
int main()
{
int assignNum = 0;
int n2;
string p = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
string arr[995];
string plast = "";
//FOR LOOP TO LIST ALL CONSECUTIVE INTEGERS OF 5 DIGITS
for (assignNum = 0; assignNum < 995; assignNum++) { //Sliding up initial digit
plast = "";
for (int j =0; j<5; j++) {
plast = plast + p[assignNum + j];
}
arr[assignNum]= plast;
}
cout << sizeof(arr)/sizeof(arr[0]) << endl;
findmax(arr); // THE ERROR REFERS TO HERE ******!!!!!!
}
//FIND MAX FUNCTION USING RECURSION
void findmax(string test) {
string right="";
int k;
int stringSize = sizeof(test)/sizeof(test[0]);
int middle = (sizeof(test)/sizeof(test[0]))/2;
for (k=0; k<=stringSize; k++){
if (test[k] >= test[middle]){
right = right + test[k];
}
else {
cout << "do nothing " << endl;
}
if (stringSize != 2){
findmax(right);
}
else {
if (test[0] >= test[1]){
cout << test[0]<< endl;
break;
}
else {
cout << test[1] << endl;
break;
}
}
}
}
Any help would be greatly appreciated, I've been working on this for quite a while and really want it to work.
For anyone interested, I solved the problem, which I actually misread at the start. Here is my code (without trying to use recursion):
#include <iostream>
#include <string>
#include <stdio.h> /* printf, fgets */
#include <stdlib.h> /* atoi */
#include <math.h>
using namespace std;
void findmax(string test[]);
int main()
{
int assignNum = 0;
int n2;
int ntp;
int ltp = 0;
string p = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
string pnew = "";
//FOR LOOP TO LIST ALL CONSECUTIVE INTEGERS OF 5 DIGITS
for (assignNum = 0; assignNum < 995; assignNum++){ //Sliding up initial digit
pnew = "";
for (int j =0; j<5; j++){
pnew = pnew + p[assignNum + j];
}
string myString = pnew;
int value = atoi(myString.c_str()); //value = 45
ntp = ((value / 10000) % 10) *((value / 1000) % 10) * ((value /100) % 10) * ((value / 10)% 10) * ((value / 1) % 10);
cout << (value / 10000) % 10 << endl;
cout << (value / 1000) % 10 << endl;
cout << (value / 100) % 10 << endl;
cout << (value / 10) % 10 << endl;
cout << (value / 1) % 10 << endl;
cout << "this is ntp: " << ntp << endl;
if (ltp>=ntp){
cout << "ltp: " << ltp << "is >= ntp: " << ntp << endl;
}
else {
cout << "ntp: " << ntp << "is > ltp: " << ltp << endl;
ltp=ntp;
}
cout << ltp << endl;
}
cout << "THE LARGEST CONSECUTIVE 5 DIGIT PRODUC IS: " << ltp << endl;
}
Answer: 40824
execution time: .986s

findmax expects a string, but you're feeding it a string [].
You can fix the prototype and the signature:
void findmax(string test[]);
void findmax(string test[]) {
But you do it in another location:
string right="";
// ...
findmax(right); // right is not an array
I suspect your intention is something like this:
std::string res = ""; // build string out of array
for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i++)
res += arr[i];
findmax(res);
But I get a segmentation fault, so there is something wrong with your algorithm regardless.

p is one std::string variable that is instantiated using the (const char *) value.
arr is an array of std::string that can hold up to 995 independent std::string values. findmax is a function returning void that accepts a copy of std::string value. So when you try passing arr to findmax, the compiler which only knows about a findmax that accepts std::string gets confused on being invoked with an array of std::string.
You are probably getting confused with the std::string being initialized with a char [] or char *. 73167176531330624919225 ... can either be treated as a char arr[995] or a char *arr with the appropriate code modifications so that when findmax is invoked, a std::string object gets constructed.
PS: Also, when passing around non-pod values to functions you are better off passing them as a reference as to avoid unnecessary copying:
void findmax(std::string & test) // If you want to manipulate value in function
OR
void findmax(const std::string & test) // To avoid surprises in client code

Related

C++ taking an array of Fahrenheit temperatures, converting them to Celsius, then adding the Celsius temperatures to a new array

Hi so this is my first time on Stackoverflow so I apologize in advance if I'm not to clear with what I'm asking. I have an assignment for class to take an array of temperatures in Fahrenheit, convert them to Celsius, and then place those converted numbers into another array of the same size. My issue seems to be with memory locations and getting the actual value from the Fahrenheit array and performing the arithmetic to convert that value. I've searched for a while to try and find more information on this because it seems pretty basic but for some reason cannot find any help, thus my reasoning for joining Stackoverflow. Any help or suggestions (including those telling me to just blow my brains out) or links to tutorials would be greatly appreciated.
#include <string>
#include <iostream>
using namespace std;
void toCelsius(double fahrenheitTemps[],double *celsiusTemps,int n){
for (int i = 0; i < n; i++) {
*(celsiusTemps + i) = (((*(fahrenheitTemps + i)) - 32)*(5 / 9));
cout << ((*(fahrenheitTemps + i) - 32)*(1.8)) << endl;
}
}
void PrintResults(double tF[], double tC[], int n) {
cout << "Fahrenheit Temps: ";
for (int i = 0; i < n; i++) {
cout << tF[i] << " ";
}
cout << endl;
cout << "Celsius Temps: ";
for (int i = 0; i < n; i++) {
cout << tF[i] << " "; //This was my main issue and mistake (it should be tC) view update to see corrected code
}
cout << endl;
}
int main(){
double tempF[] = { 33.5,67.5,67.5,88.0,46.0,94.5,77.5,83.0,95.0,80.5 }, tempC[10];
double * ptrTempF, * ptrTempC;
*ptrTempF = &tempF;
ptrTempC = tempC;
for (int i = 0; i < 10; i++) {
*(tempC + i) = (*(tempF + i) - 32)*(5 / 9);
cout << tempC << " " << tempC[i] << endl;
}
toCelsius(tempF, tempC, 10);
PrintResults(tempF, tempC, 10);
}
Update: I'm just a complete moron everything you guys suggested genuinely fixed some of the bug problems I was having. However the reason the array tempC looked like it wasn't changing in the output was because I put in the function printResults cout << tF[i] << " "; where cout << tC[i] << " "; should have been. My output looked made it look like nothing was working since when it was.
This is the working form of the code just in case anyone googles this and wants to see the working code:
#include <string>
#include <iostream>
using namespace std;
void toCelsius(double fahrenheitTemps[],double * &celsiusTemps,int n){ //I think & made a difference
for (int i = 0; i < n; i++) {
celsiusTemps[i] = ((fahrenheitTemps[i] - 32.0)*(5.0 / 9.0));
}
}
void PrintResults(double tF[], double tC[], int n) {
cout << "Fahrenheit Temps: ";
for (int i = 0; i < n; i++) {
cout << tF[i] << " ";
}
cout << endl;
cout << "Celsius Temps: ";
for (int i = 0; i < n; i++) {
cout << tC[i] << " ";
}
cout << endl;
}
int main(){
double tempF[] = { 33.5,67.5,67.5,88.0,46.0,94.5,77.5,83.0,95.0,80.5 }, tempC[10];
double * ptrTempC;
ptrTempC = tempC;
toCelsius(tempF, ptrTempC, 10);
PrintResults(tempF, tempC, 10);
}
double * ptrTempF, * ptrTempC;
*ptrTempF = &tempF;
ptrTempf is a pointer to a double. Therefore *ptrTempF is a double.
Setting aside that this is undefined behavior, dereferencing an uninitalized pointer, this assigment attempts to assign a pointer to a double array to a double. That, of course, makes no sense. The assignment should really be:
ptrTempF = tempF;
The second bug:
*(celsiusTemps + i) = (((*(fahrenheitTemps + i)) - 32)*(5 / 9));
"5/9" is integer division, which calculates to 0. You need to make it a double division:
*(celsiusTemps + i) = (((*(fahrenheitTemps + i)) - 32)*(5.0 / 9));
5 / 9
You're dividing int by int here, and the result is also an int, rounded down. In other words instead of typing (5 / 9) you could have typed 0. Change it to
5.0 / 9.0
By the way, for pointers *(celsiusTemps + i) is exactly the same as celsiusTemps[i], but it cuts down on the parentheses and makes your code more readable.

null terminated char array (c-string) not printing without for loop

**I can't use vectors, or any functions from the standard library with this program. Hence why I am writing it all myself.
Alright, this program is just about finished. All my user defined functions are working fine, except for the reverseCString function. When I run the program, and enter a string of "hello", I can select menu option "rev" to reverse the string. My main function then calls the reverseCString function, and uses the for loop in my main to print out my reversed c-string. The program works fine at this point, until the do-while loop continues..
After the rv function is called though, the program loops to continue to allow the user to modify their string, as it should. However, my c-string vanishes at this point. I can still operate on it if I use the other commands/funtions, but the c-string doesn't print to cout.
I don't understnad what is causing this, but I have isolated the problem down to the reverseCString function.
Here is my code so far:
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<string>
#include<math.h>
using namespace std;
void shiftLeft(char szString[], size_t shiftBy)
{
const char *p = szString;//
while (*p) ++p;//advance p to point to the the null terminator, found via personal research at http://stackoverflow.com/questions/12201815/what-difference-between-whilepp-while-p-and-whilep
size_t len = p - szString;//len is set to the size of our c-string
if (len > 1 && (shiftBy %= len))
{
char *ends[] = { szString+shiftBy, szString+len, szString+(len - shiftBy) };//create a temporary array for storage
for (size_t i = 0; i < 3; ++i)//use a for loop to flip the first three character
{
char *start = szString, *end = ends[i];
while (start < --end)//now flip the rest of the characters
{
char ch = *start;
*start++ = *end;
*end = ch;
}
}
}
}
void shiftRight (char szString[], int size, int shiftBy)
{
if(shiftBy > size){
shiftBy = shiftBy - size;
}
if(size == 1){
//do nothing, exit function with no change made to myarray
}
else{
char temp;
//for loop to print the array with indexes moved up (to the right) --> by 2
for (int i=0; i < size; i++)
{//EXAMPLE shift by 3 for a c-string of 5
temp = szString[size-shiftBy];//temp = myarray[2]
szString[size-shiftBy] = szString[i];//myarray[2] = myarray[i]
szString[i] = temp;//myarray[i] = temp(value previously at index 2)
}
}
}
void reverseCString(char szString[], const size_t& size){
char temp;
int i = 0;
int j = size-1;
//we can use a simple tep variable to flip everything
while(i < j)
{
temp = szString[i];
szString[i] = szString[j];
szString[j] = temp;
i++;
j--;
}
}
int main(){
string repeat = "";
string userInputString;
cout << "Please eneter your string: " << endl;
//cin >> ws;
getline(cin,userInputString);
char * szString = new char [userInputString.length()+1];
strcpy (szString, userInputString.c_str());
do {
cout << "Your c-string is: " << szString << endl;
string commandString = "";
cout << "Please enter a command: ";
getline(cin,commandString);
if (commandString[0] == 'L'){
cout << "You have chosen to shift your string left by " << commandString[1] << " characters." << endl;
const char * shiftLeftPtr = &commandString[1];
//convert this value to an int for our function
int shiftLeftBy = atoi(shiftLeftPtr);
//run our shifleft function
shiftLeft(szString,shiftLeftBy);
//print the results
cout << "Your c-string, shifted left by " << commandString[1] << endl;
cout << szString;
}
if (commandString[0] == 'R'){
cout << "You have chosen to shift your string right by " << commandString[1] << " characters." << endl;
const char * shiftRightPtr = &commandString[1];
//convert this value to an int for our function
int shiftRightBy = atoi(shiftRightPtr);
//run our shiftright function
shiftRight(szString,userInputString.length(),shiftRightBy);
//print the results
cout << "Your c-string, shifted right by " << commandString[1] << endl;
cout << szString;
}
if (commandString.compare("rev") == 0){
cout << "You have chosen to reverse your string. " << endl;
//run our reverseArray function
reverseCString(szString,userInputString.length()+1);
cout << "Your c-string, reversed: ";
for(int i = 0; i < userInputString.length()+1; i++){
///////////////////////////right her seems to be my issue
cout << szString[i];
}
}
if (!(commandString[0] == 'R' || commandString[0] == 'L' || commandString.compare("rev") == 0)){
cout << "You have entered an invalid selection." << endl;
}
cout << "\nEnter 'quit' to close the program, anything else to continue: ";
getline(cin,repeat);
}while(repeat.compare("quit") != 0);
return 0;
}
Your length logic is broken. Say the string contains "bar\0". You do this:
reverseCString(szString,userInputString.length()+1);
Now, length is 3, and you pass 4 to reverseCString. Then this happens:
void reverseCString(char szString[], const size_t& size){
char temp;
int i = 0;
int j = size-1;
Now, i is 0 and j is 3. So you swap items 0 and 3. Well, what are the items?
0 = b
1 = a
2 = r
3 = \0
When you swap items 0 and 3, you create a string that starts with a terminator, "\0rab". Of course printing that out won't produce anything.

Mastermind string cout issue

Ok I have been struggling with this code and I think I have it written out right but here is the rules from my teacher
1 = implies right Number, Right Place.
2 = implies right Number, Wrong Place.
0 = implies Wrong Number.
So the computer decides on 12345; the user guesses 11235; the computer should respond with 10221. Hint: Watch out for a double number like 11 when there is only one.
I have it where it does all of that except I can not get it to show a 0 when it is wrong can you please help me every single part is written except that part here is my code
// Programming 2
// Mastermind
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
struct fields{//the list of variables used in my program
int size = 5;;
int range = 9;
char lowest = '0';
string guess;
string answer;
int number;
int correct;
int position;
bool gameover = false;
};
void gameplay(fields & info);//declaring the function
int main()
{
fields game;
gameplay(game);//calling the function
system("pause");
return 0;
}
void gameplay(fields & info){//calling the structure into the function
srand(time(0));//to randomize number
info.answer = "";//getting the number
for (int i = 0; i < info.size; i++)
{
char ch = info.lowest + rand() % info.range;
info.answer += ch;
}
info.number = 1;
info.correct = 0;
info.position = 0;
while (!info.gameover)//using a while loop to let them go until they guess it
{
cout << "Guess #" << info.number << ": Enter 5 numbers that are '0' through '9': ";//asking them to guess
cout << info.answer;
cout << "\n";
cin >> info.guess;
if (info.guess == info.answer)//if the guess is right this will end the game
{
cout << "Right! It took you " << info.number << " move";
if (info.number != 1) cout << "s";
cout << "." << endl;
info.gameover = true;
}
int correctNumbers = 0;
for (char const &ch : info.guess) //seeing if there are numebrs in the guess that is in the answer
{
if (info.answer.find(ch) != string::npos)
{
++correctNumbers;
}
}
int const digits = 5;
int correctPositions = 0;
int correctPosition[digits];
int test = 0;
for (int i = 0; i < digits; ++i)//telling which numbers is correct and displaying the 2 or 0 for number is correct or number is wrong
{
if (info.answer[i] == info.guess[i])
{
++correctPositions;
}
if (info.answer[i] == info.guess[i]){
correctPosition[i] = 2;
cout << correctPosition[i];
}
if (correctPosition[i] != 2){
correctPosition[i] = 1;
cout << correctPosition[i];
}
if (correctPosition[i] != 2 && correctPosition[i] != 1)){
correctPosition[i] = 0;
cout << correctPosition[i];
}
}
cout << "\nYou have " << correctPositions << " numbers in the correct position " <<endl;
cout << "You have " << correctNumbers <<" correct numbers in the wrong position"<< endl;
}
cout << "GAME OVER\n\n";
}

Having trouble with converting a decimal number to a binary

I'm making a program that converts a string that the user enters such as "APPLE" into a binary number through the corresponding ASCII numbers that represent each character of the string "APPLE." For example A = 65 in ascii etc.. I've created a function that converts the string into a binary but it doesn't seem to be working. It displays "The equivalent binary number is: 0031F240for A" in an infinite loop and gives me "0031F240for" instead of being in the binary version of 65. I know this function works for converting a decimal number into binary because I've tried it, but I think my implementation of the bin[] array is messing things up. Any help would be appreciated.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <fstream>
using namespace std;
class RandomString
{
private:
string input;
string bin[100];
public:
RandomString() : bin(), input("")
{
}
void getData()
{
cout << "Enter the word to be encoded into a binary file.";
cin >> input;
}
void numToBin()
{
int i = 0;
int len = input.length();
int num = int(input[i]);
for(int i = 0; i < len; i++)
{
while(num != 0)
{
if (num % 2 == 0)
bin[i].insert(0, "0");
else
bin[i].insert(0, "1");
num = num / 2;
cout << "The equivalent binary number is: " << bin << "for " << input[i] << endl;
}
}
}
void display()
{
}
};
I haven't test if the result is correct but this code convert a string to binary. Probably you have to modify it to fit with ASCII codes.
void DecimalToBinary(char a,std::vector<char>& v)
{
if(a==0)
v.push_back(0);
if(a==1)
v.push_back(1);
else
{
v.push_back(a%2);
DecimalToBinary(a/2,v);
}
}
int main()
{
std::vector<char> v;
std::string line;
getline(std::cin,line);
std::istringstream input(line);
char c;
while(input >> c)
{
DecimalToBinary(c,v);
}
std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout,""));
}
First Your while loop never stops because you don't change the value of i inside the while loop, so int(input[i]) has always the same value, you have to use break somewhere or i++, but I don't know if the result is correct,I think recursion is better than while in this situation, but anyway try the following:
void numToBin()
{
int i = 0;
int len = input.length();
int num = int(input[i]);
for(int i = 0; i < len; i++)
{
while(int(input[i]) != 0)
{
if (num % 2 == 0)
{
bin[i].insert(0, "0");
break;
}
else
{
bin[i].insert(0, "1");
num = num / 2;
}
cout << "The equivalent binary number is: " << bin << "for " << input[i] << endl;
}
}
}
Second, doing std::cout << bin you print a memory address, not the contents of the bin.
while(int(input[i]) != 0)
{
if (num % 2 == 0)
bin[i].insert(0, "0");
else
{
bin[i].insert(0, "1");
}
num = num / 2;// this line should be in both case.
cout << "The equivalent binary number is: " << bin << "for " << input[i] << endl;
}
I've changed num = num / 2 for both cases. Please check it.
You may want to change the 'bin' in
cout << "The equivalent binary number is: " << bin
to 'bin[i]'.
Because 'bin' is a string array, also the pointer/address to the string array, so 'cout << bin' will always output the address.

Stuck with my solution to #4 of Project Euler

I am stuck with my solution to problem 4 of Project Euler, I have the following code which should work and it does iterate through the solution to the problem as I have researched and discovered (993*913):
// Michael Clover
// Project Euler
// Problem 4
/* A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
Find the largest palindrome made from the product of two 3-digit numbers. */
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
using namespace std;
bool achieved = false; // change to true when the palindrome is found
string string_conversion = "";
string first_half = ""; // first half of string
string second_half = ""; // second half of string
stringstream conversion; // use this to convert integers to strings
int check(string first_half, string second_half) {
if (first_half.compare(second_half) == 0) {
achieved = true;
return 0;
}
else {
return 0;
}
return 0;
}
int convert(int result) {
char temp;
conversion << result;
conversion >> string_conversion;
if (string_conversion.size() == 6) {
temp = string_conversion.at(0);
//cout << "temp = " << temp << endl;
first_half+=(temp);
temp = string_conversion.at(1);
//cout << "temp = " << temp << endl;
first_half+=(temp);
temp = string_conversion.at(2);
//cout << "temp = " << temp << endl;
first_half+=(temp);
temp = string_conversion.at(5);
//cout << "temp = " << temp << endl;
second_half+=(temp);
temp = string_conversion.at(4);
//cout << "temp = " << temp << endl;
second_half+=(temp);
temp = string_conversion.at(3);
//cout << "temp = " << temp << endl;
second_half+=(temp);
//cout << first_half << second_half << endl;
check(first_half, second_half);
}
//if (string_conversion.size() == 5) {
//cout << "The size of the string is 5" << endl;
//exit(1);
//}
string_conversion = "";
cout << first_half << endl;
cout << second_half << endl;
first_half.clear();
second_half.clear();
conversion.clear();
conversion.str("");
//cout << "conversion: " << conversion << endl;
return 0;
}
int iterate(int operator_one, int operator_two) { // takes two numbers and iterates through them, each time it is iterated, the result is passed to the convert function to convert to string
int two = operator_two;
for (int i = operator_one; i > 100; i--) {
int result = i * two;
cout << i << "x" << two << endl;
convert(result);
}
return 0;
}
int main() { // Use the stringstream to convert the numerical values into strings which you can then use to check if they are palindromes
int operator_one = 999;
int operator_two = 999;
while (achieved == false) {
for (int i = operator_two; i > 100; i--) {
iterate(999, i);
}
}
cout << "The largest palindrome made from the product of two 3-digit numbers is: " << string_conversion << endl;
return 0;
}
This program iterates through all of the numbers through 999x999 downwards and then splits the 6-digit numbers into two strings with the second half of the result being arranged from back to front. As shown in the console using cout << during runtime, the program tries 993*913 and both the second_half string and the first_half string contain 906. What I thought the program should then do is perform the check(string first_half, string second_half) function after iterating this and decide that both of the strings match (which should according to various sources return 0) this should then initiate the if statement within check() and set the boolean achieved to true ending the program within the main statement and printing the result before exiting the program. It does not do this however and this is my problem. Thank you for any and all help.
Leaving aside some other issues I see in this code, I think your termination problem is because you only check achieved after the outer loop in main() is finished. So the inner loop (iterate()) will continue until operator_one falls below 100 and then the outer loop will continue until operator_two falls below 100 before you actually check your termination condition.