Help with if statement and output

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 29
Reputation: hurbano is an unknown quantity at this point 
Solved Threads: 0
hurbano hurbano is offline Offline
Light Poster

Help with if statement and output

 
0
  #1
Apr 9th, 2009
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!

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6.  
  7. {
  8.  
  9. string Pname, CEmployer, AWUTDspecification;
  10. int age, n, y;
  11. char AWUTD;
  12.  
  13. n=0;
  14. y=0;
  15.  
  16.  
  17. cout<<"Patients Name:"<<endl;
  18. getline (cin, Pname);
  19.  
  20.  
  21. cout<<"Age:"<<endl;
  22. cin>>age;
  23. cin.ignore(1000,'\n');
  24.  
  25. cout<<"Current Employer:"<<endl;
  26. getline (cin, CEmployer);
  27.  
  28.  
  29. cout<<"Any Work-Up to Date? Y/N"<<endl;
  30. cin>>AWUTD;
  31. cin.ignore(1000,'\n');
  32.  
  33. if(AWUTD=='y'||'Y')
  34. {
  35. cout<<"Please Specify:"<<endl;
  36. getline (cin, AWUTDspecification);
  37. }
  38. else if (AWUTD=='n'||'N')
  39. {
  40. cout<<endl;
  41. }
  42.  
  43. cout<<"Patients Name:"<<Pname<<endl;
  44. cout<<"Patients Age:"<<age<<endl;
  45.  
  46. cout<<"Patients Current Employer:"<<CEmployer<<endl;
  47. cout<<"Work up to date:"<<AWUTD<<endl;
  48.  
  49.  
  50. };
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: Help with if statement and output

 
0
  #2
Apr 9th, 2009
Change
  1. if(AWUTD=='y'||'Y')
  2. {
  3. cout<<"Please Specify:"<<endl;
  4. getline (cin, AWUTDspecification);
  5. }
  6. else if (AWUTD=='n'||'N')
  7. {
  8. cout<<endl;
  9. }
to

  1. if(AWUTD=='y'||AWUTD=='Y')
  2. {
  3. cout<<"Please Specify:"<<endl;
  4. getline (cin, AWUTDspecification);
  5. }
  6. else if (AWUTD=='n'||AWUTD=='N')
  7. {
  8. cout<<endl;
  9. }

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 9: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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 29
Reputation: hurbano is an unknown quantity at this point 
Solved Threads: 0
hurbano hurbano is offline Offline
Light Poster

Re: Help with if statement and output

 
0
  #3
Apr 9th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 236
Reputation: TheBeast32 is on a distinguished road 
Solved Threads: 6
TheBeast32's Avatar
TheBeast32 TheBeast32 is offline Offline
Posting Whiz in Training

Re: Help with if statement and output

 
0
  #4
Apr 9th, 2009
You can just see if AWUTD is y or n and display what you want.

  1. cout<<"Patients Name:"<<Pname<<endl;
  2. cout<<"Patients Age:"<<age<<endl;
  3.  
  4. cout<<"Patients Current Employer:"<<CEmployer<<endl;
  5.  
  6. if(AWUTD=='y'||AWUTD=='Y')
  7. {
  8. cout<<"Work up to date:Yes"<<endl;
  9. }
  10. else if(AWUTD=='n'||AWUTD=='N')
  11. {
  12. cout<<"Work up to date:No"<<endl;
  13. cout<<"Specification:"<<AWUTDspecification<<endl;
  14. }

I added the specification if it was no, too.
Last edited by TheBeast32; Apr 9th, 2009 at 9: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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 29
Reputation: hurbano is an unknown quantity at this point 
Solved Threads: 0
hurbano hurbano is offline Offline
Light Poster

Re: Help with if statement and output

 
0
  #5
Apr 9th, 2009
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??

  1.  
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7.  
  8. {
  9.  
  10. string Pname, CEmployer, AWUTDspecification, AMPM, RLhand,
  11. AllergiesSpecification;
  12. int age, n, y;
  13. char AWUTD, Allergies;
  14.  
  15. n=0;
  16. y=0;
  17. AWUTD=0;
  18.  
  19.  
  20. cout<<"Patients Name:"<<endl;
  21. getline (cin, Pname);
  22. cout<<'\n';
  23.  
  24.  
  25. cout<<"Age:"<<endl;
  26. cin>>age;
  27. cin.ignore(1000,'\n');
  28. cout<<'\n';
  29.  
  30. cout<<"Current Employer:"<<endl;
  31. getline (cin, CEmployer);
  32. cout<<'\n';
  33.  
  34. cout<<"Right or Left Handed?"<<endl;
  35. getline (cin, RLhand);
  36. cout<<'\n';
  37.  
  38. cout<<"Was appointment AM or PM"<<endl;
  39. getline (cin, AMPM);
  40. cout<<'\n';
  41.  
  42. cout<<"Does the patient have any allergies? Y/N"<<endl;
  43. cin>>Allergies;
  44. cin.ignore(1000,'\n');
  45. cout<<'\n';
  46.  
  47. if(Allergies=='y'||Allergies=='Y')
  48. {
  49. cout<<"What is patient allergic to:"<<endl;
  50. getline (cin, AllergiesSpecification);
  51. cin.ignore(1000,'\n');
  52.  
  53. }
  54. else if (Allergies=='n'||Allergies=='N')
  55. {
  56. cout<<endl;
  57.  
  58. }
  59.  
  60.  
  61.  
  62.  
  63. cout<<"Any work up to date? Y/N"<<endl;
  64. cin>>AWUTD;
  65.  
  66.  
  67. if(AWUTD=='y'||AWUTD=='Y')
  68. {
  69. cout<<"Please Specify:"<<endl;
  70. getline (cin, AWUTDspecification);
  71.  
  72. }
  73. else if (AWUTD=='n'||AWUTD=='N')
  74. {
  75. cout<<endl;
  76. }
  77.  
  78.  
  79.  
  80. cout<<'\n';
  81. cout<<'\n';
  82. cout<<'\n';
  83.  
  84.  
  85.  
  86. cout<<"Patients Name: "<<Pname<<endl;
  87.  
  88. cout<<"Patients Age: "<<age<<endl;
  89.  
  90. cout<<"Patients Current Employer: "<<CEmployer<<endl;
  91.  
  92. cout<<"Right or left handed: "<<RLhand<<"handed"<<endl;
  93.  
  94. cout<<"Appointment: "<<AMPM<<endl;
  95.  
  96. cout<<"Allergies: "<<Allergies<<endl;
  97. if
  98. (Allergies=='y' || Allergies=='Y')
  99. {
  100. cout<<"Patient is allergic to:"<<AllergiesSpecification<<endl;
  101.  
  102. }
  103. else if (Allergies =='n' || Allergies=='N')
  104. {
  105. cout<<endl;
  106. }
  107.  
  108.  
  109. cout<<"Any work up to date?"<<AWUTD<<endl;//if statement to show correct output
  110. if
  111. (AWUTD=='y' || AWUTD=='Y')
  112. {
  113. cout<<"Work up to date includes:"<<AWUTDspecification<<endl;
  114. }
  115. else if (AWUTD =='n' || AWUTD=='N')
  116. {
  117. cout<<endl;
  118. }
  119.  
  120.  
  121. };
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 90
Reputation: unbeatable0 is an unknown quantity at this point 
Solved Threads: 12
unbeatable0 unbeatable0 is offline Offline
Junior Poster in Training

Re: Help with if statement and output

 
0
  #6
Apr 10th, 2009
I'm guessing it's because of the getline() function. When I replace the
  1. if(AWUTD=='y'||AWUTD=='Y')
  2. {
  3. cout<<"Please Specify:"<<endl;
  4. getline (cin, AWUTDspecification);
  5. }

with
  1. if(AWUTD=='y'||AWUTD=='Y')
  2. {
  3. cout<<"Please Specify:"<<endl;
  4. cin>>AWUTDspecification;
  5. }

it works better. I suggest that you read the following ReadMe thread: How do I flush the input stream?.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Help with if statement and output

 
0
  #7
Apr 10th, 2009
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 ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC