Need help, having input problems.

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

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

Need help, having input problems.

 
0
  #1
Oct 23rd, 2009
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)
  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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 343
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 38
Clinton Portis's Avatar
Clinton Portis Clinton Portis is online now Online
Posting Whiz
 
1
  #2
Oct 23rd, 2009
  1.  
  2. if (StatsChange == "Strength" or "strength" or "str" or "STR" or "Str")

should be:
  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:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 29
Reputation: Shinedevil is an unknown quantity at this point 
Solved Threads: 0
Shinedevil Shinedevil is offline Offline
Light Poster
 
0
  #3
Oct 23rd, 2009
Thanks so much for the help. IT works! Thank you.
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