Thread: Got stuck --> need help

  1. #1
    Registered User
    Join Date
    Oct 2010

    Got stuck --> need help

    hi guys i have a program about counting items and adding their prices and checking if the item is Towel i should start counting coz the costumer must only buy 3 towels.
    i got program right but the total always doubled ):
    can u help
    here's the code

    thanx in advance

    #include <stdio.h>
    #include <string.h>
    	char item[20];
    	int  towelnum;
    	float subtotal, price;
    int main() {
    	towelnum = 0.0;
    	printf("Enter item then price\n");
    	printf("Enter XXX to stop\n");
    	subtotal = 0;
    		if (strcmp(item,"XXX")==0){
    		if (strcmp(item,"Towel")== 0 ){
    		scanf("%f", &price);
    		subtotal = subtotal + price;
    		if(towelnum > 3){
    			printf("You exceeded\n");
    			subtotal = subtotal - price;
    	printf("Subtotal: $%1.2f\n", subtotal);
    	if(towelnum == 3){
    		printf("Total: $%1.2f", subtotal - 5);
    		printf("Total: $%1.2f", subtotal);

  2. #2
    Registered User
    Join Date
    Sep 2006
    I'm not that familiar with gets() because it's hella unsafe, but - I believe the scanf() line of code inside the while() loop, is leaving the newline char behind in the keyboard buffer (OK, I know that's true). Which makes gets() accept the item again. (not positive of that, though and haven't run your code.)

    The test would be to add a getchar() right after the scanf() to pull the newline char off the keyboard buffer.

    Try that.

  3. #3
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Pune, Maharashtra, India
    ya, it's true.
    i tried your code, used flushall() before gets() and don't forget to hit enter after writing "Towel" and then it's cost.
    "Towel" and "XXX" should be used with proper cases...

  4. #4
    Registered User
    Join Date
    Oct 2010
    getchar(); < solved the problem

    thanx a lot for both of u

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small -> big -> bigger -> bigger than bigger -> ?
    By happyclown in forum A Brief History of
    Replies: 9
    Last Post: 03-11-2009, 12:12 PM
  2. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  3. Dev-C++ -> Tools - > Editor -> Syntax
    By Yuri2 in forum C++ Programming
    Replies: 19
    Last Post: 07-03-2006, 07:48 AM
  4. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 02:06 PM