1.
Use code tags! Since you didn't, HTML removed all the leading spaces in your code, thus removing its formatting, and turned it into an unreadable mess. If you use
code tags, then that will not happen. Rest assured that you will hear about from us every time you don't use
code tags
It's very simple. These are what code tags look like:
[code] [/code] All you need to do is to copy-and-paste your formatted code between them. From now on, do not forget to use them. For that matter, go back to your first message and add them.
2. You broke your promise to the compiler. You told the compiler that your main function would return an int (which
is correct), and yet you did not return anything. The last line executed in main needs to be a
return 0; -- returning a zero indicates successful completion of the program, whereas returning a non-zero indicates that you terminated the program due to an error condition (eg, a file it was supposed to open and read could not be opened).
3. Since your having left out that return statement must have caused the compiler to issue at least a warning, that means that you must be ignoring warnings.
Never ignore warnings! Warnings are much more important than error messages are. You must
never ignore warnings!
Quote:
| Originally Posted by Michael Carl If the user enters a number greater than 99, it'll say "Change amount must be between 1 and 99," but then will continue to run the rest of the program. How do I end it if a number between 1 and 99 isn't entered? |
Consider this part of your program (also note the use of code tags):
Code:
printf ( "Enter the change amount.\n(Change amount must be between 1 and 99.)\n" ); // prompt
scanf ( "%d", &changeamount ); // read an integer
if ( changeamount < 1 )
{
printf ( "Change amount must be between 1 and 99.\n" );
} // end if
if ( changeamount > 99 )
{
printf ( "Change amount must be between 1 and 99.\n" );
}
First, instead of two separate tests, why not combine them? Implicitly you are saying "if changeamount is less than 1
or changeamount is greater than 99", so why not say it
explicitly? BTW, the relational OR operator is
||.
Then once you have combined those two if-statements into one, use the if-else construct thus:
Code:
printf ( "Enter the change amount.\n(Change amount must be between 1 and 99.)\n" ); // prompt
scanf ( "%d", &changeamount ); // read an integer
if ( changeamount < 1 || changeamount > 99 )
{
printf ( "Change amount must be between 1 and 99.\n" );
}
else
{
// into here insert "the rest of the program"
}
One basic approach to designing a program is to think about its overall structure, write that overall structure, and then fill in the blanks with code.
PS
As Larry Wall, creator of Perl ("Perfectly Eclectic Rubbish Lister", once the language of choice in web programming), said: "There's more than one way to do it."
What a C programmer would typically do would be to return with an error as soon as an error is detected, so that the only way you can get further down the code would be if you had not encountered an error. So, keeping your original structure, that would look like this:
Code:
printf ( "Enter the change amount.\n(Change amount must be between 1 and 99.)\n" ); // prompt
scanf ( "%d", &changeamount ); // read an integer
if ( changeamount < 1 )
{
printf ( "Change amount must be between 1 and 99.\n" );
return 1; // a non-zero to indicate failure; ie, termination due to an error
} // end if
// getting this far means that changeamount is not less than 1
if ( changeamount > 99 )
{
printf ( "Change amount must be between 1 and 99.\n" );
return 2; // a non-zero to indicate failure; ie, termination due to an error
}
// getting this far means that changeamount is valid; it's within the limits of 1 to 99, inclusive
// therefore, you're good-to-go for successful completion of the program
if ( 0 < changeamount && changeamount < 100 )
{
if ( changeamount >= 25 )
{
This is the way that actual C programmers write code. However, it violates the rules of structured programming -- specifically the rule that each function has only one entry point and only one exit point -- and so your instructor will not like it.
Write properly structured code as you are taught, but recognize what another programmer is doing when you encounter code like this.