hello im having problem trying to get this to do what i want it to do. after the program asks if the user has had any work up to date, i want it to ask for specification if the answer is yes, or go to a next question if no. except right now when the user inputs no, it still asks for specification. if the user inputs yes i want the specification to show in the output at the end. if user said no work was done i just want it to read as a simple no work done. any help would be appreciated. thanks alot!

#include <iostream>
#include <string>
using namespace std;

int main()

{
	
	string Pname, CEmployer, AWUTDspecification;
	int age, n, y;
	char AWUTD;

	n=0;
		y=0;
	

	cout<<"Patients Name:"<<endl;
	getline (cin, Pname);


	cout<<"Age:"<<endl;
	cin>>age;
	cin.ignore(1000,'\n'); 

	cout<<"Current Employer:"<<endl;
	getline (cin, CEmployer);
	

	cout<<"Any Work-Up to Date? Y/N"<<endl;
	cin>>AWUTD;
	cin.ignore(1000,'\n');
	
	if(AWUTD=='y'||'Y')
	{
		cout<<"Please Specify:"<<endl;
		getline (cin, AWUTDspecification);
	}
	else if (AWUTD=='n'||'N')
	{
		cout<<endl;
	}

	cout<<"Patients Name:"<<Pname<<endl;
cout<<"Patients Age:"<<age<<endl;

cout<<"Patients Current Employer:"<<CEmployer<<endl;
cout<<"Work up to date:"<<AWUTD<<endl;


};

Dani AI

Generated

Good progress so far. Two separate problems were hiding in the thread: the logical test for the Y/N response (caught by ) and the classic input-buffer/newline interaction when mixing operator>> and getline (noted by and ). Fix the condition, then make the input handling consistent — that will stop the “ends early” behaviour and let you print either the specification or a simple “No” cleanly.

Keep input handling consistent. Two practical, robust approaches:

  • Read everything with std::getline (convert number-strings with std::stoi) so there’s no mixing of formatted extraction and line reads.
  • If you must use operator>> for tokens, always consume the rest of the line afterwards with a full ignore (use std::numeric_limits<std::streamsize>::max()), or use std::ws before a getline to skip leftover whitespace.

A compact pattern that avoids the newline problem and accepts “yes/no” in a case-insensitive way:

std::string answer;
std::getline(std::cin, answer);
char r = answer.empty() ? 'n' : std::tolower(static_cast<unsigned char>(answer[0]));
if (r == 'y') {
    std::string spec;
    std::getline(std::cin, spec);
    std::cout << "Work up to date: Yes\nSpecification: " << spec << '\n';
} else {
    std::cout << "Work up to date: No\n";
}

One more gotcha to watch for in your posted code: you have a cin.ignore(...) immediately after a getline (for allergies). Remove that extra ignoregetline already consumes the newline, and the extra ignore will skip the next prompt. If problems persist, add quick debug prints (show lengths or the raw char codes) to verify whether a string is empty or the buffer contained a leftover newline.

Recommended Answers

All 6 Replies

Change

if(AWUTD=='y'||'Y')
	{
		cout<<"Please Specify:"<<endl;
		getline (cin, AWUTDspecification);
	}
	else if (AWUTD=='n'||'N')
	{
		cout<<endl;
	}

to

if(AWUTD=='y'||AWUTD=='Y')
	{
		cout<<"Please Specify:"<<endl;
		getline (cin, AWUTDspecification);
	}
	else if (AWUTD=='n'||AWUTD=='N')
	{
		cout<<endl;
	}

I'm pretty sure that adding ||'Y' to your if means that if the ASCII code for 'Y' is nonzero, then it's true. So it will be true for the first if every time.

thanks it worked!!
but now how would i get the output to display no if the user inputs no, or all the specifications if the user inputs yes?

You can just see if AWUTD is y or n and display what you want.

cout<<"Patients Name:"<<Pname<<endl;
cout<<"Patients Age:"<<age<<endl;

cout<<"Patients Current Employer:"<<CEmployer<<endl;

if(AWUTD=='y'||AWUTD=='Y')
{
	cout<<"Work up to date:Yes"<<endl;
}
else if(AWUTD=='n'||AWUTD=='N')
{
	cout<<"Work up to date:No"<<endl;
	cout<<"Specification:"<<AWUTDspecification<<endl;
}

I added the specification if it was no, too.

thanks it really shaping up now. i cant thank you enough.
i hope im not frustrating you with what seems as juvinelle questions regarding c++. with my most recent version of this program, everything is working till the end. when its supposed to display the work up to date it is not working it ends early. yet when the allergic specifications comes out it works fine. is there anything im doing wrong??

#include <iostream>
#include <string>
using namespace std;

int main()

{
	
	string Pname, CEmployer, AWUTDspecification, AMPM, RLhand,
		 AllergiesSpecification;
	int age, n, y;
	char AWUTD, Allergies;

	n=0;
		y=0;
		AWUTD=0;
	

	cout<<"Patients Name:"<<endl;
	getline (cin, Pname);
	cout<<'\n';


	cout<<"Age:"<<endl;
	cin>>age;
	cin.ignore(1000,'\n');
	cout<<'\n';

	cout<<"Current Employer:"<<endl;
	getline (cin, CEmployer);
	cout<<'\n';

	cout<<"Right or Left Handed?"<<endl;
	getline (cin, RLhand);
	cout<<'\n';

	cout<<"Was appointment AM or PM"<<endl;
	getline (cin, AMPM);
	cout<<'\n';

	cout<<"Does the patient have any allergies? Y/N"<<endl;
	cin>>Allergies;
	cin.ignore(1000,'\n');
	cout<<'\n';

	if(Allergies=='y'||Allergies=='Y')	
	{
		cout<<"What is patient allergic to:"<<endl;
		getline (cin, AllergiesSpecification);
		cin.ignore(1000,'\n');
		
	}
	else if (Allergies=='n'||Allergies=='N')
	{
		cout<<endl;
		
	}

	


	cout<<"Any work up to date? Y/N"<<endl;
	cin>>AWUTD;


	if(AWUTD=='y'||AWUTD=='Y')	
	{
		cout<<"Please Specify:"<<endl;
		getline (cin, AWUTDspecification);
		
	}
	else if (AWUTD=='n'||AWUTD=='N')
	{
		cout<<endl;
	}

	
	
cout<<'\n';
cout<<'\n';
cout<<'\n';
	
	
	
cout<<"Patients Name: "<<Pname<<endl;

cout<<"Patients Age: "<<age<<endl;

cout<<"Patients Current Employer: "<<CEmployer<<endl;

cout<<"Right or left handed: "<<RLhand<<"handed"<<endl;

cout<<"Appointment: "<<AMPM<<endl;

cout<<"Allergies: "<<Allergies<<endl;
if
(Allergies=='y' || Allergies=='Y')
{
	cout<<"Patient is allergic to:"<<AllergiesSpecification<<endl;

}
else if (Allergies =='n' || Allergies=='N')
{
	cout<<endl;
}


cout<<"Any work up to date?"<<AWUTD<<endl;//if statement to show correct output
if
(AWUTD=='y' || AWUTD=='Y')
{
	cout<<"Work up to date includes:"<<AWUTDspecification<<endl;
}
else if (AWUTD =='n' || AWUTD=='N')
{
	cout<<endl;
}


};

I'm guessing it's because of the getline() function. When I replace the

if(AWUTD=='y'||AWUTD=='Y')
{
cout<<"Please Specify:"<<endl;
getline (cin, AWUTDspecification);
}

with

if(AWUTD=='y'||AWUTD=='Y')
{
cout<<"Please Specify:"<<endl;
cin>>AWUTDspecification;
}

it works better. I suggest that you read the following ReadMe thread: How do I flush the input stream?.

Yup, if you're using the getline or cin.get(charvar, number_of_chrs) methods/functions, you always have to bear in mind the '\n' character stays in the input buffer ...
But as already mentioned there's an extensive thread about it ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.