943,188 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 310
  • C++ RSS
Oct 8th, 2009
2

If statement confusion

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main()
  5. {
  6. // Variables for used for user input
  7. int nbase;
  8.  
  9.  
  10. // Users input is needed but no error checking is done as per lab instuctions
  11. cout << "Please enter your choice for conversion. The letter O for octal OR B for binary!" << endl;
  12. cin >> nbase;
  13.  
  14. if (nbase == 2 || nbase == 8)
  15. {
  16. int total;
  17. int r3;
  18. int r2;
  19. int r1;
  20. int r0;
  21. int convert;
  22. // char lbase;
  23. cout << "Please enter your four digit number!" << endl;
  24. cin >> total;
  25.  
  26. // The below math calculations break down the full four digit number so the user does not
  27. // have to enter 4 individual numbers.
  28. r3 = total / 1000;
  29. total = total - (r3 * 1000);
  30. r2 = total / 100;
  31. total = total - (r2 * 100);
  32. r1 = total / 10;
  33. total = total - (r1 * 10);
  34. r0 = total;
  35.  
  36. if (nbase == 2 && r3 && r2 && r1 && r0 <= 1 && r3 && r2 && r1 && r0 >= 0)
  37. {
  38. // This coverts the users numbers into the base specified earlier
  39. convert = nbase * nbase * nbase * r3 + nbase * nbase * r2 + nbase * r1 + r0;
  40. cout << "Your number converted to decimal is " << convert << endl;
  41. }
  42. else if (nbase == 8 && r3 && r2 && r1 && r0 <= 7 && r3 && r2 && r1 && r0 >= 0)
  43. {
  44. // This coverts the users numbers into the base specified earlier
  45. convert = nbase * nbase * nbase * r3 + nbase * nbase * r2 + nbase * r1 + r0;
  46. cout << "Your number converted to decimal is " << convert << endl;
  47. }
  48. else
  49. {
  50. cout << nbase << r3 << r2 << r1 << r0 << endl;
  51. cout << "You did not enter a valid number, try again" << endl;
  52. }
  53. }
  54. else
  55. cout << "You did not enter 2 or 8, try again" << endl;
  56.  
  57. cin.get();
  58. return 0;
  59. }

I'm having trouble on lines 36 and 42 of my code. No errors occur when I compile but I cannot get line 36 and 42 to work the way I want. When I pick 1010 for my four digit binary number it does not work but 1111 works fine. I already tried to cout all my variables around line 35 just before the If statement so I can make sure nothing is assigned incorrectly. All my variables seem to be alright and when I work out the math portion on paper it seems to be ok. Any advice would be appeciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Tjvelcro is offline Offline
1 posts
since Oct 2009
Oct 8th, 2009
0
Re: If statement confusion
If you intend to check if each r value is less than or equal to 1 then you must say
C++ Syntax (Toggle Plain Text)
  1. if(r3 <= 1 && r2 <= 1 && r1 <= 1 && r0 <= 1 )...
Do this for each of your statements that are not working correctly.
This should resolve your problem.
Is this your problem?
Last edited by Grn Xtrm; Oct 8th, 2009 at 7:46 pm.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008

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: C++ TicTacToe
Next Thread in C++ Forum Timeline: String from file editing desperate help needed =(





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


Follow us on Twitter


© 2011 DaniWeb® LLC