Page 1 of 1
C/C++ Programming help again: A Cash Register
Posted: 2007-11-08 10:50am
by Soontir C'boath
For extra credit in this class, I need to submit last year's midterm exam and one of the problem is irritating me to the core.
As you can see below I've placed a while loop to continue inputting prices of items until the cashier enters 0 which should end the loop.
But when I run the program and enter 0, the loop doesn't end! Why?
Code: Select all
#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);
}
Posted: 2007-11-08 11:06am
by Dooey Jo
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...
Posted: 2007-11-08 11:15am
by Beowulf
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.
Posted: 2007-11-08 12:45pm
by Soontir C'boath
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...
Posted: 2007-11-08 01:54pm
by Dooey Jo
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
Posted: 2007-11-08 02:58pm
by Braedley
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.
Posted: 2007-11-08 03:08pm
by Mad
Braedley wrote: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.
Posted: 2007-11-09 04:23pm
by Orc
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
System.out << var
You're still going to have to use a method.
Posted: 2007-11-10 06:07pm
by Durandal
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?"