Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. having problem to find an error

having problem to find an error

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestioncomalgorithms
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bot joy
    wrote on last edited by
    #1

    Nobita and String | Basics of String Manipulation & Algorithms Practice Problems | HackerEarth[^] this is the question. my code:

    #include
    using namespace std;

    int main()
    {
    int t;
    cin>>t;
    std::string s[t];
    for(int i=0;i<=t-1;i++)
    {
    string st;
    getline(cin,st);
    int l=(int)st.length();
    string s2="",s3="";
    for(int i=0;i<=l;i++)
    {
    if(st[i]==' '||st[i]=='\0')
    {
    s3=s2+" "+s3;
    s2.clear();
    }
    else
    {
    s2+=st[i];
    }
    }
    s[i]=s3;
    }
    for(int i=0;i<=t-1;i++)
    cout<

    the error message is saying " your program doesnt print anything. can anyone help me find whats wrong?

    J CPalliniC D P 4 Replies Last reply
    0
    • B bot joy

      Nobita and String | Basics of String Manipulation & Algorithms Practice Problems | HackerEarth[^] this is the question. my code:

      #include
      using namespace std;

      int main()
      {
      int t;
      cin>>t;
      std::string s[t];
      for(int i=0;i<=t-1;i++)
      {
      string st;
      getline(cin,st);
      int l=(int)st.length();
      string s2="",s3="";
      for(int i=0;i<=l;i++)
      {
      if(st[i]==' '||st[i]=='\0')
      {
      s3=s2+" "+s3;
      s2.clear();
      }
      else
      {
      s2+=st[i];
      }
      }
      s[i]=s3;
      }
      for(int i=0;i<=t-1;i++)
      cout<

      the error message is saying " your program doesnt print anything. can anyone help me find whats wrong?

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      for(int i=0;i<=l;i++)

      Try changing to

      for(int i=0;iYou're index is going out of bounds on the st variable 'st[i]', when i is equal to l.
      Also, having nested for loops with the same index variable name is tough to read.

      "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
      "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
      "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

      1 Reply Last reply
      0
      • B bot joy

        Nobita and String | Basics of String Manipulation & Algorithms Practice Problems | HackerEarth[^] this is the question. my code:

        #include
        using namespace std;

        int main()
        {
        int t;
        cin>>t;
        std::string s[t];
        for(int i=0;i<=t-1;i++)
        {
        string st;
        getline(cin,st);
        int l=(int)st.length();
        string s2="",s3="";
        for(int i=0;i<=l;i++)
        {
        if(st[i]==' '||st[i]=='\0')
        {
        s3=s2+" "+s3;
        s2.clear();
        }
        else
        {
        s2+=st[i];
        }
        }
        s[i]=s3;
        }
        for(int i=0;i<=t-1;i++)
        cout<

        the error message is saying " your program doesnt print anything. can anyone help me find whats wrong?

        CPalliniC Online
        CPalliniC Online
        CPallini
        wrote on last edited by
        #3

        After the following line

        cin>>t;
        

        there are still charactes in the input buffer, hence, the next call to getline gets them. A commont way to deal with this issue is using cin.ignore (see, for instance What is the use of cin.ignore() in C++?[^]).

        "In testa che avete, Signor di Ceprano?" -- Rigoletto

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • B bot joy

          Nobita and String | Basics of String Manipulation & Algorithms Practice Problems | HackerEarth[^] this is the question. my code:

          #include
          using namespace std;

          int main()
          {
          int t;
          cin>>t;
          std::string s[t];
          for(int i=0;i<=t-1;i++)
          {
          string st;
          getline(cin,st);
          int l=(int)st.length();
          string s2="",s3="";
          for(int i=0;i<=l;i++)
          {
          if(st[i]==' '||st[i]=='\0')
          {
          s3=s2+" "+s3;
          s2.clear();
          }
          else
          {
          s2+=st[i];
          }
          }
          s[i]=s3;
          }
          for(int i=0;i<=t-1;i++)
          cout<

          the error message is saying " your program doesnt print anything. can anyone help me find whats wrong?

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Have you tried stepping through the code using a debugger? Doing so will let you know in a hurry what is wrong.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          1 Reply Last reply
          0
          • B bot joy

            Nobita and String | Basics of String Manipulation & Algorithms Practice Problems | HackerEarth[^] this is the question. my code:

            #include
            using namespace std;

            int main()
            {
            int t;
            cin>>t;
            std::string s[t];
            for(int i=0;i<=t-1;i++)
            {
            string st;
            getline(cin,st);
            int l=(int)st.length();
            string s2="",s3="";
            for(int i=0;i<=l;i++)
            {
            if(st[i]==' '||st[i]=='\0')
            {
            s3=s2+" "+s3;
            s2.clear();
            }
            else
            {
            s2+=st[i];
            }
            }
            s[i]=s3;
            }
            for(int i=0;i<=t-1;i++)
            cout<

            the error message is saying " your program doesnt print anything. can anyone help me find whats wrong?

            P Offline
            P Offline
            Patrice T
            wrote on last edited by
            #5

            In addition to other answers. Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.

            #include
            using namespace std;

            int main()
            {
            int t;
            cin>>t;
            std::string s[t];
            for(int i=0;i<=t-1;i++)
            {
            string st;
            getline(cin,st);
            int l=(int)st.length();
            string s2="",s3="";
            for(int i=0;i<=l;i++)
            {
            if(st[i]==' '||st[i]=='\0')
            {
            s3=s2+" "+s3;
            s2.clear();
            }
            else
            {
            s2+=st[i];
            }
            }
            s[i]=s3;
            }
            for(int i=0;i<=t-1;i++)
            cout<
            Indentation style - Wikipedia[^]

            Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
            Notepad++ Home[^]
            ultraedit[^]
            Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]

            Patrice

            “Everything should be made as simple as possible, but no simpler.” Albert Einstein

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups