in my switch program keeps reading wrong input also i know i used way too much fflush(stdin); because stupid printf keeps inputting to the screen so scanf gets bad but i m sure i desinged program nice i dunno why when i like enter num 5 it keeps reading artichokes even though i made the default break;

#include <stdio.h>
//everything here is per pound ofcourse
#define artichokes 1
#define beets 2 
#define carrots 3
int ParseFees(int dollars,int type)
{
    char shipping;
    int pounds=dollars;
    dollars*=type;//to get dollar amount
    fputs("do you want the stuff shipped ?:_\n",stdout);
    fflush(stdin);
    shipping=getchar();
    toupper(shipping);
    if(shipping=='Y') {
         if(type==1) 
             dollars=(dollars + 8) + (type * 2);
         switch(pounds) {
            case 5:    
                 dollars+=3;
                 break;
            case 10:
                 dollars+=10;
                 break;
            }
         }
    return dollars;
}
int main(void)
{
    int groc;//variable used for gocery
    int pounds;
    int dollars=0;
    for(;;) {
         puts("Hello Sir May I help you with gocery?\n"
              "Here is the Menu\n"
              "1)artichokes for $1 per pound\n"
              "2)beets for 2 $ per pound\n"
              "3)carrots for 3 $ per pound\n"
              "\nNotice: We give 5 % discount for orders over 100 $\n"
              "\n2nd Notice: enter other value to quit the programe and know how many dollars you owe\n"
              );
         
         switch(scanf("%d",&groc)) {
              case 1:
                   fputs("how many pounds you want? of artichokes",stdout);
                   fflush(stdin);
                   scanf("%d",&pounds);
                   dollars+=ParseFees(pounds,artichokes);//rest of here will handle groceries etc
                   continue;
              case 2:
                   fputs("how many pounds you want? of artichokes",stdout);
                   fflush(stdin);
                   scanf("%d",&pounds);
                   dollars+=ParseFees(pounds,artichokes);//rest of here will handle groceries etc
                   continue;
              case 3:
                   fputs("how many pounds you want? of artichokes",stdout);
                   fflush(stdin);
                   scanf("%d",&pounds);
                   dollars+=ParseFees(pounds,artichokes);//rest of here will handle groceries etc
                   continue;
              default:
                      puts("thanks for shopping Please visit us again\n");
                      break;
              }
         break;
         }
    if(dollars>100) {
        int temp=dollars;
        printf("Please Pay %d sir $",(temp/100) * 5 - dollars);
        }
    else
       printf("you owe us %d $",dollars);
    fflush(stdin);
    getchar();
    return 0;
}

it doesnt even read other input either than first input

i mean the artichokes i know i did it 3 times in printf but now i fixed it it keeps reading 1st only either than that it doesnt read the other option

I see several problems. You would have found them immediately if you had typedef enum's but here goes.

You enter types 1,2,3 but you submit them for processing as type artichokes, artichokes, artichokes.

Also your data entry is confusing.

You should enter weight and product type, pass it as such, and resolve into monetary at one place. You're entering pounds calling it dollars.

You also have a problem with your shipping costs. Shipping is usually tier based.
Weight between one range and another is one cost, etc. But you'r e trapping for 5 pounds, 10 pounds, ignoring inbetween values.

Also when you put " % " in a string, you need to enter it as " %% ".

@wildgoose

You're entering pounds calling it dollars.

Pound as a unit of weight not as a monetary unit.

@MrNoob

1>toupper() is defined in <ctype.h> and I don't see it in your headers.

2>
Even though the problems mentioned in above posts are right most of them are problems due to logical thinking "Tier" "Shipping cost" and all.Ya you calling artichokes for everything is a fault,but both of you failed to notice one major problem.

switch(scanf("%d",&groc))

Scanf returns the number of inputs it has successfully read not their value so if you feed in even 3 or 2 it has successfully read one input so it returns 1. So here you are not deciding the switch upon the choice of the user at all.Because on every successful read scanf returns 1 and the user is forcibly directed to choice 1 even if he chooses 3.
It should be as :

for(;;) //Your for statement only
{
     // The stuff you have written before switch
     scanf("%d",&groc);
     switch(groc)
    {
          //Switch conditions
    }
    //Other stuff
}

You both need to concentrate on what stuff you are dealing with.This is C language and it requires some patience and some effort.

Rats! I looked right at that and skipped over it! Good catch CSurfer!

I really don't get why MrNoob is so obsessed with fflush(stdin); and also with fputs .And ya one more thing "Program Keeps reading wrong input".

It always just does what you have programmed it for.So the program is not wrong you are !!!

Rats! I looked right at that and skipped over it! Good catch CSurfer!

Don't let your eyes run ahead of your brains!!!:)

commented: Solid contribution in the whole thread +36

when i remove fflush(stdin) in some the scanf gets missed it moves to other instruction also thanks man I dont wanna use fflush but sometimes when i have many puts or printf some scanf gets missed is there a way to fix it without fflush(stdin) and for dollar variable i named it dollar coz i convert the pounds to dollar i m gonna fix that function 2 since i m calculating wrong for the shipping

fflush(stdin) is totally, utterly, and completely wrong.

never, ever, use it.

learn to parse your input correctly in the first place, and you wont have this problem.

so get rid of scanf() while you're at it. that's a bad function to use, and is a source of the problem.

use fgets() instead, and parse the input to find the data you're looking for.

but fgets reads strings can it read ints 2 ?maybe if i read a string and convert to int by atoi ?

Ya you can do that or device your own method to convert it into an integer.

but fgets reads strings can it read ints 2 ?maybe if i read a string and convert to int by atoi ?

that's one way to do it. I prefer to use "strtol()", because you can do more robust error checking to see if the user entered a valid number.

but sure... try "atoi()" to get started.

.

thanks a lot guys for helping me out everytime

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.