if-else Statement giving errors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Speaker
    New Member
    • Oct 2008
    • 2

    if-else Statement giving errors

    I am receiving a few errors when I try to run my program and I can't figure out what I am doing wrong. I'm coding in C++ with minGW on Vista SP1.

    Here is my code:
    Code:
    #include <iostream>
    using namespace std;
    
    const float stanCatA = 3.00;
    const float stanCatB = 1.45;
    const float stanDestination = 2.50;
    
    int main()
    {
    	int shipMeth;
    	int itemAmount;
    	int itemWeight;
    	char cat, A, B, destination, y, n;
    	
    	cout << "Author: Dax Allen"
    		 << "\nProject 03"
    		 << "\nSection 002"
    		 << "\n***************************" << endl;
    	
    	cout << "\nPlease tell me what shipping method you want: "
    		 << "\nEnter 1 - for Standard Shipping."
    		 << "\nEnter 2 - for Express Shipping."
    		 << "\nEnter 3 - for Same Day Shipping."
    		 << "\nType your choice (1, 2, or 3): ";
    	cin >> shipMeth;
    	
    	cout << "\n";
    	if ( shipMeth == 1 )
    	{
    		
    		
    		cout << "\nThank you."
    			 << "\nIs this shipment in category A or category B?"
    			 << "\nType your choice (A or B): ";
    		cin >> cat;
    		
    		if( cat == A )
    		{
    			cout << "\n";
    	
    			cout << "\nThank you."
    				 << "\nHow many items are in this shipment? ";
    			cin >> itemAmount;
    	
    			cout << "\n";
    		
    			cout << "\nThank you."
    				 << "\nIs this shipment going to Alaska or Hawaii (y or n)? ";
    			cin >> destination;
    			
    			if( destination == y )	
    			{
    				cout << "\n";
    		
    				cout << "\nThank you ... calculating shipping costs...";
    				
    				float stanCatADestYesTotal = itemAmount * stanCatA + stanDestination;
    				
    				cout << "\nStandard Shipping"
    					 << "\n" << itemAmount << " Items"
    					 << "\nSurcharge $" << stanDestination
    					 << "\nTotal Shipping Cost: $" << stanCatADestYesTotal << endl;
    			}
    			else( destination == n )	
    	               {
    				cout << "\n";
    		
    				cout << "\nThank you ... calculating shipping costs...";
    				
    				float stanCatADestNoTotal = itemAmount * stanCatA;
    				
    				cout << "\nStandard Shipping"
    					 << "\n" << itemAmount << " Items"
    					 << "\nTotal Shipping Cost: $" << stanCatADestNoTotal << endl;
    			}
                    }
    		else ( cat == B )
                   {
    			cout << "\n";
    	
    			cout << "\nThank you."
    				 << "\nHow much does this shipment weigh? ";
    			cin >> itemWeight;
    	
    			cout << "\n";
    		
    			cout << "\nThank you."
    				 << "\nIs this shipment going to Alaska or Hawaii (y or n)? ";
    			cin >> destination;
    			
    			if( destination == y )	
    			{
    				cout << "\n";
    		
    				cout << "\nThank you ... calculating shipping costs...";
    				
    				float stanCatBDestYesTotal = itemWeight * stanCatB + stanDestination;
    				
    				cout << "\nStandard Shipping"
    					 << "\n" << itemWeight << " lbs"
    					 << "nSurcharge $" << stanDestination
    					 << "\nTotal Shipping Cost: $" << stanCatBDestYesTotal << endl;
    			}
    		        else ( destination == n )
                           {
    				cout << "\n";
    		
    				cout << "\nThank you ... calculating shipping costs...";
    				
    				float stanCatBDestNoTotal = itemWeight * stanCatB;
    				
    				cout << "\nStandard Shipping"
    					 << "\n" << itemWeight << " lbs"
    					 << "\nTotal Shipping Cost: $" <<                               stanCatBDestNoTotal << endl;
    			}
    		}
            }
    	system("PAUSE");
    	return 0;
    }
    When I try and run that code I get the following errors and warnings:

    error: Expected ' ; ' before ' { ' token on line 65
    warning: statement has no effect on line 76
    error: expected ' ; ' before ' { ' token on line 78
    warning: statement has no effect on line 117

    Can someone tell me what I am doing wrong
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    On lines like
    else( destination == n )
    you should instead have:
    else if( destination == n )
    You can only use use else without any condition.


    And also,
    char cat, A, B, destination, y, n;
    don't declare all the letters you're using.
    instead, just declare the variables you need,
    char cat, destination;
    and use constant chars by putting them in single quotes:
    else if( destination == 'n' )

    Hope this helps.

    Comment

    • Speaker
      New Member
      • Oct 2008
      • 2

      #3
      Thank you very much that fixed the issue.

      Comment

      Working...