boolean help

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;
Last edited on
is the b' initial valve is false,


i never see b to be seted true. it will always be false.
Last edited on
sorry, I should have posted more of the code, it looks like this, where b is set to true, but it gives the same result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
string name;
	char answer;
	bool b;
	vector<string> vect;

while (answer == 'y')

b= true;
        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;
Last edited on
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.
Last edited on
Are you suggesting this? Because I get erros when I try this code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
string name;
	char answer;
	bool b;
	vector<string> vect;

while (answer == 'y')
{
b= true;
        cout << "Enter name: " <<flush ;
        cin >> name;
		for (int i=0; i<vect.size(); i++)
		{
			if (vect[i]==name) {b= false;}
			
            {cout<<"You entered that name already"<<endl;}
		}
     else {b=true}
		   vect.push_back (name);

        cout << "More names, enter 'y' or 'n'? " << flush;
        cin >> answer;
}
for (int i=0; i<vect.size(); i++)
{
if (vect[i]==name)
{
cout<<"You entered that name already"<<endl;
}
else {
vect.push_back (name);
}
}

actually b is not necessary!

in your code cout<<"You entered that name already"<<endl; will always be executed
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);
}
}
how do I make it so that it won't execute every time, but only when there is a same name in the vector?
is it work?
in your previous code,

”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!



Last edited on
it is still not working for me
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace 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;
		for (int i=0; i<vect.size(); i++)
		{
		    if (vect[i]==name)
                           {cout<<"You entered that name already"<<endl;
                            break;}
                    else
		   {vect.push_back (name);}

        cout << "More names, enter 'y' or 'n'? " << flush;
        cin >> answer;
}

    cout<< "You entered this names: ";
    ostream_iterator<string> out_it (cout,", ");
    copy ( vect.begin(), vect.end(), out_it );

    return 0;
}}
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
45
46
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
using namespace 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 (unsigned int 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!


Last edited on
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!
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
45
46
47
48
49
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
using namespace 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 (unsigned int 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 << "More names, enter 'y' or 'n'? " << flush;
		cin >> answer;
		}

		cout << "You entered this names: " << endl;
		copy(vect.begin(), vect.end(), ostream_iterator<string>(cout, " "));
		cout << endl;


	return 0;
}
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;

		unsigned int 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);
		}
sorry , i make a mistake. dhayden is right.
thank you both very much for your help!
Topic archived. No new replies allowed.