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. C++ coding problem is failing for different test cases especially when input is 1. Please Help.

C++ coding problem is failing for different test cases especially when input is 1. Please Help.

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
6 Posts 6 Posters 26 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.
  • P Offline
    P Offline
    priyanka mp
    wrote on last edited by
    #1

    Hi I have below code written while contesting on a platform, though I find working with code but it fails with different test cases especially when Input value is 1 and no string is provided. Can anyone please help to resolve this issue ?

    #include
    using namespace std;

    string reSize(string s)
    {
    if(s.length() > 0)
    {
    std::string res;
    int count =1;
    for(int i =0;i<(s.length() - 1);i++)
    {
    if(s[i] == s[i+1])
    {
    count++;
    }
    else
    {
    res += s[i];
    if(count > 1)
    {
    res += std::to_string(count);
    }
    count = 1;
    }
    }

           res += s\[s.length() - 1\];
           if(count > 1)
           {
               res += std::to\_string(count);
           }
    
       return res;
    

    }
    else if(s.empty())
    {
    return "";
    }

    }

    int main() {
    int t;
    cin>>t;
    while(t--)
    {
    string s;
    cin>>s;
    cout<

    P L M CPalliniC J 5 Replies Last reply
    0
    • P priyanka mp

      Hi I have below code written while contesting on a platform, though I find working with code but it fails with different test cases especially when Input value is 1 and no string is provided. Can anyone please help to resolve this issue ?

      #include
      using namespace std;

      string reSize(string s)
      {
      if(s.length() > 0)
      {
      std::string res;
      int count =1;
      for(int i =0;i<(s.length() - 1);i++)
      {
      if(s[i] == s[i+1])
      {
      count++;
      }
      else
      {
      res += s[i];
      if(count > 1)
      {
      res += std::to_string(count);
      }
      count = 1;
      }
      }

             res += s\[s.length() - 1\];
             if(count > 1)
             {
                 res += std::to\_string(count);
             }
      
         return res;
      

      }
      else if(s.empty())
      {
      return "";
      }

      }

      int main() {
      int t;
      cin>>t;
      while(t--)
      {
      string s;
      cin>>s;
      cout<

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      What you need to do is put a breakpoint in your code, and step through it a line at a time. When you are executing a line, think about what you expect the outcome of that operation to be and compare what you think should be happening with what actually happens. Generally, this will lead you to work out what the problem actually is. There's a name for this type of approach, it's called debugging, and it's one of the most valuable skills you will ever learn.

      Advanced TypeScript Programming Projects

      1 Reply Last reply
      0
      • P priyanka mp

        Hi I have below code written while contesting on a platform, though I find working with code but it fails with different test cases especially when Input value is 1 and no string is provided. Can anyone please help to resolve this issue ?

        #include
        using namespace std;

        string reSize(string s)
        {
        if(s.length() > 0)
        {
        std::string res;
        int count =1;
        for(int i =0;i<(s.length() - 1);i++)
        {
        if(s[i] == s[i+1])
        {
        count++;
        }
        else
        {
        res += s[i];
        if(count > 1)
        {
        res += std::to_string(count);
        }
        count = 1;
        }
        }

               res += s\[s.length() - 1\];
               if(count > 1)
               {
                   res += std::to\_string(count);
               }
        
           return res;
        

        }
        else if(s.empty())
        {
        return "";
        }

        }

        int main() {
        int t;
        cin>>t;
        while(t--)
        {
        string s;
        cin>>s;
        cout<

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        priyanka mp wrote:

        Can anyone please help to resolve this issue ?

        You have not actually expalined what the issues is. What is the code supposed to do, what inputs are you using, what results do you get, and why are they not correct?

        1 Reply Last reply
        0
        • P priyanka mp

          Hi I have below code written while contesting on a platform, though I find working with code but it fails with different test cases especially when Input value is 1 and no string is provided. Can anyone please help to resolve this issue ?

          #include
          using namespace std;

          string reSize(string s)
          {
          if(s.length() > 0)
          {
          std::string res;
          int count =1;
          for(int i =0;i<(s.length() - 1);i++)
          {
          if(s[i] == s[i+1])
          {
          count++;
          }
          else
          {
          res += s[i];
          if(count > 1)
          {
          res += std::to_string(count);
          }
          count = 1;
          }
          }

                 res += s\[s.length() - 1\];
                 if(count > 1)
                 {
                     res += std::to\_string(count);
                 }
          
             return res;
          

          }
          else if(s.empty())
          {
          return "";
          }

          }

          int main() {
          int t;
          cin>>t;
          while(t--)
          {
          string s;
          cin>>s;
          cout<

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          what is this supposed to do ? what is the input and what is the output ? (what are the test cases ? )

          CI/CD = Continuous Impediment/Continuous Despair

          1 Reply Last reply
          0
          • P priyanka mp

            Hi I have below code written while contesting on a platform, though I find working with code but it fails with different test cases especially when Input value is 1 and no string is provided. Can anyone please help to resolve this issue ?

            #include
            using namespace std;

            string reSize(string s)
            {
            if(s.length() > 0)
            {
            std::string res;
            int count =1;
            for(int i =0;i<(s.length() - 1);i++)
            {
            if(s[i] == s[i+1])
            {
            count++;
            }
            else
            {
            res += s[i];
            if(count > 1)
            {
            res += std::to_string(count);
            }
            count = 1;
            }
            }

                   res += s\[s.length() - 1\];
                   if(count > 1)
                   {
                       res += std::to\_string(count);
                   }
            
               return res;
            

            }
            else if(s.empty())
            {
            return "";
            }

            }

            int main() {
            int t;
            cin>>t;
            while(t--)
            {
            string s;
            cin>>s;
            cout<

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            You know, you cannot get an empty string using cin (see, for instance, Get empty input - C++ Forum[^]).

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

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • P priyanka mp

              Hi I have below code written while contesting on a platform, though I find working with code but it fails with different test cases especially when Input value is 1 and no string is provided. Can anyone please help to resolve this issue ?

              #include
              using namespace std;

              string reSize(string s)
              {
              if(s.length() > 0)
              {
              std::string res;
              int count =1;
              for(int i =0;i<(s.length() - 1);i++)
              {
              if(s[i] == s[i+1])
              {
              count++;
              }
              else
              {
              res += s[i];
              if(count > 1)
              {
              res += std::to_string(count);
              }
              count = 1;
              }
              }

                     res += s\[s.length() - 1\];
                     if(count > 1)
                     {
                         res += std::to\_string(count);
                     }
              
                 return res;
              

              }
              else if(s.empty())
              {
              return "";
              }

              }

              int main() {
              int t;
              cin>>t;
              while(t--)
              {
              string s;
              cin>>s;
              cout<

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              Maybe its a contest. Guess the point and win the prize. I do note that there are two possible inputs. So if '1' was put in for the first one, then I expect it works. If put in for the second then I suspect '2', ..., '9' would also fail.

              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