problem with if else statement

// I am trying to code for shipping methods, but I keep getting this error
//Error 1 error C2015: too many characters in constant//

#include<iostream>
#include<string>





using namespace std;
double rate;
void getUPSShipping(double weight, int zipCode, double& shippingCost);
void getFedExShipping(double weight, int zipCode, double& shippingCost);


int main()

{
double weight, shippingCost;
int zipCode;

cout<<"Enter weight: ";
cin>>weight;
cout<<"Enter Zip Code: ";
cin>>zipCode;

char shippingMethod;
cout<<"Enter Shipping Method: ";
cin>>shippingMethod;

if (shippingMethod == 'UPS'|| shippingMethod == 'ups')
{
getUPSShipping(weight, zipCode, shippingCost);
cout<<"shippingCost is "<<shippingCost<<" for UPS"<<endl<<endl;
}

if (shippingMethod == 'FedEx'|| shippingMethod == 'fedex')
{
getFedExShipping(weight, zipCode, shippingCost);
cout<<"shippingCost is "<<shippingCost<<" for FedEx"<<endl;
}








return 0;
}

void getUPSShipping(double weight, int zipCode, double& shippingCost)
{
rate = 4.5;
shippingCost = weight * rate;
}

void getFedExShipping(double weight, int zipCode, double& shippingCost)
{
rate = 5.6;
shippingCost = weight * rate;
}
String literals must be enclosed in quotation marks, not single quotes.
And change char shippingMethod; to string shippingMethod; because you want to get a string from user, not a single character.
#include<iostream>
#include<string>
using namespace std;


double rate;
void getUPSShipping(double weight, int zipCode, double& shippingCost);
void getFedExShipping(double weight, int zipCode, double& shippingCost);


int main()
{
double weight, shippingCost;
int zipCode;

cout<<"Enter weight: ";
cin>>weight;
cout<<"Enter Zip Code: ";
cin>>zipCode;

string shippingMethod;
cout<<"Enter Shipping Method: ";
cin>>shippingMethod;

if (shippingMethod == "UPS"|| shippingMethod == "ups");
{
getUPSShipping(weight, zipCode, shippingCost);
cout<<"shippingCost is "<<shippingCost<<" for UPS"<<endl;
}
else if (shippingMethod == "FedEx"|| shippingMethod == "fedex");
{
getFedExShipping(weight, zipCode, shippingCost);
cout<<"shippingCost is "<<shippingCost<<" for FedEx"<<endl;
}
else
cout<<"NO";


return 0;
}

void getUPSShipping(double weight, int zipCode, double& shippingCost)
{
rate = 4.5;
shippingCost = weight * rate;
}

void getFedExShipping(double weight, int zipCode, double& shippingCost)
{
rate = 5.6;
shippingCost = weight * rate;
}


Now I am getting these errors:

Error 2 error C2181: illegal else without matching if
Error 4 error C2181: illegal else without matching if

1
2
3
4
5
6
7
8
9
10
if (shippingMethod == "UPS"|| shippingMethod == "ups"); //DELETE THE ;
{
getUPSShipping(weight, zipCode, shippingCost);
cout<<"shippingCost is "<<shippingCost<<" for UPS"<<endl;
}
else if (shippingMethod == "FedEx"|| shippingMethod == "fedex"); //SAME HERE.
{
getFedExShipping(weight, zipCode, shippingCost);
cout<<"shippingCost is "<<shippingCost<<" for FedEx"<<endl;
}


;)
Do you see why?

-Albatross
Last edited on
thank all of you so much!! it finally
umm.. help.. am seriously dumb at ths i dnt hv a clue nd my midterms are after 2morrw. wts wrong with this:

#include <iostream.h>
int main ()
{
double a,b,c,d=a*b;




cout<<"plz enter the nbr of hours worked (-1 2 end):";
cin>>a;

cout<<"plz enter the amount of $/hour: ";
cin>>b;

if (a<=40)
cout << " your salary is $ : "<<d;

cout<<d;
else
cout<<"you salary is $: "<<(40*b+(a-40)*(1.5*b));




return 0;
}

?!?! it keep on telln me tht there is an illlegal else wtht matching if!
If it's a larger statement after if(...) put {...} and after that else

1
2
3
4
5
6
7
if (a<=40) {
cout << " your salary is $ : "<<d;

cout<<d; 
}
else
cout<<"you salary is $: "<<(40*b+(a-40)*(1.5*b));
a3333 thnks.. it worked finally! i still hv so many 2 ask so i can pass this test! :S
am sry i posted this here, but i ddnt knw how 2 open a new one..
anyway my question is...in this :
#include <iostream.h>
int main ()
{float NG,NM,MPG,TMPG=0;

while (NM!=1)
{


cout<<"enter the miles used (-1 to end):";
cin>>NM;




if (NM!=-1)
{cout<<"enter gallons";
cin>>NG;
MPG=NM/NG;
cout<<"miles/gallons this tankful:"<<MPG<<endl;

if(TMPG==0)
TMPG=MPG;
else
TMPG=(TMPG+MPG)/2;
cout<<"total MPG:"<<TMPG<<endl<<endl;
}



}
return 0;
}


the bold sentnce.. what is the difference if we used one "=" instead of two? and why do we even use the two "=="?
thnks
hi,

"=" is an assignment operator, "==" is a comparison operator.

when you write this:
 
if(TMPG==0)


1) TMP gets assigned the value 0
2) the operation succeded so the if condition resolves to TRUE

in other words, you are not testing the value of TMPG, you are testing the operation of assigning 0 to TMPG.
Topic archived. No new replies allowed.