Why can't the vector catch the last term

Hi, I'm working on a project reading in a boolean function from a .txt file.
The .txt file is like:
ABcD+ABCD+aBcD+aBCD.
A 0.2
B 0.4
C 0.6
D 0.8

The number stands for the probability of variable being 0.
The goal is to partition the boolean function into several minterms.
In this case, I want to get ABcD, ABCD, aBcD, aBCD four terms.
My code so far is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <bits/stdc++.h> 
#include <iostream>
#include <fstream>
#include <string>
#include <sstream> 

using namespace std; 


int main()
{	
	string str("ABcD+ABCD+aBcD+aBCD.");
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << endl;
	}
	
	vector<string> minterm_list;
	stringstream ss;
	string minterm;
	char c;
	
	ss << str;
	while(ss >> c)
	{
    	if(c =='+')
    	{ 
			minterm_list.push_back(minterm);
			minterm = "";
    	}
		else
    	{
    		minterm = minterm + c;
    	}
	}
	
	cout << "There are " << minterm_list.size() << " minterm in total" << endl;
	for(int i=0; i < minterm_list.size(); i++)
	{	
		cout << minterm_list[i] << endl;
	}
	
return 0;
}

And the output is
A
B
c
D
+
A
B
C
D
+
a
B
c
D
+
a
B
C
D
.
There are 3 minterm in total
ABcD
ABCD
aBcD

Why isn't the fourth term aBcD not pushed by the vector?
Thx in advance!
Last edited on
Add the line
if ( !minterm.empty() ) minterm_list.push_back(minterm);
after the end of the while loop, or you won't catch what's left in at the end.

For goodness' sake, STOP CREATING NEW THREADS FOR THE SAME PROBLEM.
http://www.cplusplus.com/forum/beginner/278202/#msg1200958
http://www.cplusplus.com/forum/beginner/278111/#msg1200533


I've figured out the problem and modified the if-else condition and get the correct result.
1
2
3
4
5
else if(c == '.')
    	{
    	      minterm_list.push_back(minterm);
	      minterm = "";
	} 

A
B
c
D
+
A
B
C
D
+
a
B
c
D
+
a
B
C
D
.
There are 4 minterms in total, which are :
ABcD
ABCD
aBcD
aBCD
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
#include <string>
#include <sstream>

int main()
{
   std::string str("ABcD+ABCD+aBcD+aBCD.");

   for (const auto& itr : str)
   {
      std::cout << itr << '\n';
   }
   std::cout << '\n';

   std::vector<std::string> minterm_list;
   std::stringstream ss;
   std::string minterm;
   char c;

   ss << str;

   while (ss >> c)
   {
      if (c == '+'  || c == '.')
      {
         minterm_list.push_back(minterm);
         minterm = "";
      }
      else
      {
         minterm = minterm + c;
      }
   }

   std::cout << "There are " << minterm_list.size() << " minterm in total\n";

   for (const auto& itr : minterm_list)
   {
      std::cout << itr << '\n';
   }
}

A
B
c
D
+
A
B
C
D
+
a
B
c
D
+
a
B
C
D
.

There are 4 minterm in total
ABcD
ABCD
aBcD
aBCD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
#include <string>

int main()
{
	const std::string str {"ABcD+ABCD+aBcD+aBCD."};
	std::vector<std::string> terms;

	for (size_t fnd {}, pos {}; (fnd = str.find_first_of("+.", pos)) != std::string::npos; pos = fnd + 1)
		terms.push_back(str.substr(pos, fnd - pos));

	std::cout << "There are " << terms.size() << " minterm in total\n";

	for (const auto& itr : terms)
		std::cout << itr << '\n';
}



There are 4 minterm in total
ABcD
ABCD
aBcD
aBCD

Topic archived. No new replies allowed.