0

My program does everything right but the program doesn't ask the user anuthing and skips everything in the function, can you see what's wrong? All functions work properly, since it wnds up printing the "Are you sure you want to buy?"

I already tried to move the printf into the function, and it still appears in the screen, so it is something after it.

void shopBuy (){
    char input [50];
    char nameprice [80];
    char price [5];
    char myMoney [100];
    int money=0,mymoney;
    printf("What do you want to buy?\n");
    scanf("%s",&input);
    getPrice(nameprice,input);
    last3(nameprice,price);
    money=atoi(price);
    printf("It costs: $%d\n",money);
    FILE *fp;
    fp = fopen("money.txt", "r");   
    fgets(myMoney, 80, (FILE*)fp);
    fclose(fp);
    mymoney=atoi(myMoney);
    if (mymoney>money)
    {
        printf("Are you sure you want to buy?(y/n)\n");
        acceptBuy(mymoney,money);
    }
    else{
        denyBuy();
    }
}

void acceptBuy(int m,int p){
    int afterbuy;
    char a;
    char afterbuyString [100];
    scanf("%c",&a);
    if(a=='y'||a=='Y'){
        afterbuy=m-p;
        itoa(afterbuy,afterbuyString,10);
        FILE *fp;
        fp = fopen("money.txt", "w+");
        fprintf(fp, afterbuyString);
        fprintf(fp, "\n");
        fclose(fp);
    }
}

int main(void){
    shopBuy();
    return 0;
}
Ash
  • 78
  • 12

1 Answers1

1

As "user3121023" say, it needs the space behind "%c".

So the function is:

void acceptBuy(int m,int p){
    int afterbuy;
    char a;
    char afterbuyString [100];
    scanf(" %c",&a); // note the space.
    if(a=='y'||a=='Y'){
        afterbuy=m-p;
        itoa(afterbuy,afterbuyString,10);
        FILE *fp;
        fp = fopen("money.txt", "w+");
        fprintf(fp, afterbuyString);
        fprintf(fp, "\n");
        fclose(fp);
    }
}
Ash
  • 78
  • 12