Reputation: 13
#include <iostream>
#include <vector>
#include <string>
using std::cout; using std::endl;
using std::cin; using std::vector; using std::string;
int main()
{
cout << "Input strings(end-of-file to exit):"<<endl;
vector<string> strings;
string x;
while (cin >> x)
{
strings.push_back(x);
}
typedef vector<string>::size_type vc_sz;
vc_sz size = strings.size();
int same_string=0;
for (int i = 0; i < size; i++)
{
for (int j = i+1; j < size; j++)
{
if (strings[i] == strings[j])
++same_string;
}
}
cout << "Same string number:" << same_string << endl;
system("pause");
return 0;
}
This is a code for a simple program that counts how many string inputs are redundant. Everything seems to work fine except I need to type in end-of-file(ctr+z) two times to end the loop and get the result. I can't figure out why this is happening.
Upvotes: 1
Views: 261
Reputation: 44278
You can significantly simplify your code if you use std::set
int main()
{
cout << "Input strings(end-of-file to exit):"<<endl;
set<string> strings;
string x;
int same_string=0;
while (cin >> x)
{
if( !strings.insert(x).second ) ++same_string;
}
cout << "Same string number:" << same_string << endl;
system("pause");
return 0;
}
Upvotes: 0
Reputation: 15870
It would appear that you are attempting to out the EOF character at the end of a line sequence:
> This is my inputEOF
This will force you to enter another EOF
to actually end the stream. If you want to end the stream with a single EOF, you need to hit enter first:
> This is my inputENTER
> EOF
Upvotes: 4