Oct 30, 2010 at 5:42pm UTC
// 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;
}
Oct 30, 2010 at 5:54pm UTC
String literals must be enclosed in quotation marks, not single quotes.
Oct 30, 2010 at 5:56pm UTC
And change char shippingMethod;
to string shippingMethod;
because you want to get a string from user, not a single character.
Oct 30, 2010 at 6:46pm UTC
#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
Oct 30, 2010 at 6:57pm UTC
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 Oct 30, 2010 at 6:58pm UTC
Oct 30, 2010 at 7:24pm UTC
thank all of you so much!! it finally
Nov 23, 2010 at 10:26am UTC
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!
Nov 23, 2010 at 11:01am UTC
a3333 thnks.. it worked finally! i still hv so many 2 ask so i can pass this test! :S
Nov 23, 2010 at 6:51pm UTC
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