On Fri, Jun 6, 2008 at 6:53 PM, bo <[EMAIL PROTECTED]> wrote:
> I am working on a tax program. I want to add a section that verifys
> the input from the customer. I added an if-then statement but can't
> seem to get it to work.
> Here is the code. In the section on function for inputing sales here
> is the error message:line 37: lexical: unknown escape sequence `\P'
> 'printf ("n\Please enter a valid amount\n")'
>
> What does Lexical: mean?
See comments below
> /*original version*/
> /* ver 2 removed extra semi-colons*/
> /* ver 3 added braces in function */
> /* ver 4 added conversion specifier*/
> /* ver 5 reworked formula for tatal*/
>
> #include <stdio.h>
> #include <math.h>
> main()
int main()
> {
> /*setting variables for store choice*/
>
> float fSales;
> float fTax1;
> float fTax2;
> float fTax3;
> float fTotal1;
> float fTotal2;
> float fTotal3;
>
> /*initializing variables*/
>
> fSales = 0;
> fTax1 = .0725;
> fTax2 = .075;
> fTax3 = .0775;
> fTotal1 = 0;
> fTotal2 = 0;
> fTotal3 = 0;
You could condense this by declaring and initializing together. It
just saves screen space.
float fSales = 0;
float fTax1 = .0725;
float fTax2 = .075;
float fTax3 = .0775;
float fTotal1 = 0;
float fTotal2 = 0;
float fTotal3 = 0;
>
> /*function for inputing sales*/
This isn't a function. All you are doing is creating a scope by
encasing the statements in { } but it's not a function. A function
wouild be defined outside of main() and be something like:
float inputSales(void) {
...
}
> {
> printf ("\nEnter amount of purchase:\n");
> scanf ("%f", &fSales);
I personally don't like using scanf. I might use fgets() or similar to
capture the input into a string and then convert to a float using
atof(). This is where you would want to validate the input. For a
float, you could iterate through the string, verifying that each char
is either a digit or a . and set up some rules to ensure that it
conforms to xxxx.xx (a regular expression library might make this even
easier). Definitely something you would want to do in a separate
function and not in the body of main.
Actually, I would not even use stdio.h, I'd use C++ I/O and strings.
You should check it out, you may find things easier.
> if (fSales <= 0.00);
Here's a problem. You end a conditional with a ; when it should be
followed by a single statement or {} for multiline statements:
if(fSales <= 0.00)
printf ("n\Please enter a valid amount\n");
else
printf ("\n You entered: %.2f", fSales);
I tend to enclose single line statements with { } just because it's
easier to read the logical structure, but they are optional
if(fSales <= 0.00) {
printf ("n\Please enter a valid amount\n");
} else {
printf ("\n You entered: %.2f", fSales);
}
> printf ("n\Please enter a valid amount\n");
Should be \nP -- you have n\P and the compiler is trying to interpret
\P as a non-printing character, and this is an invalid one.
> else
> printf ("\n You entered: %.2f", fSales);
> }
> fTotal1 = (fTax1 * fSales) + fSales;
> fTotal2 = (fTax2 * fSales) + fSales;
> fTotal3 = (fTax3 * fSales) + fSales;
>
> /*function for conditions*/
Again, not functions
> {
> printf ("\nYour tax rate at the Del Mar store is .0725");
> printf ("\nYour tax on your purchase amount at the Del Mar
> store is: %.2f", fTax1 * fSales);
> printf ("\nYour total at the Del Mar store is: %.2f",
> fTotal1);
> }
> {
> printf ("\nYour tax rate at the Encinitas store is .075");
> printf ("\nYour tax on your purchase amount at the Encinitas
> store is: %.2f\n", fTax2 * fSales);
> printf ("\nYour total at the Encinitas store is: %.2f\n",
> fTotal2);
> }
> {
> printf ("n\Your tax rate at the La Jolla store is .0775");
> printf ("\nYour tax on your purchase amount at the La Jolla
> store is: %.2f", fTax3 * fSales);
> printf ("\nYour total at the La Jolla store is: %.2f",
> fTotal3);
> }
>
Since main() returns an int, good idea to return 0 at the end of your function
> }
-- Brett
------------------------------------------------------------
"In the rhythm of music a secret is hidden;
If I were to divulge it, it would overturn the world."
-- Jelaleddin Rumi