me again - different assigment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tsushio
    New Member
    • Feb 2009
    • 6

    me again - different assigment

    this


    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main ()
    {
    	int code,age,days;
    
       char category;
    
       cout << "\nAge: ";
       cin >> age;
    
       cout << "\nDays: ";
       cin >> days;
    
       if (age > 50)
       {
       	if (age > 55)
          	{
       			cout << "\nPatient's category: ";
       			cin >> category;
    
             	if (category == 'G',category == 'g')
                	cout << "Charges: " << days*5 << " \n";
    				   cout << "\nType of ward: Class 1\n";
    
                else
                {
                	if (category == 'N',category == 'n')
                	cout << "Charges: RM15";
                   cout << "\nType of ward: Class 2\n";
                }
    
             }
       	else
          	cout << "Charges: RM15";
       		cout << "\nType of ward: Class 2\n";
             cout << endl;
       }
       else
       {
          if (age < 50)
    
          cout << "\nCategory: ";
          cin >> category;
    
        	cout << "\nType of ward: ";
       	cin >> code;
    
        	if (category == 'G',category == 'g')
               {
               	if (code == 1)
                   	cout << "\nCharges: RM" << days*90 << " \n";
                   else if (code == 2)
                     	cout << "\nCharges: RM" << days*70 << " \n";
                   else if (code == 3)
                     	cout << "\nCharges: RM" << days*50 << " \n";
                   else
                   	cout << "UNDEFINED";
                }
            else  if (category == 'N',category == 'n')
                {
                	if (code == 1)
                   	cout << "\nCharges: RM" << days*120 << " \n";
                   else if (code == 2)
                     	cout << "\nCharges: RM" << days*90 << " \n";
                   else if (code == 3)
                     	cout << "\nCharges: RM" << days*70 << " \n";
                   else
                   	cout << "UNDEFINED";
                }
    
       }
    
       getch ();
    
       return 0;
    
    }
    the error is

    cp (27,17) : misplaced else

    -------


    i tried removing the else but the program did not run correctly.

    what mistake that i actually did?i really cant see it.T_T
    Last edited by JosAH; Feb 27 '09, 07:12 AM. Reason: fixed the [code] ... [/code] tags
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    You made some indentation there, but the line
    cout << "\nType of ward: Class 1\n";
    after which the 'else' is placed is not a part of 'if' expression.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      It's a matter of curly parentheses: as a starting programmer always put curly braces around the if-else statement(s)

      Code:
      if (<condition>) {
         // zero or more statements
      }
      else {
         // zero or more statements
      }
      Even seasoned programmers may be helped by this practice. Python is an exception to this rule but then again: Python is a silly language ;-)

      kind regards,

      Jos

      Comment

      • tsushio
        New Member
        • Feb 2009
        • 6

        #4
        i dont quite get it.T_T

        maybe..simpler. ...and...maybe with example?^_^

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by tsushio
          i dont quite get it.T_T

          maybe..simpler. ...and...maybe with example?^_^
          Always use those curly brackets as in the example in my previous reply.

          kind regards,

          Jos

          Comment

          Working...