Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a c++ program. I have some problem is I can\'t run the third function an

ID: 3667860 • Letter: T

Question

This is a c++ program.

I have some problem is I can't run the third function and forth function. But I can correct run the first and secod function. I write a loop in if_b function, because I want user can retype at anytime. However, when user type 3 or 0, which means they want run function new_string() and function total_0, they can not going type new string or end the program. I don't know where is wrong.

This is my code:

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;


void if_b(int b,int k, int m, string &a, string &s, string &r);
void total(int ,int , int , string &, string &, string &);


void new_string(int b,int k, int m, string &a, string &s, string &r){
   if (m==3){

       cin.ignore();  
      
       cout<<"Enter New String :";
       getline(cin, a);
       total(b, k ,m, a, s, r);
  
       k=1;      
       }
}


void get_string(int b,int k, int m, string &a, string &s, string &r){
   int n=a.length();
  
   for (int i=0; i<n; i++){
       if (a[i]!=a[n-i-1]){
           b=1;
           break;
       }  
      
       else if (a[i]=a[n-i-1]){
           b=0;
       }  
   }      
   if_b(b,k,m,a,s,r);
}


void if_b(int b, int k, int m, string &a, string &s, string &r){  
  
   do{
   if(b==1){
       cout<<"This is not palindrome"<<endl;
                  
   }
      
   if (b==0){
       cout<<"This is palindrome"<<endl;
   }
  
       total(b,k,m,a,s,r);
  
   }while(k==0);
  
  
}

void replace_s(string &s, string &r){
   cin.ignore();
  
   cout<<"Please input the string which you want to search :";
   getline(cin, s);
   cout<<"Please input the string which you want ro replac: ";
   getline(cin, r);  
}


string replace_a(string &a, string &s, string r){  

   replace_s(s, r);
   if(a.find(s)!=std::string::npos){
       a.replace(a.find(s), s.length(), r);
       return a;  
   }
  
   else{
   cout<<"Invalid Inpus String !! Search String Not found ";
       return "";
   }
  
}  

void total_1(int b,int k, int m, string &a, string &s, string &r){
       if (m==1){
       k=0;
   get_string(b,k,m,a,s,r);
       }
}

void total_2(int b, int k, int m, string &a, string &s, string& r){
       if (m==2){
           k=1;
           replace_a(a, s, r);
           cout<<"Your new string is: "<<a<<endl;          
       }
       total(b,k,m,a,s,r);
}

void total_0(int b, int k, int m){

   if(m==0){
k=1;
cout<<"Bye Bye!"<<endl;
          
   }

}

void total(int b, int k, int m, string &a, string &s, string& r){  
      
       cout<<"Detemine if palindrome (1), replace string (2), enter new string(3), or quit (0) :";
       cin>>m;
                          
       total_1(b,k,m,a,s,r);          
       total_2(b,k,m,a,s,r);          
       new_string(b,k, m, a, s, r);
       total_0(b,k,m);  
  
}

int main(){

   string a, s, r;
int k=1, m, l=2, b;
  
   cout<<"Enter a String :";
getline(cin, a);
   do{
       total(b, k, m, a, s, r);
  
   }while (k==0);          
  
  
cout<<a<<endl;
      
  
  

return 0;
}

Explanation / Answer

The problem is with the total_2 function, in that function you not needed to call the total(b,k,m,a,s,r); function call in the last. Remove it or comment it out and try.

Please find the total_2 function below, with the total function call removed. Replace this fuction in the program, and it will run fine :

void total_2(int b, int k, int m, string &a, string &s, string& r){
       if (m==2){
           k=1;
           replace_a(a, s, r);
           cout<<"Your new string is: "<<a<<endl;          
       }
      }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote