944,084 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 280
  • C++ RSS
Oct 23rd, 2009
0

Need help, having input problems.

Expand Post »
Ok, I know I'm probably programing in some kind of archaic way for C++ but my knowledge base is rather limited.
I'm having problems with my input not being identified correctly (or however you would say it, rusty is me)
C++ Syntax (Toggle Plain Text)
  1. cin>>StatsChange;
  2. if (StatsChange == "Strength" or "strength" or "str" or "STR" or "Str")
  3. {
  4. //Strength Changes
  5. Strength = Strength + 1;
  6. abilpts = abilpts -1;
  7. goto hpmpstat;
  8. }
  9. else if (StatsChange == "Dexterity" or "dexterity" or "dex" or "DEX" or "Dex")
  10. {
  11. //Dexterity Changes
  12. Dexterity = Dexterity + 1;
  13. abilpts = abilpts -1;
  14. goto hpmpstat;
  15. }
  16. else if (StatsChange == "Constitution" or "constitution" or "con" or "CON" or "Con")
  17. {
  18. //Constitution Changes
  19. Constitution = Constitution +1;
  20. abilpts = abilpts - 1;
  21. goto hpmpstat;
  22. }
  23. else if (StatsChange == "Intelligence" or "intelligence" or "int" or "INT" or "Int")
  24. {
  25. //Intelligence Changes
  26. Intelligence = Intelligence + 1;
  27. abilpts = abilpts -1;
  28. goto hpmpstat;
  29. }
  30. else if (StatsChange == "Wisdom" or "wisdom" or "wis" or "WIS" or "Wis")
  31. {
  32. //Wisdom Changes
  33. Wisdom = Wisdom + 1;
  34. abilpts = abilpts -1;
  35. goto hpmpstat;
  36. }
  37. else if (StatsChange == "Charisma" or "charisma" or "cha" or "CHA" or "Cha")
  38. {
  39. //Charisma Changes
  40. Charisma = Charisma +1;
  41. abilpts = abilpts -1;
  42. goto hpmpstat;
  43. }
  44. else
  45. {
  46. //error statement
  47. cout<<"You fail"<<endl;
  48. system("PAUSE");
  49. goto CCRT;
  50. }
When You type in like Dex or something it still only applies the strength bonus. Also if you type in random letters it only goes to the "//strength changes" part of the code instead of the "//error statement" part of the code. Can someone help me resolve this issue? Or bring me up to speed with some code that could help me?
Similar Threads
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
Shinedevil is offline Offline
71 posts
since Nov 2008
Oct 23rd, 2009
1
Re: Need help, having input problems.
C++ Syntax (Toggle Plain Text)
  1.  
  2. if (StatsChange == "Strength" or "strength" or "str" or "STR" or "Str")

should be:
C++ Syntax (Toggle Plain Text)
  1. if (StatsChange == "Strength" || StatsChange == "strength" || StatsChange== "str" || StatsChange== "STR" ||StatsChange== "Str")

I know, believe me I did the same thing when I first got into boolean logic.. because the way you do it (aside from the incorrect symbology) seems correct at first, and more code efficient. However, please realize that there are specific rules at play here; each expression is handled individually.

If only one variable exists, the boolean test will either return TRUE if not zero, and FALSE if zero. Example:
C++ Syntax (Toggle Plain Text)
  1.  
  2. if(variable)
  3. {
  4. //code
  5. }

In this instance, if 'variable' contains any value, it will test TRUE and will enter/execute the contents of the if() structure. Conversely, if 'variable' contains a zero, the boolean expression will test FALSE and will simply skip over the entire if() structure.

So like me, you are probably wondering how to avoid these huge multiple lines of logic. Here is one way I can think of:
C++ Syntax (Toggle Plain Text)
  1. string input = {"Strength", "strength", "str", "STR", "Str"};
  2. bool match = FALSE:
  3. int i=0;
  4.  
  5. do{
  6. if(StatsChange == input[i])
  7. {
  8. match = TRUE;
  9. }
  10. }while(match == FALSE || i < 5);

Placing boolean tests inside of a loop could potentially eliminate several lines of logical testing.
Last edited by Clinton Portis; Oct 23rd, 2009 at 11:59 pm.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005
Oct 23rd, 2009
0
Re: Need help, having input problems.
Thanks so much for the help. IT works! Thank you.
Reputation Points: 8
Solved Threads: 4
Junior Poster in Training
Shinedevil is offline Offline
71 posts
since Nov 2008

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: Waiting between lines
Next Thread in C++ Forum Timeline: Using a CHAR in if statement





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


Follow us on Twitter


© 2011 DaniWeb® LLC