Hey Again.
Just for the hell of it, I'm posting the program I'm working on as a C practice project. Everything I'm doing right now is console so that I get the hang of real coding and syntax before moving on to more object oriented endeavors.
Tater, in this example, I've implemented your range solution. However, the program still freaks out if the user enters letters instead of numbers.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int rating, statusQuo, quit;
float bill, tip, badTip, okTip, goodTip;
badTip = 0.1;
okTip = 0.15;
goodTip = 0.2;
statusQuo = 1;
printf ( "\n\*WELCOME TO THE EASY TIP CALCULATOR!\*\n");
while ( statusQuo == 1) {
printf("\n\nPlease enter the amount of your bill: $");
scanf ("%f", &bill );
if (bill <= 0 || bill > 1000000) {
printf ( "\nI'm sorry, but no restaurant bill would ever be that amount.\n\nPlease enter a REAL amount: $");
scanf ("%f", &bill);
}
if ( bill >0 && bill < 1000000) {
printf ( "\n\nPlease enter:\n\n1\. For bad service\n2\. For Okay service\n3\. For \"I want to date the waitor\"\n\nRating: ");
scanf ("%d", &rating);
}
if (rating < 1 || rating > 3) {
printf ( "\nLook, we have restrictions around here. Please enter a 1, 2 or 3: ");
scanf ( "%d", &rating);
}
if ( rating == 1 ) {
tip = (bill * badTip);
}
else if ( rating == 2) {
tip = (bill * okTip);
}
else if ( rating == 3) {
tip = (bill * goodTip);
}
printf( "\nYou should tip: $%.2f\n ", tip);
printf ( "\nPlease enter 1 to quit or 2 to continue: ");
scanf ( "%d", &quit);
printf ( "\n");
if ( quit == 1) {
(++statusQuo);
printf ( "Thanks for using my program! Hope you found it helpful.\n" );
}
}
return 0;
}
Just posted this in case anyone is nice enough to grade my homework. It's been a while since I've spent a good amount of time coding, so even though the program works for the most part, I'd love some feedback on implementation, coding practices, neatness...etc.
I also started to do this modularly by turning most of the code into functions, but the local nature of variables is throwing me off a little. I think to really start using functions well, I need to get deeper into pointers, which is where I am in the book I'm reading. Thoughts?
Also, I know it's not commented. Since it's small and I'm the only one working on it, I didn't bother. But I should go back and do it at some point.
Finally, in regard to the Main function and the compiler I'm using. Most examples on this site, I'm noticing, use
However, the book I'm learning from uses:
Code:
int main (int argc, const char * argv[]) {
I've actually found this small sticking point somewhat confusing. I've also seen the (void) version. The GCC compiler seems to take any of these versions, so it hasn't been much of a hangup, but if anyone has a definitive answer, I'm all ears.
Thanks again for the help.