problem, uninitialized local variable

i had my practical test for C++ and i have no idea why i cant go to "else if('E') and also for else if 'n' condition and there is a warning sign there said uninitialized local variable 'E' and 'n'.

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int weight,distance;
double rate,total;
char N,E,y,n;

cout<<"Program will determine the total cost based on distance and weight"<<endl;
cout<<"Please enter weight in kg:"<<endl;
cin>>weight;
cout<<"Please enter the distance:"<<endl;
cin>>distance;
cout<<"Please enter the type of post (N/E)"<<endl;
cin>>N || E;
if('N')
{
if(weight>=1 && weight<=5)
{
if(distance>=1 && distance<=99)
rate = 1.20;

else
if(distance>=100 && distance<=299)
rate = 1.80;
else
if(distance>=300)
rate = 2.50;
}
else
if(weight>=6&& weight<=30)
{
if(distance>=1 &&distance<=199)
rate=2.20;

else
if (distance>=200)
rate=2.50;

}
else
if(weight>=30)
{
if(distance>=1)

rate=4.50;
}

total = weight*rate;
cout<<"total charge:"<<fixed<<setprecision(2)<<total<<endl;
}

else
if('E')
{
if(distance>=0 && distance<=20)
{

total=(2.80)*weight;
}

else
if(distance>=21 && distance<=50)
{
total=(1* weight)+((2.80)*(weight));
}

else
if(distance>=50)
{
total=((1.50)*weight)+(1* weight)+((2.80)*weight);
}



}
cout<<"Is it under corperate member account? (y/n):"<<endl;
cin>>y ||n;
if('y')
{total=total-(total*5/100);
cout<<fixed<<setprecision(2)<<total<<endl;}
else
if('n')
cout<<"total charge"<<fixed<<setprecision(2)<<total<<endl;

system("PAUSE");
return 0;
}
please use code tags!!

you don't understand how if works

read it like this:

you: if 'N' then do this..
compiler: what 'N'?.. wht is it?

the compiler does not know what you mean.

what you need to do is give a expression... if the expression is true it will be executed
example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int miain()
{
  char a = 'a';
  if(a == 'a')
  {
     //this will be executed
  }

  if(a=='b')
  {
    //this will not be executed
  }

  return 0;
}


and another thing:

cin>>N || E; is not right.
cin is you keyboard and the character is trasfered into the variable on the right... example:

1
2
3
4
5
6
char newChar;
cin >> newChar;
if(newChar == 'N')
{
  //do stuff when the input is N
}

Last edited on
I think maybe you intended to use if(E) instead of if('E'). if(E) checks the information stored in the variable with the name E, whereas if('E') refers to the literal 'E'.

Also, please use the code tags when posting code, it makes it way easier to read and provides line numbers. To do that, use the Format button for code when you are posting
ohh now i see, thx! sry for that because this is the first time i use char , so didnt know it was like that.
didnt know it was like that.


all types are like that...
oh, now i get it, sry because im still new to C++ lol
Last edited on
Topic archived. No new replies allowed.