0

I have this C++ program but its not printing anything in the console. I am copying the content of the string in 2 vectors and doing count increment operations on them.

#include <iostream>
#include<vector>
#include<bits/stdc++.h>
#include<string>
using namespace std;

int main()
{
  string s;
  cin>>s;
  int i,j;
  vector<char> a;
  vector<char> b;
  int count1=0;
  int count2=0;
  int count3=0;
  int t=s.length()/2;
  for(i=0;i<t-1;t++)
  {
      a.push_back(s[i]);
  }
  for(j=t;j<s.length();j++)
  {
      b.push_back(s[j]);
  }
        int e1,e2;
  for (auto it1 = a.begin(), it2 = b.begin();
     it1 != a.end() && it2 != b.end(); 
     ++it1, ++it2)
  {
      if(int(*it1)>int(*it2))
         count1++;
 
      if(int(*it1)<int(*it2))
         count2++;
 
      if(int(*it1)==int(*it2))
         count3++;
  }
  
  cout<<min(min(count1,count2),count3);
}

I am printing the minimum of the three counts in the end.

Himanshu Ranjan
  • 282
  • 1
  • 7
  • 22
  • Have you tried debugging your program? Also, `#include` is bad practice. – Mansoor Aug 29 '20 at 13:02
  • see https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – 2785528 Aug 29 '20 at 16:25
  • You are allowed to (at least temporarily) cout intermediate results. Example: out << "\n count1: " << count1 << " count2: " << count2 << " count3: " << count3 << endl << "\n min: " << min(min(count1,count2), count3) << endl; – 2785528 Aug 29 '20 at 16:41
  • Please provide a [mre] (you are pretty close). What is missing is an example of input and expected output and actual output. – 2785528 Aug 29 '20 at 16:43
  • Several places you try to compare integers of different sign. You should get in the habit of enabling more compiler warnings, such as. [ warning: comparison of integer expressions of different signedness: ‘int’ ....} [-Wsign-compare] ] – 2785528 Aug 29 '20 at 16:46

2 Answers2

0

I found your code fall into a infinite loop in the following.

for (i = 0; i < t - 1; t++)   // not t++, but it should be i++
{
    a.push_back(s[i]);
}

I also believe that a debugging skill helps you finding a bug in your program.

John Park
  • 1,644
  • 1
  • 12
  • 17
0

After your insert cin>>s; the program keep waiting a data inserted, after the data inserted on variable string sbut after this your code it does not seem clear about its objectives, because, for several inputs, your method with the internal if's will basically accomplish nothing, what is your original objective?

In your for have a loops. Overwrite your original block quote to:

  int t=s.length()/2;
  for(i=0;i<t-1;i++)
  {
      a.push_back(s[i]);
  }
Nor Sea
  • 23
  • 2