Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8987093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:42:27+00:00 2026-06-15T21:42:27+00:00

I have the following code: #include <iostream> using namespace std; #include <string.h> int main(){

  • 0

I have the following code:

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

int main(){

 string track2;
 string Track2;
 string end_character("?");
 Track2={";80221000000100018A1301000000000000000?"};
 track2=Track2;

 cout<<"Track2={"<<Track2<<"}\n";

  if((track2.length()>=24 )  && (track2.find_first_not_of('0', 24) ==string::npos)){
     track2.erase (track2.begin()+24, track2.end());
  cout<<"\nTrack2={"<<track2<<"}\n";
  Track2=track2 + end_character;

  cout<<"\nTrack2={"<<Track2<<"}\n";
}
  else
     cout<<"Track2 has not more than 24 characters.\n";
}

I want to check first the length of the string track2, if this is bigger or equal to 24 character, then if the rest of character after the 24th character =0 and if the last character = ?. Then I will cut the string track2 at the begining of the 24th character until the end of track2. After that, I will insert the ? character at the end of the string track2 and printed this.
Here is the expected output:

Track2={;80221000000100018A1301000000000000000?}

Track2={;80221000000100018A13010}

Track2={;80221000000100018A13010?}

But I have a problem and I cann’t enter my if loop, when I put the character ? in my string. My program execute directely the else loop.

Could anyone help me to resolve this please?
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:42:28+00:00Added an answer on June 15, 2026 at 9:42 pm

    How about something like this:

    if (Track2.length() > 24)
    {
        auto question_mark_pos = Track2.find_first_not_of('0', 24);
    
        // If there is a character after the zeros AND
        // if that character is last AND
        // if that character is a question mark
        if (question_mark_pos != std::string::npos &&
            question_mark_pos == Track2.length() - 1 &&
            Track2[question_mark_pos] == '?')
        {
            // Get the first 24 characters of the string
            Track2 = Track2.substr(0, 24);
    
            // And append the special end character
            Track2 += end_character;
        }
    }
    

    Tested, and seems to work, here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code #include <iostream> #include <string> #include <algorithm> using namespace std; int
i have following code #include <iostream> using namespace std; int main(){ int i; char
I have the following code: main.cpp #include <iostream> #include <string> using namespace std; string
I have the following code: #include<iostream> using namespace std; typedef void (*HandlerFunc)(int, int); HandlerFunc
i have following code in c++ #include <iostream> using namespace std; void qsort5(int a[],int
I have the following code #include <iostream> #include<string> #include <sstream> using namespace std; struct
I have the following code: #include <iostream> #include <string> #include <unistd.h> using namespace std;
i have following code #include <iostream> #include <string> using namespace std; string generate(){ for
i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class
I have the following C++ code: #include <iostream> #include <string> using namespace std; class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.