By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Malformed if statements and program flow help

P: 8
the idea here is to eliminate all solutions except for the square. I am stuck in trying to figure out how to utilize the if statement in the choice. I know its probably simple but its driving me crazy. any sugestins on how to make this better would be appreciated be specific do i need to remove something or add thank you

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. const int SQUARE = 1;
  7. const int TRIANGLE = 2;
  8. const int RECTANGLE = 3;
  9. const int CIRCLE = 4;
  10.  
  11. int main()
  12. {
  13.  
  14.     // Declare Variable
  15.  
  16.     int multiply;
  17.     int number5;
  18.     int number6;
  19.  
  20.     // Declare Choice
  21.  
  22.     int choice;
  23.  
  24.  
  25.     // Display Choice
  26.  
  27.         cout << " What is the area of a square, triangle, rectangle, or circle? " ;
  28.         cout << " Make your choice? /n " ;        
  29.         cout << SQUARE << " ) Square: /n " ;
  30.         cout << TRIANGLE << " ) Triangle: /n " ;
  31.         cout << RECTANGLE << " ) Rectangle: /n " ;
  32.         cout << CIRCLE << " ) Circle: /n "  ;
  33.  
  34.  
  35.  
  36.     // Get users choice
  37.  
  38.         cin  >> choice ;
  39.  
  40.     // Display Proper feedback based on users choice          
  41.  
  42.  
  43.         if (choice == TRIANGLE ) RECTANGLE ; CIRCLE; {
  44.               cout << " Choose another: /n" ;
  45.  
  46.         }
  47.  
  48.             if ( choice == SQUARE ) ;{
  49.            cout << "The area of a square is: /n" ;
  50.            }
  51.  
  52.     // Get one side
  53.                 cout << " Enter one side: /n ";
  54.                 cin >> number5;
  55.  
  56.     // Get the other side
  57.                 cout << " Enter other side: /n";
  58.                 cin >> number6;
  59.  
  60.     // Multiply one side by the other
  61.                 multiply = number5 * number6 ;
  62.  
  63.     // Display sum
  64.  
  65.             cout <<  "Mulitply is " << multiply << endl;
  66.  
  67.  
  68. system ("pause");
  69.  
  70.     return 0;
  71.  
  72.  
  73. }
Sep 18 '10 #1
Share this Question
Share on Google+
5 Replies


Banfa
Expert Mod 5K+
P: 8,916
if (choice == TRIANGLE ) RECTANGLE ; CIRCLE; {

is mal formed, you need to check choice individually against each value you are interested in using the logical operators (&& ||) etc for example

Expand|Select|Wrap|Line Numbers
  1. int a;
  2.  
  3. cin >> a;
  4.  
  5. if (a == 4 || a == 8)
  6. {
  7.     cout << "a is either 4 or 8) << endl;
  8. }
  9.  
On line 48 you have a ; which will stop the if statement doing what you want.

I think lines 52 - 65 should be inside the code block for the if ( choice == SQUARE ) condition.
Sep 18 '10 #2

P: 8
ok I dont get how to do exactly what you mean when i try to set it up that way it has run errors so I am still missing something here
Sep 18 '10 #3

100+
P: 542
Expand|Select|Wrap|Line Numbers
  1. if(choice=TRIANGLE||choice=RECTANGLE||... )choice=SQUARE;
  2. Maybe enum shape = {SQUARE,TRIANGLE......};//might be neater
Sep 19 '10 #4

P: 8
anymore suggestions here really could use some help
Sep 20 '10 #5

Banfa
Expert Mod 5K+
P: 8,916
Err no other than writing you if statement the correct way you are unlikely to get further suggestions.

Perhaps you should post the corrected code and the run time errors you are getting.
Sep 20 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.