#include "stdio.h"
#include "math.h"
void main()
{
float purchases, sum=0;
int item=1;
printf("Shellfish Dock.\nEnter order price.\n");
while((scanf_s("%f", &purchases))>0){
printf("Order %d: $%f\n", item, purchases);
sum=sum+purchases;
item++;
printf("Enter another order price.\n");}
sum=sum*0.0775; /*taxes*/
printf("The total of your %d orders, including tax is $%f\n", item, sum);
}
I have almost reached the regrettable conclusion that the Negro's great stumbling block in his stride toward freedom is not the White Citizen's Counciler or the Ku Klux Klanner, but the white moderate, who is more devoted to "order" than to justice; who constantly says: "I agree with you in the goal you seek, but I cannot agree with your methods of direct action"; who paternalistically believes he can set the timetable for another man's freedom; who lives by a mythical concept of time and who constantly advises the Negro to wait for a "more convenient season."
According to this site, scanf() returns the number of successfully changed variables, not the value of any of them. You'll have to check that separately, like for instance:
while (scanf_s("%f", &purchases) != EOF && purchases > 0)
Also, this is technically C, you don't even use C++ style comments...
"Nippon ichi, bitches! Boing-boing." Mai smote the demonic fires of heck...
C++ style comments got added in C99. They're legal. But, as Dooey Jo said, scanf returns the number of changed variables. If you input 0, the purchase variable still gets changed. Alternatively, if this is a C++ class, you could use the C++ style input operators, which would look like "cin >> purchase", and be able to bang that into the while conditional. Note that C and C++ are different though. Doing that in plain C will result in failure.
Also, you have another bug, but you'll find that soon enough.
"preemptive killing of cops might not be such a bad idea from a personal saftey[sic] standpoint..." --Keevan Colton
"There's a word for bias you can't see: Yours." -- William Saletan
The class actually is C but we're using Visual Studio 2005 using C++ settings.
Putting in Doey's suggestion, it messes up the addition of the orders if that's the bug you mean. Another problem I recognized but didn't mentioned was that item goes one more than it should which I thought the loop would get out before being inputted (or was that it?).
This is really getting to me and it's extra credit!
I know while (purchase) won't work as it won't read the scanf that I would put in the loop. So I'd have to somehow give it an input before I start the loop but I don't know how...
I have almost reached the regrettable conclusion that the Negro's great stumbling block in his stride toward freedom is not the White Citizen's Counciler or the Ku Klux Klanner, but the white moderate, who is more devoted to "order" than to justice; who constantly says: "I agree with you in the goal you seek, but I cannot agree with your methods of direct action"; who paternalistically believes he can set the timetable for another man's freedom; who lives by a mythical concept of time and who constantly advises the Negro to wait for a "more convenient season."
It's considered good form in C++ to check for invalid input by using
while (cin >> purchase)
which will return NULL (which means false here) if the input fails, reaches EOF, or whatever, and if one wants to check the value (and don't want to use a break), one could just say
while ((cin >> purchase) && (purchase > 0))
Using the != and == operators with floats can also be dangerous, though it should be alright when comparing with zero...
Beowulf wrote:C++ style comments got added in C99. They're legal.
Well, he still isn't using them
"Nippon ichi, bitches! Boing-boing." Mai smote the demonic fires of heck...
Dooey Jo wrote:It's considered good form in C++ to check for invalid input by using
while (cin >> purchase)
which will return NULL (which means false here) if the input fails, reaches EOF, or whatever, and if one wants to check the value (and don't want to use a break), one could just say
while ((cin >> purchase) && (purchase > 0))
Or you could just do while((cin >> purchase)!=0).
Why you weren't using cin and cout to begin with is beyond me. On that matter, does anyone know of equivalents in Java? I hate using System.out.println() for simple outputs, and reading in can be just as bad.
My brother and sister-in-law: "Do you know where milk comes from?"
My niece: "Yeah, from the fridge!"
Dooey Jo wrote:It's considered good form in C++ to check for invalid input by using
while (cin >> purchase)
which will return NULL (which means false here) if the input fails, reaches EOF, or whatever, and if one wants to check the value (and don't want to use a break), one could just say
while ((cin >> purchase) && (purchase > 0))
Or you could just do while((cin >> purchase)!=0).
The !=0 is still comparing against the return value of cin in your example, and not purchase.
Braedley wrote:Why you weren't using cin and cout to begin with is beyond me. On that matter, does anyone know of equivalents in Java? I hate using System.out.println() for simple outputs, and reading in can be just as bad.
System.out is an instance of java.io.PrintStream, so you can use any of the methods in that class. Java doesn't do operator overloading (as far as I know), so you aren't going to get something as simple as
Braedley wrote:Why you weren't using cin and cout to begin with is beyond me. On that matter, does anyone know of equivalents in Java? I hate using System.out.println() for simple outputs, and reading in can be just as bad.
I'm guessing that C++'s utterly insane I/O syntax was one of the things the Java designers (wisely) decided not to borrow.
Ah C++, the language that answers the question "What happens when you shift a file I/O stream by a string?"
Damien Sorresso
"Ever see what them computa bitchez do to numbas? It ain't natural. Numbas ain't supposed to be code, they supposed to quantify shit."
- The Onion