C++ Basic Help

h4rdw1re

Beta member
Messages
3
Location
USA
I just started my intro to programming class last week, and my professor ran out of time to check my work for a grade (pass/fail) so could someone check this code for me and see if it looks correct?
Code:
// This program calculates your total Restaurant Bill
// Inputs :  mealCharge
//           taxRate
//           tipRate

// Outputs : mealCharge
//           taxAmount
//           tipAmount (calculate meal cost + tax)
//           totalBill

#include <iostream>
#include <iomanip>
using namespace std;

void main()
{
  // Input variables
  double mealCharge, taxRate, tipRate;

  // Output variables
  double taxAmount, tipAmount, totalBill; 

  // set formatting
  cout << fixed << setprecision (2) << showpoint;

  // Get the cost of the meal.
  cout << "What is the total cost of your meal?";
  cin  >> mealCharge;
  
  // Get the tax rate.
  cout << "What is the local tax rate?";
  cin  >> taxRate;

  // Get the tip rate.
  cout << "What is the suggested tip rate?";
  cin  >> tipRate;

  taxRate = (taxRate / 100);
  tipRate = (tipRate / 100);
  taxAmount = taxRate * mealCharge;
  tipAmount = tipRate * (mealCharge + taxAmount);
  totalBill = mealCharge + taxAmount + tipAmount;

  //Calculate the input
  cout << "Your meal starts at $" << mealCharge;
  cout << ".\n";
  cout << "Your tax is $" << taxAmount;
  cout << ".\n";
  cout << "Your tip is $" << tipAmount;
  cout << ".\n";
  cout << "Your total charge is $" << totalBill;
  cout << ".\n";
}
 
Last edited by a moderator:
Usually you want to have main return an int and not necessarily be void, and have "return 0;" at the end. But that's just how I was taught.

Code:
int main()
{
     //code 
     return 0;
}

Otherwise...as far as just looking at it without compiling it looks fine. Have you tried compiling it and running it to see if it works...?
 
I did compile it and run it. It worked OK, I just wasn't sure if there was some redundancy in there that I could change or get rid of (such as the ".\n" at the end of all my outputs).

Our professor prefers us to use the void main so we don't have to remember to use the return 0.

Is there a difference between the two versions?
If so, could someone explain it to me?
 
I did compile it and run it. It worked OK, I just wasn't sure if there was some redundancy in there that I could change or get rid of (such as the ".\n" at the end of all my outputs).

Well you could include it on the previous line of your output instead of as a separate cout statement.

Example:
Instead of:
Code:
cout << "Your meal starts at $" << mealCharge;
cout << ".\n";
you could have:
Code:
cout << "Your meal starts at $" << mealCharge << ".\n";

Does the same thing, but just cleaner.

Our professor prefers us to use the void main so we don't have to remember to use the return 0.

Is there a difference between the two versions?
If so, could someone explain it to me?

Because that's how it's specc'd. Just because your teacher says so, doesn't always mean it's right ;).

From:
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?id=1043284376&answer=1044841143

above link said:
What's the deal with void main()

Under regular function calling/returning in C and C++, if your don't ever want to return anything from a function, you define it's return type as void. For example, a function that takes no arguments, and returns nothing can be prototyped as:

void foo(void);

A common misconception is that the same logic can be applied to main(). Well, it can't, main() is special, you should always follow the standard and define the return type as int. There are some exceptions where void main() is allowed, but these are on specialised systems only. If you're not sure if you're using one of these specialised systems or not, then the answer is simply no, you're not. If you were, you'd know it.

Be warned that if you post your "void main" code on the forums, you're going to get told to correct it. Responding with "my teacher said it's OK" is no defence; teachers have a bad habit of being wrong. Be safe, and post only standard code, and you'll find people concentrate on answering your other problems, rather than waste time telling you about this type of thing.

And another:
http://www.parashift.com/c++-faq/main-returns-int.html
 
Last edited:
Awesome! Thank you.
I just try to do everything the way he says so I pass with a grade as high as possible haha :p
Our teacher is a little "older" and he grades us on how we set up our program aesthetically as well as functionally.
 
Awesome! Thank you.
I just try to do everything the way he says so I pass with a grade as high as possible haha :p
Our teacher is a little "older" and he grades us on how we set up our program aesthetically as well as functionally.

And rightly he should; but that's still not really an excuse for him to not teach a very basic standard, especially since it can be compiler dependent on if it actually accepts "void main" or not.
 
Back
Top Bottom