| | |
Help with if statement and output
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 29
Reputation:
Solved Threads: 0
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!
C++ Syntax (Toggle Plain Text)
#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; };
Change
to
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.
C++ Syntax (Toggle Plain Text)
if(AWUTD=='y'||'Y') { cout<<"Please Specify:"<<endl; getline (cin, AWUTDspecification); } else if (AWUTD=='n'||'N') { cout<<endl; }
C++ Syntax (Toggle Plain Text)
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.
Last edited by TheBeast32; Apr 9th, 2009 at 8:24 pm.
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
--Martin Golding
You can just see if AWUTD is y or n and display what you want.
I added the specification if it was no, too.
C++ Syntax (Toggle Plain Text)
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.
Last edited by TheBeast32; Apr 9th, 2009 at 8:58 pm.
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."
--Martin Golding
--Martin Golding
•
•
Join Date: Sep 2008
Posts: 29
Reputation:
Solved Threads: 0
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??
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??
C++ Syntax (Toggle Plain Text)
#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; } };
•
•
Join Date: Sep 2008
Posts: 90
Reputation:
Solved Threads: 12
I'm guessing it's because of the getline() function. When I replace the
with
it works better. I suggest that you read the following ReadMe thread: How do I flush the input stream?.
c++ Syntax (Toggle Plain Text)
if(AWUTD=='y'||AWUTD=='Y') { cout<<"Please Specify:"<<endl; getline (cin, AWUTDspecification); }
with
c++ Syntax (Toggle Plain Text)
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?.
![]() |
Similar Threads
- Switch Statement output error (PHP)
- How to code a program that can show color text output??? (C)
- Problems with switch statement (C++)
- Using while statement..any suggestions? (C++)
- need help with an IF statement (C++)
- Problems with switch statement (C++)
- loop in main function to an "if" statement (C++)
Other Threads in the C++ Forum
- Previous Thread: How to Read Excel File
- Next Thread: SID
Views: 363 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment beginner binary browser c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count delete desktop display dll dynamic encryption error exception file files fstream function functions game givemetehcodez graph gui helpwithhomework homework i/o iamthwee input int integer lazy library linked-list linker list loop looping loops math matrix member memory newbie news number object objects opengl output parameter path pointer pointers problem program programming project random read recursion recursive reference sort sorting spoonfeeding string strings struct student studio template templates text time tree undefined variable vc++ vector video visual win32 window windows winsock






