View Single Post
Old May 4th, 2008, 11:25 AM   #7
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 4 Arnack is on a distinguished road
Re: Simple if statement question :(

Thanks, peaceofpi. That was helpful and I've fixed my problem; yes I want to get deeper and more complicated in this simple program I have built.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main(void)
  6. {
  7. system("TITLE Addition");
  8. system("COLOR 3");
  9. int number1;
  10. int number2;
  11. int number3;
  12. int number4;
  13. int sum;
  14. int one = 1;
  15. string yesno;
  16. string relay;
  17.  
  18. while (true)
  19. {
  20. cout<<"I'm going to add these numbers you give me. First Number: ";
  21. cin>>number1;
  22. cout<<"next number: ";
  23. cin>>number2;
  24. cout<<"and the next number: ";
  25. cin>>number3;
  26. cout<<"and the last number: ";
  27. cin>>number4;
  28. sum=(number1+number2+number3+number4);
  29.  
  30.  
  31. if(sum < 10) {
  32. relay = "small";
  33. cout<<"Your number is " << sum << " and it is a " << relay << " number. \n";
  34. }
  35. else if ( sum > 10 && sum < 50 ) {
  36. relay = "big";
  37. cout<<"Your number is " << sum << " and it is a " << relay << " number. \n";
  38. }
  39. else if (sum > 50 && sum <150 ) {
  40. relay = "large";
  41. cout<<"Your number is " << sum << " and it is a " << relay << " number. \n";
  42. }
  43. else {
  44. cout<<"You have a really big number \n" ;
  45. }
  46.  
  47. cout<<"Try again? Y/N: ";
  48. cin>> yesno;
  49. if (yesno == "Y") {
  50. return 0;
  51. }
  52. else if (yesno == "N") {
  53. system("pause");
  54. }
  55. }
  56. }
As you see here, I have added a "Try Again?" question at the end of the program. I have ran into a problem, though. When I type in "Y" it simply exits the program. When I type in "N" it asks me to press any key to continue, and then it starts the program over again.
But what I have done is basically reversed the Y/N if statements:
C++ Syntax (Toggle Plain Text)
  1. if (yesno == "Y") {
  2. system("pause");
  3. }
  4. else if (yesno == "N") {
  5. return 0;
which works perfectly.
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote