Jump to content

I have this program which follows this:

 

Write a C program checkout.c that adds up numbers as they are typed in. This is rather like scanning prices at a supermarket checkout. The program should repeatedly input float values representing prices (in pounds and pence such as 3.25) and keep a running total

When the user enters a zero price the program should stop reading numbers and print out (a) the grand total and (b) the number of items. For example:

Price?   0.54
Price?   2.23
Price?   0.77
Price?   1.99
Price?   0.00
       ------
Total:   5.53   (4 items)

 

However I've stuck with the part that asks me to count the number of items the user typed into the program. So far I've only thought of adding an int varaible called count. Now i'm not really sure where to place this count varaible. I tried adding another for loop but it didn't go so well.

 

Here is my code so far:

#include <stdio.h>int main(){	float price = 0, total = 0;	int count = 0;	do {		printf(" Price: ", price);		scanf(" %f", &price);		total += price;	} while (price > 0);	printf( " Total: %.2f\n", total);	printf( " %i items\n ", count);	printf("\n");	return 0;}

I'm sure it's something simple but I'm stumped. Any help is greatly appricated.

 

   
   
Link to post
Share on other sites


#include <stdio.h>

int main()

{

float price = 0, total = 0;

int count = 0, i=0;

do {

printf(" Price: ", price);

scanf(" %f", &price);

total += price;

i++;

} while (price > 0);

printf( " Total: %.2f\n", total);

printf( " %d items\n ", i);

printf("\n");

return 0;

}

Link to post
Share on other sites

I have this program which follows this:

 

Write a C program checkout.c that adds up numbers as they are typed in. This is rather like scanning prices at a supermarket checkout. The program should repeatedly input float values representing prices (in pounds and pence such as 3.25) and keep a running total

When the user enters a zero price the program should stop reading numbers and print out (a) the grand total and ( B) the number of items. For example:

Price?   0.54

Price?   2.23

Price?   0.77

Price?   1.99

Price?   0.00

       ------

Total:   5.53   (4 items)

 

However I've stuck with the part that asks me to count the number of items the user typed into the program. So far I've only thought of adding an int varaible called count. Now i'm not really sure where to place this count varaible. I tried adding another for loop but it didn't go so well.

 

Here is my code so far:

#include <stdio.h>int main(){	float price = 0, total = 0;	int count = 0;	do {		printf(" Price: ", price);		scanf(" %f", &price);		total += price;	} while (price > 0);	printf( " Total: %.2f\n", total);	printf( " %i items\n ", count);	printf("\n");	return 0;}

I'm sure it's something simple but I'm stumped. Any help is greatly appricated.

you got wrong the %i, you have to use a %d for integers. plus, i didn't notice but you had actually initiated a counter but i didn't see it haha

Link to post
Share on other sites

#include <stdio.h>int main(){	float price = 0, total = 0;	int count = 0, i=0;	do {		printf(" Price: ", price);		scanf(" %f", &price);		total += price;                i++;} while (price > 0);	printf( " Total: %.2f\n", total);	printf( " %d items\n ", i);	printf("\n");	return 0;}

wow that is simple, I just wan't sure where to increment the count varaible. Also is it possible to make the count skip if a user enters the price 0 So it only counts the price of something above 0? So say you entered 3 values then 0 to bring up the total, it would only read 3 items rather than 4?

   
   
Link to post
Share on other sites

you got wrong the %i, you have to use a %d for integers.

There is no difference between %d and %i for printf() . For scanf() , things are different though , and %i can support multiple bases(for example hexadecimal).

Taken from the C99 standard :

 

 d,i The int argument is converted to signed decimal in the style

[−]dddd. The precision specifies the minimum number of digits to

appear; if the value being converted can be represented in fewer

digits, it is expanded with leading zeros. The default precision is

1. The result of converting a zero value with a precision of zero is

no characters.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×