Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Simple if statement question :( (http://www.programmingforums.org/showthread.php?t=15774)

Arnack May 4th, 2008 9:30 AM

Simple if statement question :(
 
I haven't used C++ in a while, and right when I get back to starting up again, I fall upon this simple error in my simple code here:
:

  1. #include <iostream.h>
  2. #include <stdio.h>
  3. #include <string>
  4. using namespace std;       
  5.  
  6. int main(void)
  7. {
  8. int number1;
  9. int number2;
  10. int number3;
  11. int number4;
  12. int sum;
  13. string relay;
  14.  
  15. cout<<"I'm going to add these numbers you give me. First Number: ";
  16. cin>>number1;
  17. cout<<"next number: ";
  18. cin>>number2;
  19. cout<<"and the next number: ";
  20. cin>>number3;
  21. cout<<"and the last number: ";
  22. cin>>number4;
  23. sum=(number1+number2+number3+number4);
  24.  
  25. {
  26. if(sum < 10){
  27.       relay = "small.";
  28. }
  29. if (sum > 10) && (sum >50)
  30. {
  31.       relay = "big.";
  32. }
  33.  
  34.  
  35. cout<<"Your number is " << relay << "\n";
  36.  
  37.  
  38. system("PAUSE");

It comes out with errors such as: expected
`;' before '(' token
expected identifier before '(' token

Any help, please?

Klarre May 4th, 2008 10:19 AM

Re: Simple if statement question :(
 
Error:
if (sum > 10) && (sum >50)

Should be:
if ( (sum > 10) && (sum >50) )

Arnack May 4th, 2008 10:38 AM

Re: Simple if statement question :(
 
Silly me :icon_redface:
Thanks... and just out of curiosity, how can I get the program to go back to the start after ending instead of "Press any key to continue" and the command prompt exits?

Klarre May 4th, 2008 10:42 AM

Re: Simple if statement question :(
 
Try changing system("PAUSE"); to cin.get();

Arnack May 4th, 2008 10:59 AM

Re: Simple if statement question :(
 
Okay- all that happens is that when I input all 4 numbers the screen just exits.

peaceofpi May 4th, 2008 11:00 AM

Re: Simple if statement question :(
 
1. You don't need stdio.h (it should be cstdio for c++ anyway).
2. iostream.h should be iostream
3. As for your last question: place everything from the first to the last cout in a while (true) { } loop or something of the sort.
4. Don't forget to return 0; and close main.

Arnack May 4th, 2008 11:25 AM

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.
:

  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:
:

  1. if (yesno == "Y") {
  2. system("pause");
  3. }
  4. else if (yesno == "N") {
  5. return 0;

which works perfectly.

Sane May 4th, 2008 11:31 AM

Re: Simple if statement question :(
 
:

if (yesno == "Y") {
return 0;
}
else if (yesno == "N") {
system("pause");
}


The command "return" exits from main. Therefore, if they enter "Y", main is exited. You want the program to terminate if they hit "N", otherwise ignore the input.

:

        cout << "Try again? Y/N: ";
        cin >> yesno;
        if(yesno == "N") {
            return 0;
        }
    }
}


By the way, you should probably practice proper indenting style. It makes code a lot more readable. One popular method is 4 spaces per block, keeping the open bracket on the same line as what opens the block, and the close bracket on a new line a level down (as shown in my posted code).

Arnack May 4th, 2008 11:39 AM

Re: Simple if statement question :(
 
Thanks a lot :)
I will also try proper indenting techniques as well.

peaceofpi May 4th, 2008 2:20 PM

Re: Simple if statement question :(
 
Here's your code indented and fixed up a bit.

:

  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 = 0, number2 = 0, number3 = 0, number4 = 0, sum = 0;
  10.     // int one = 1; <-- never used
  11.     string yesno;
  12.     // string relay; <-- useless
  13.     bool bCont = true;
  14.     while (bCont) {
  15.         cout<<"I'm going to add these numbers you give me. First Number: ";
  16.         cin>>number1;
  17.         cout<<"next number: ";
  18.         cin>>number2;
  19.         cout<<"and the next number: ";
  20.         cin>>number3;
  21.         cout<<"and the last number: ";
  22.         cin>>number4;
  23.         sum = number1 + number2 + number3 + number4;
  24.  
  25.         if(sum < 10)
  26.             cout<<"Your number is " << sum << " and it is a small number. \n";
  27.         else if ( sum > 10 && sum < 50 )
  28.             cout<<"Your number is " << sum << " and it is a big number. \n";
  29.         else if (sum > 50 && sum <150 )
  30.             cout<<"Your number is " << sum << " and it is a large number. \n";
  31.         else
  32.             cout<<"You have a really big number \n" ;
  33.  
  34.         cout<<"Try again? Y/N: ";
  35.         cin>> yesno;
  36.         if (yesno == "Y" || yesno == "y")
  37.             continue;
  38.         else
  39.             bCont = false;
  40.     }
  41.     cin.get();
  42.     return 0;
  43. }



All times are GMT -5. The time now is 12:30 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC