Hi, I have a problem with this piece of code. When user enters the same name more than once I get multiple output "You entered that name already", and not only one time, like I intended to.
Thank you!
1 2 3 4 5 6 7 8 9 10 11 12 13
cout << "Enter name: " <<flush ;
cin >> name;
for (int i=0; i<vect.size(); i++)
{
if (vect[i]==name) b= false;
if (b==false)
{cout<<"You entered that name already"<<endl;}
}
if(b==true)
vect.push_back (name);
cout << "More names, enter 'y' or 'n'? " << flush;
cin >> answer;
if (vect[i]==ingredient){
b= false;
}else{
b = true;
}
if the condition vect[i]==ingredient is not satisfied , b will not be updated.
so if there is an i, that vect[i] == ingrediten, b will be false , and will never be true again,
even vect[i] != ingrediten.
for (int i=0; i<vect.size(); i++)
{
if (vect[i]==name)
{
cout<<"You entered that name already"<<endl;
break; // it is not necessary to iterate
}
else {
vect.push_back (name);
}
}
”cout<<"You entered that name already"<<endl “ will always execute。
in your code:
if (vect[i]==name)
{b= false;}
{cout<<"You entered that name already"<<endl;}
the right is
if (vect[i]==name)
{b= false;
cout<<"You entered that name already"<<endl;
}
they are different, the second will display "You entered that name already" only if the condition vect[i]==name is true.
i suggest you to set a breakpoint on the line "if (vect[i]==name)", debug step by step, and see what will happen when the condition vect[i]==name is not satisfied.
same mistake will be found in the else branch
else {b=true}
vect.push_back (name);
it should be:
else {b=true;
vect.push_back (name);
}
my English is not very good, hope it could help you!
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
usingnamespace std;
int main()
{
string name;
char answer;
vector<string> vect;
cout << "Do you want to write down any names? (y/n)" << flush;
cin >> answer;
while (answer == 'y')
{
cout << "Enter name: " << flush;
cin >> name;
if (0 == vect.size()){
vect.push_back(name);
}
else{
for (unsignedint i = 0; i<vect.size(); i++)
{
if (vect[i] == name)
{
cout << "You entered that name already" << endl;
break;
}
else
{
vect.push_back(name);
break;
}
}
}
cout << "You entered this names: " << endl;
copy(vect.begin(), vect.end(), ostream_iterator<string>(cout, " "));
cout << endl;
cout << "More names, enter 'y' or 'n'? " << flush;
cin >> answer;
}
return 0;
}
i found some mistakes,
when program start, vector vect is empty, so the result vect.size() equals to zero,
so the for loop condition will not be satisfied.
i modified your source on my computer, and it works!
thank you so much for your help, it works fine now the way you made it, thank you again.
I have one more question, I modified a little your code, since I want to ask user after he enters name every time if he wants to enter new name and then at the end when he is finished to list all the names, but when I have done this, I only get for the first name entered output "You entered that name already" and after that not. And in the end of the program it lists all the names entered, even the duplicates, and not just single names. I hope I made myself clear. Here is my code. Thank you again for all your help!
This code still doesn't work. In particular, look at the break statements at lines 28 and 33. The first time through the loop, one or the other of these breaks will execute, so the loop always executes exactly once and it's equivalent to:
1 2 3 4 5 6 7 8
if (vect[0] == name)
{
cout << "You entered that name already" << endl;
}
else
{
vect.push_back(name);
}
The thing you're both missing in all of this is that the code to insert the name should be outside the loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
cout << "Enter name: " << flush;
cin >> name;
unsignedint i;
for (i = 0; i<vect.size(); i++) {
if (vect[i] == name) {
break;
}
}
// Now if you found the name then you broke out of the loop early.
// If you didn't find it, then i == vect.size(). Note that this works even
// when vect is empty.
if (i == vect.size()) {
// Name wasn't found
vect.push_back(name);
}