944,161 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3461
  • C++ RSS
Nov 6th, 2004
-1

Using while statement..any suggestions?

Expand Post »
Everything works well, but I couldn't figure out how to use the else statement in case the user inputs 0 as a number..any comments/suggestions?

C++ Syntax (Toggle Plain Text)
  1. /* Program description: Create a program that prompts for two integer values.
  2. The program will display whether the integers are either:
  3. - and +, + and +, or - and -, or 0.
  4. Author: Eric Martin
  5. Date: 8 November 2004 */
  6.  
  7. #include <iostream>
  8. #include <conio.h>
  9. using namespace std;
  10.  
  11. int main (){
  12. int Number1;
  13. int Number2; // Declare integers
  14. int Zero = 0;
  15.  
  16. cout << "USING POSITIVES AND NEGATIVES!";
  17. cout << endl;
  18. cout << endl;
  19. cout << "Please enter the first number: ";
  20. cin >> Number1;
  21. cout << "Please enter the second number: ";
  22. cin >> Number2;
  23. cout << endl;
  24.  
  25. while (Number1 >> 0) {
  26. if (Number1 > 0)
  27. cout << Number1 << " is a positive number!";
  28. if (Number1 < 0)
  29. cout << Number1 << " is a negative number!";
  30. break;
  31. }
  32. cout << endl;
  33.  
  34. while (Number2 >> 0) {
  35. if (Number2 > 0)
  36. cout << Number2 << " is a positive number!";
  37. if (Number2 < 0)
  38. cout << Number2 << " is a negative number!";
  39. break;
  40. }
  41.  
  42.  
  43. getch ();
  44. return 0;
  45. }
Last edited by alc6379; Nov 7th, 2004 at 5:35 pm. Reason: added [code] tags
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
dcving is offline Offline
20 posts
since Sep 2004
Nov 6th, 2004
-1

Re: Using while statement..any suggestions?

HEY WHY DONT YOU FORGET THE WHILE LOOPS. THE CODE BELOW GIVES U THE DESIRED OUTPUT

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <conio.h>
  3. using namespace std;
  4.  
  5. int main (){
  6. int Number1;
  7. int Number2; // Declare integers
  8. int Zero = 0;
  9.  
  10. cout << "USING POSITIVES AND NEGATIVES!";
  11. cout << endl;
  12. cout << endl;
  13. cout << "Please enter the first number: ";
  14. cin >> Number1;
  15. cout << "Please enter the second number: ";
  16. cin >> Number2;
  17. cout << endl;
  18.  
  19. if (Number1 > 0)
  20. cout << Number1 << " is a positive number!";
  21. else if (Number1 < 0)
  22. cout << Number1 << " is a negative number!";
  23. else
  24. cout << Number1 << " is ZERO!";
  25.  
  26. cout << endl;
  27.  
  28. if (Number2 > 0)
  29. cout << Number2 << " is a positive number!";
  30. else if (Number2 < 0)
  31. cout << Number2 << " is a negative number!";
  32. else
  33. cout << Number2 << " is ZERO!";
  34.  
  35.  
  36. getch ();
  37. return 0;
  38. }
Last edited by alc6379; Nov 7th, 2004 at 5:35 pm. Reason: added [code] tags
Reputation Points: 10
Solved Threads: 0
Light Poster
jigvesh is offline Offline
44 posts
since Oct 2004
Nov 6th, 2004
0

Re: Using while statement..any suggestions?

Oh... That makes it less complicated.. Thank you Jigvesh
Reputation Points: 11
Solved Threads: 0
Newbie Poster
dcving is offline Offline
20 posts
since Sep 2004
Nov 6th, 2004
0

Re: Using while statement..any suggestions?

>while (Number1 >> 0) {
"While the number is much greater than zero"? A bitwise shift doesn't appear to be something you would want in this program.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

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: Help with assignment
Next Thread in C++ Forum Timeline: Mathematical formulas used in C++ ?





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


Follow us on Twitter


© 2011 DaniWeb® LLC