943,861 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 434
  • C++ RSS
Apr 9th, 2009
0

Help with if statement and output

Expand Post »
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)
  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. };
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
hurbano is offline Offline
50 posts
since Sep 2008
Apr 9th, 2009
0

Re: Help with if statement and output

Change
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Apr 9th, 2009
0

Re: Help with if statement and output

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
hurbano is offline Offline
50 posts
since Sep 2008
Apr 9th, 2009
0

Re: Help with if statement and output

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

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 79
Solved Threads: 6
Posting Whiz in Training
TheBeast32 is offline Offline
236 posts
since Dec 2007
Apr 9th, 2009
0

Re: Help with if statement and output

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??

C++ Syntax (Toggle Plain Text)
  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. };
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
hurbano is offline Offline
50 posts
since Sep 2008
Apr 10th, 2009
0

Re: Help with if statement and output

I'm guessing it's because of the getline() function. When I replace the
c++ Syntax (Toggle Plain Text)
  1. if(AWUTD=='y'||AWUTD=='Y')
  2. {
  3. cout<<"Please Specify:"<<endl;
  4. getline (cin, AWUTDspecification);
  5. }

with
c++ Syntax (Toggle Plain Text)
  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?.
Reputation Points: 42
Solved Threads: 13
Junior Poster in Training
unbeatable0 is offline Offline
90 posts
since Sep 2008
Apr 10th, 2009
0

Re: Help with if statement and output

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 ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How to Read Excel File
Next Thread in C++ Forum Timeline: SID





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC