2 Replies - 254 Views - Last Post: 03 September 2013 - 11:39 AM Rate Topic: -----

#1 NotoriousArab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 03-September 13

Program skips over the input

Posted 03 September 2013 - 11:25 AM

I've tried researching strings and how to operate them but unfortunately, I am still an amateur so my knowledge isn't sufficient enough to understand C++ resources. I know assignments are frowned upon here, but I am not asking for the answer. Just some explanation as to what is happening and how I can solve it.

#include <cmath>
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string name;
	string friendName;
	char gender;

	cout << "Enter you full name: ";
	getline(cin, name);
	cout << endl;
	cout << name << ", please enter your gender (m/f): ";
	cin >> gender;
	cout << endl;
	if (gender=='m')
	{
		cout << "Mr. " << name;
		cout << ", please enter you friend's name: ";
		getline(cin, friendName);
		cout << endl;
		cout << "Hello " << "Mr. " << friendName << " , " << name << " considered you as a friend!" << endl;
	}
	else 
	{
		cout << "Mrs. " << name << " please enter you friend's name: ";
		getline(cin, friendName);
		cout << endl;
		cout << "Hello " << "Mrs. " << friendName << " , " << name << " considered you as a friend!" << endl;
	}

	return 0;
}


And here's a sample of the output:

Enter you full name: testing test

testing test, please enter your gender (m/f): m

Mr. testing test, please enter you friend's name:
Hello Mr.  , testing test considered you as a friend!
Press any key to continue . . .


As you can see, the program skips over the part where you are supposed to input another string for your friend's name.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Program skips over the input

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 660
  • View blog
  • Posts: 2,270
  • Joined: 31-December 10

Re: Program skips over the input

Posted 03 September 2013 - 11:36 AM

Your problem occurs after you input the 'gender' variable because 'gender' is a char and when you get the input, cin leaves the newline character in standard input. So the next time you get input, the newline is reached and nothing is input. You can either put a cin.get() or a cin.ignore() function call right after you input 'gender'.
Was This Post Helpful? 1
  • +
  • -

#3 NotoriousArab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 03-September 13

Re: Program skips over the input

Posted 03 September 2013 - 11:39 AM

View Postvividexstance, on 03 September 2013 - 10:36 AM, said:

Your problem occurs after you input the 'gender' variable because 'gender' is a char and when you get the input, cin leaves the newline character in standard input. So the next time you get input, the newline is reached and nothing is input. You can either put a cin.get() or a cin.ignore() function call right after you input 'gender'.


Well, I knew was something simple. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1