Thread: illegal else without matching if HELP!!!!

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    12

    illegal else without matching if HELP!!!!

    this is the program i have and i keep getting these 3 errors when building it
    1>m:\my documents\visual studio 2005\projects\cobytown2.cpp(25) : error C2059: syntax error : ')'
    1>m:\my documents\visual studio 2005\projects\cobytown2.cpp(30) : error C2181: illegal else without matching if
    1>m:\my documents\visual studio 2005\projects\cobytown2.cpp(30) : error C2059: syntax error : ')'



    Code:
    // this program will calculate the viallage of cobytowns water bill for it's customers
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    using namespace std;
    int main()
    {
    	string type, name,acctyp;
    	const double metchar=5.00;
    	const double resident=.20;
    	const double business=.30;
    	double oldreading,newreading,waterused,totaldue,watercharge;
    	cout<<"Please enter the customers name:";
    	cin>>name;
    	cout<<"Please enter the customers account type:";
    	cin>>acctyp;
    	cout<<"Please enter the customers old reading:";
    	cin>>oldreading;
    	cout<<"Please enter the customers new reading:";
    	cin>>newreading;
    	waterused=newreading-oldreading;
    	                       
    	if (acctyp=)"R";
    	{
    		watercharge=(waterused/100)*resident;
    		type="resident";
    	}
    	else if (acctyp=)"B";
    	{
    		watercharge=(waterused/100)*business;
    		type="business";
    	}
    	cin.get();
    	cout<<"JOHN DOE"<<endl<<endl<<endl;
    	cout<<setw(60)<<"VILLAGE OF COBYTOWN WATER BILL\n\n\n\n";
    	cout<<setw(40)<<"Customers name:"<<name<<endl;
    	cout<<setw(40)<<"Customers account type:"<<acctyp<<endl<<endl;
    	cout<<setw(40)<<"Old reading:"<<oldreading<<endl;
    	cout<<setw(40)<<"New reading:"<<newreading<<endl<<endl;
    	cout<<setw(40)<<"Water used:"<<waterused<<endl<<endl;
    	cout<<setw(40)<<"Water charge:"<<watercharge<<endl;
    	cout<<setw(40)<<"Meter rental:"<<metchar<<endl;
    	cout<<setw(40)<<"---------"<<endl;
    	totaldue=metchar+watercharge;
    	cout<<setw(40)<<"TOTAL DUE:$"<<totaldue<<endl;
    
    
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> if (acctyp=)"R";
    Your syntax is wong here, the "R" should be inside the parentheses, you should be using ==, and there shouldn't be a semi-colon at the end.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    if (your_object == whatever_it_should_be_equal_to)
    {
    	// Code here
    }
    else if (new_if_here)
    {
    	// Code here
    }
    It's possible to do
    Code:
    if (my_if_here);
    But that would make an empty if and some compilers will warn you (like VC++). Almost always a bad usage and should be avoided.
    Anyway, that's the basic syntax on IFs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. Matching numbers
    By kirksson in forum C Programming
    Replies: 7
    Last Post: 07-23-2008, 01:51 PM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM