primary expression before else

#include <iostream>
#include <string>
using namespace std;
int main()
{
string accountNumber;
char serviceCode;
int noOfMins_daily,noOfMins_day,noOfMins_night;
double amountDue;

cout<<"Please enter your account number :"<<endl;
cin>>accountNumber;

cout<<"Please enter your service code "<<endl;
cin>>serviceCode;

{
if (serviceCode == 'r' || 'R')
cout<<"Please enter the number of minutes you have used : "<<endl;
cin>>noOfMins_daily;

else if (serviceCode == 'p' || 'P')
cout<<"Please enter the number of minutes you have used during the day : "<<endl;

cout<<"Please enter the number of minutes you have used during night : "<<endl;


else
cout<<"Input error!!"<<endl;
cin.clear();
cin.ignore(200,'\n');
}

return 0;
}

help me!!
i dunno where is the mistake!!
plz help me to figure out!!


ok.. first thing.. please use [code][/code] tags around the code. This formats it and makes it easier for us to read.

Second, on your if, else if, and else statements, if there is mroe than one command (as indicated by more than one semi-colon ';') then you need to put parenthasis around the block of code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (serviceCode == 'r' || serviceCode == 'R') // notice i compare twice
{
  cout <<"Please enter the number of minutes you have used : " << endl;
  cin >> noOfMins_daily;
}
else if (serviceCode == 'p' || serviceCode == 'P')
{
  cout << "Please enter the number of minutes you have used during the day : " << endl;
  cout << "Please enter the number of minutes you have used during the night : " << endl;
}
else
{
  cout << "Input error!!" << endl;
  cin.clear();
  cin.ignore(200,'\n');
}
return 0;


You can only skip the parenthasis if you have one command, one operation, one line of code to follow the if statement.
1
2
3
4
5
6
7
8
9
If (false)
   cout << "this line is part of the if statement" << endl; // this will not print
   cout << "this line is not part of the if statement" << endl; // this will print

If (true)
{
   cout << "this line is part of the if statement" << endl;
   cout << "and so is this line." << endl;
}

Last edited on
skip the parentheses


You mean the curly braces. (Minor error, I know...)
Aakanaar's article about conditions being true or false will explain what exactly you have wrong...
http://www.cplusplus.com/forum/articles/3483/
(Aakanar's answer has the error corrected)
Topic archived. No new replies allowed.