You are here:

C++/C++ errors help

Advertisement


Question
Here is my code:

#include <fstream>
#include <iomanip>
#include <cmath>


using namespace std;

int main()
{
char fare;
char choice='Y';
int hrIn, minIn, hrOut, minOut;
int hours;
int minutes;
int total_minutes;
double cost;
float charge;
float additionalTime;

while(choice=='Y'||choice=='y')
{
cout << "\nThis program will calculate a single, couple, or group "
<< "\nfare amount which is then owed by the customer for the trip.\n"
<< "\nTo calculate a single fare amount, input S (or s)"
<< "\nTo calculate a couple fare amount, input C (or c)"
<< "\nTo calculate a group fare amount, input G (or g)"
<< " \n\nInput S, C, or G: ";
cin >> fare;
//------------------------------------
if(fare=='C'||fare=='c'){

cout << "\nWhat hour did the couple depart? ";
cin >> hrOut;
cout << "\nWhat minute did the couple depart? ";
cin >> minOut;
cout << "\nYou entered for departure: " << hrOut << ":" << minOut << endl;
cout << "\nWhat hour did the couple arrive? ";
cin >> hrIn;
cout << "\nWhat minute did the couple arrive? ";
cin >> minIn;
cout << "\nYou entered for arrival: " << hrIn << ":" << minIn << "\n" << endl;
cout << "A rickshaw departed at " << hrOut << ":" << minOut
<< " and arrived at " << hrIn << ":" << minIn << " with a couple of customers.\n" << endl;

if(hrIn <= hrOut && minIn <= minOut){

hours = (hrIn - hrOut) + 23;
cout << "The couple arrived in " << hours << " hour(s)";
minutes = (minIn - minOut) + 60;
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
else{

hours = (hrIn - hrOut) + 23;
cout << "The couple arrived in " << hours << " hour(s)";
minutes = (minIn - minOut);
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
}
if(hrOut <= hrIn && minOut <= minIn){

hours = (hrIn - hrOut);
cout << "The couple arrived in " << hours << " hour(s)";
minutes = (minIn - minOut);
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}
else{

hours = (hrIn - hrOut) + 24;
cout << "The couple arrived in " << hours << " hour(s)";
minutes = (minIn - minOut);
cout << " and " << minutes << " minute(s)";
total_minutes = (hours * 60) + minutes;
cout << ",\nor in a total of " << total_minutes << " minutes.\n" << endl;
}

if(total_minutes <=20){

charge = 10.50;
cout << "The couple will pay $" << charge << " dollars.\n" << endl;
}
else{

additionalTime = total_minutes - 20;
cout << "The couple will be charged for " << additionalTime << " extra minute(s).\n" << endl;
charge = 10.50 + (((total_minutes) - 20) * 2.50);
cout << "The couple will pay $" << charge << " dollars.\n" << endl;
}

}
//------------------------------------
else

cout << "\nThe character that you have entered is invalid. Please try again.\n" << endl;

cout << "Would you like to perform another calculation (Y/N)? ";
cin >> choice;

}

cout << "\nThank you for using the ODUSPORTS calculator!\n" << endl;

cout << " TTTTTTT H H A N N K K SSSS !! " << endl;
cout << " T H H A A NN N K K S !! " << endl;
cout << " T HHHH AAAAA N N N KK SSSS !! " << endl;
cout << " T H H A A N NN K K S " << endl;
cout << " T H H A A N N K K SSSS !! " << endl;


system("pause");
return 0;
}


Errors: line 99 expected primary-expression before "else"
line 99 expected `;' before "else"
line 99 99 At global scope:

Help me out with these errors please.

Answer
Joe, the structure of your current code is this:

main()
{
  while()
  {
  }
  else
  cout << "\nThe character that you have entered is invalid. Please try again.\n" << endl;
  cout << "Would you like to perform another calculation (Y/N)? ";
  cin >> choice;
}
cout << "\nThank you for using the ODUSPORTS calculator!\n" << endl;


You can see that the else is right after the while loop and there is no "if" to match the else. Also you have a closing brace ending your main program, followed by more lines of code.

It seems like what you really want is to ask the user for input at the end of the loop, so look carefully at your program and you will see that at the end of the while loop, but still inside the while loop, you need to print the prompt, get the input, and check it. So the structure is something like this (in pseudo code):

main()
{
  while(choice=='Y'||choice=='y')
  {
      ....

      
      while(true)
      {
         cout << "Would you like to perform another calculation (Y/N)? ";
         cin >> choice;
         if choice is not Y and choice is not N then
         {
         cout << "\nThe character that you have entered is invalid. Please try again.\n" << endl;
         }
         else break;
      }

  }

  cout << "\nThank you for using the ODUSPORTS calculator!\n" << endl;
}

I would take the whole inner loop for prompting the user and put it into a separate function so that it does not clutter the main program. Something like this:

char prompt()
{
     char choice;
     while(true)
      {
         cout << "Would you like to perform another calculation (Y/N)? ";
         cin >> choice;
         if choice is not Y and choice is not N then
         {
         cout << "\nThe character that you have entered is invalid. Please try again.\n" << endl;
         }
         else break;
      }
      return choice;
}

Then the main would look like this:

main()
{
  while(choice=='Y'||choice=='y')
  {
      ....

      
      choice = prompt()
  }

  cout << "\nThank you for using the ODUSPORTS calculator!\n" << endl;
}

I hope that helps you out.

Best regards
Zlatko

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2016 About.com. All rights reserved.