Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 4th, 2008, 9:30 AM   #1
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 4 Arnack is on a distinguished road
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:
C++ Syntax (Toggle Plain Text)
  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?
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote
Old May 4th, 2008, 10:19 AM   #2
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4 Klarre is on a distinguished road
Re: Simple if statement question :(

Error:
if (sum > 10) && (sum >50)

Should be:
if ( (sum > 10) && (sum >50) )
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old May 4th, 2008, 10:38 AM   #3
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 4 Arnack is on a distinguished road
Re: Simple if statement question :(

Silly me
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?
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote
Old May 4th, 2008, 10:42 AM   #4
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 301
Rep Power: 4 Klarre is on a distinguished road
Re: Simple if statement question :(

Try changing system("PAUSE"); to cin.get();
__________________
http://www.klarre.se
Klarre is offline   Reply With Quote
Old May 4th, 2008, 10:59 AM   #5
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 4 Arnack is on a distinguished road
Re: Simple if statement question :(

Okay- all that happens is that when I input all 4 numbers the screen just exits.
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote
Old May 4th, 2008, 11:00 AM   #6
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 91
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
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.
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
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
Old May 4th, 2008, 11:31 AM   #8
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,869
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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).

Last edited by Sane; May 4th, 2008 at 11:42 AM.
Sane is offline   Reply With Quote
Old May 4th, 2008, 11:39 AM   #9
Arnack
Programmer
 
Join Date: Jul 2005
Posts: 66
Rep Power: 4 Arnack is on a distinguished road
Re: Simple if statement question :(

Thanks a lot
I will also try proper indenting techniques as well.
__________________
Ack Network
*UNDER CONSTRUCTION*
Now hiring staff of all sorts.
Arnack is offline   Reply With Quote
Old May 4th, 2008, 2:20 PM   #10
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 91
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Re: Simple if statement question :(

Here's your code indented and fixed up a bit.

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 = 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. }
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
very simple question. make button change number in edit box nickm Delphi 2 Apr 29th, 2006 10:47 PM
A simple programming question punter C# 8 Jan 5th, 2006 4:04 PM
A simple question Master C++ 9 Dec 24th, 2005 2:20 PM
Simple c# question nez C# 8 Jul 1st, 2005 6:29 PM
Simple (stupid cause I don't know it) basic question massive-war C++ 17 Apr 11th, 2005 11:03 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:43 AM.

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