String with two words

I am trying to get a string with two words 'Jesus Christ within an array. I know im meant to use Char but dont know how with the classes. 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
  class Bible
{
private:
	static const size_t NR_OF_RESPONDERS=2;
	string youName[NR_OF_RESPONDERS];
        string name[NR_OF_RESPONDERS];
public:

	void getRespondersDetails()
	{
	for (size_t count = 0; count<NR_OF_RESPONDERS; ++count)
{
cout<<"Enter your name: ";
cin>>name[count]
cout<<" Do you call him Jesus Christ or Messiah?: ";
cin>>youName[count];
}
cout<<"---------------";
}
void Hisname()
for (size_t count=0; count<NR_OF_RESPONDERS; ++count)
{if(youName[count] =="Jesus Christ")
{cout<<"People who chose Jesus Christ: "<<name[count]<<endl;}
else
{cout<<"None chose JC"<<endl;}
}}
};
int main()
{
 Bible e1;
e1.getRespondersDetails();
e1.Hisname();
}
Last edited on
Calls to "cin >>" are broken up by all white space, not just '\n'.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string name;
cin >> name; // user enters "Jesus Christ"
cout << name; // will only display "Jesus"; "Christ" is still in the input stream

// how about:
string first_name;
string second_name;
cin >> first_name >> second_name; // user enters "Jesus Christ"
cout << first_name << endl; // display "Jesus"
cout << last_name << endl; // display "Christ"

//consider using getline() instead
getline(cin, name); // user enters "Jesus Christ" input is terminated by '\n'
cout << name << endl; // display "Jesus Christ" 

I will use string first_name, second_name as it is less complicated. But i would like to see how its done with the getline
1
2
3
4
5
6
7
8
9
10
{
	for (size_t count = 0; count<NR_OF_RESPONDERS; ++count)
{
cout<<"Enter your name: ";
cin>>name[count]
cout<<" Do you call him Jesus Christ or Messiah?: ";
getline(cin,youName[count]);
}
cout<<"---------------";
}


it actually skips the part where you have to insert your choice and goes back to Enter name
closed account (D80DSL3A)
You are experiencing the tiny nightmare which results from mixing formatted (cin >>) input operations with unformatted ones like getline.
cin >> leaves the \n in the stream then getline sees that leftover \n as end of line.
The best way to fix this IMO is to avoid mixing the 2 input methods.
Use getline for both:
1
2
3
4
cout<<"Enter your name: ";
getline(cin,name[count]);
cout<<" Do you call him Jesus Christ or Messiah?: ";
getline(cin,youName[count]);

This also permits the user to enter 1st + last name.

@tipaye I hadn't noticed your post, not sure why.
User may enter one or two words, it is not known, so getline is really needed here.
The 1st method would hang expecting input for last_name whether the user intends to enter a 2nd name or not.
Last edited on
Thank you tipaye and fun2code. Its working. I also add cin.ignore() for integers line that use cin>>
closed account (D80DSL3A)
You're welcome and glad you found the cin.ignore() solution to the stray \n issue present when you must mix the 2 input methods.
@fun2code Thanks for pointing that out, it usually catches people out.

@OP I probably should have elaborated, I'd expected you'd be giving your users better instructions than shown in the post. If you really want to use cin >> twice, with first_name, last_name, then I suggest explicitly using multiple statements, like:
1
2
3
4
5
6
7
8
9
10
11
12
string first_name;
cout << "Please enter first name ";
cin >> first_name;

// check stream state ??

string second_name;
cout << "Please enter second name ";
cin >> second_name;

// check stream state ??



Don't forget to check for input errors, however unlikely.
Topic archived. No new replies allowed.