A while loop for entering names

I would like to write my while loop in such a way that the user can continue entering first and last names and when they want to quit they can simply type "Q". However, the only way I can figure to do this is by continuing to ask if they want to enter another name:

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
#include <iostream>
#include <iomanip>
#include <string>
#include "Header.h"
using namespace std;

int main()
{
   linkedListType <string> list1;       
   string first_name;
   string last_name;
   char another;

cout << "Type first name: ";
	cin >> first_name;
	cout << "Type last name: ";
	cin >> last_name;
	list1.insertLast(first_name, last_name); 
	cout << "Would you like to add another? Type y for yes or n for no: ";
	cin >> another;
	

	while ( another == 'y')
	{
	cout << "Type first name: ";
	cin >> first_name;
	cout << "Type last name: ";
	cin >> last_name;
	list1.insertLast(first_name, last_name); 
	cout << "Would you like to add another? Type y for yes or n for no: ";
	cin >> another;
	}

    list1.print ();

    cout << endl;  

return 0;
               
}


I would like to do it this way instead (see code below), but with a first and last name. Could someone please help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
	linkedListType<string> list1;		
	string name;															
cout<<"Line 3: Enter names ending with Q to quit the program"<<endl;										
cin>>name;										

//begin seed read
        while(name != "Q" && name != "q")					
	{
	list1.insertLast(name);		
	cin >> name;		
	}

									
list1.print();
return 0;
}	
closed account (1wU9216C)
Try this... The while loop is set to continue no matter what. But you can stop it by typing 'Q' or 'q' as first_name. This will avoid asking each time if they want to stop. All they have to do is enter q for the first_name.

1
2
3
4
5
6
7
8
9
10
11
12
13
while (true)
{
   cout << "Type first name: ";
   cin >> first_name;

   if(first_name == 'Q' && first_name == 'q'){
   break;
   }

   cout << "Type last name: ";
   cin >> last_name;
   list1.insertLast(first_name, last_name); 
}
Last edited on
Why not use a vector<string> to store the first and last names under one variable (rather than using three different variables), and then using an iterator to print the stored names in the vectors in a 'for' loop (it could also work in a 'while' loop as well.
@YukiSnowmew

Careful...you would want logical OR here, not AND. If you want to use break in this case (the current implementation isn't necessarily the best algorithm), you want the loop to break on the occurrence of a 'Q' OR a 'q' (||), not a 'Q' AND a 'q' (&&).
closed account (Dy7SLyTq)
what i would do:
1
2
3
4
5
6
7
while(true)
{
	cin.flush();
	cout<<"First and Last Name: ";
	cin>> FirstName;
	cin>> LastName;
}
That worked. Thanks for the help!
closed account (1wU9216C)
'Q' OR a 'q' (||), not a 'Q' AND a 'q' (&&).

My mistake. It was 4AM when I wrote that, so I was kinda prone to mistakes xD
Topic archived. No new replies allowed.