C/C++ Programming help again: A Cash Register

GEC: Discuss gaming, computers and electronics and venture into the bizarre world of STGODs.

Moderator: Thanas

Post Reply
User avatar
Soontir C'boath
SG-14: Fuck the Medic!
Posts: 6855
Joined: 2002-07-06 12:15am
Location: Queens, NYC I DON'T FUCKING CARE IF MANHATTEN IS CONSIDERED NYC!! I'M IN IT ASSHOLE!!!
Contact:

C/C++ Programming help again: A Cash Register

Post 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);

}
Image

:banghead:
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."
User avatar
Dooey Jo
Sith Devotee
Posts: 3127
Joined: 2002-08-09 01:09pm
Location: The land beyond the forest; Sweden.
Contact:

Post 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...
Image
"Nippon ichi, bitches! Boing-boing."
Mai smote the demonic fires of heck...

Faker Ninjas invented ninjitsu
User avatar
Beowulf
The Patrician
Posts: 10621
Joined: 2002-07-04 01:18am
Location: 32ULV

Post 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.
"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
User avatar
Soontir C'boath
SG-14: Fuck the Medic!
Posts: 6855
Joined: 2002-07-06 12:15am
Location: Queens, NYC I DON'T FUCKING CARE IF MANHATTEN IS CONSIDERED NYC!! I'M IN IT ASSHOLE!!!
Contact:

Post 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...
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."
User avatar
Dooey Jo
Sith Devotee
Posts: 3127
Joined: 2002-08-09 01:09pm
Location: The land beyond the forest; Sweden.
Contact:

Post 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 :P
Image
"Nippon ichi, bitches! Boing-boing."
Mai smote the demonic fires of heck...

Faker Ninjas invented ninjitsu
User avatar
Braedley
Jedi Council Member
Posts: 1716
Joined: 2005-03-22 03:28pm
Location: Ida Galaxy
Contact:

Post 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.
Image
My brother and sister-in-law: "Do you know where milk comes from?"
My niece: "Yeah, from the fridge!"
User avatar
Mad
Jedi Council Member
Posts: 1923
Joined: 2002-07-04 01:32am
Location: North Carolina, USA
Contact:

Post 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.
Later...
Orc
Redshirt
Posts: 10
Joined: 2007-11-02 10:37pm

Post 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.
User avatar
Durandal
Bile-Driven Hate Machine
Posts: 17927
Joined: 2002-07-03 06:26pm
Location: Silicon Valley, CA
Contact:

Post 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?"
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
Post Reply