0

Hi guys, I have a problem with this question, where the amount is constantly $0 and at the end if the user enters Y/N it just closes without saying "Transaction Completed/Cancelled".

Any tips on how to fix it?

Thanks

#include<stdio.h>
#include <iostream>
#include<math.h>

int main()
{
	int numofbees;
	float price = 5;
	char query;
	char Y;
	char N;

	printf("How many bees would you like to purchase?\n");
		scanf("%d", &numofbees);
		printf("That will be $%d, confirm? (Y/N)\n", price*numofbees);
		scanf("%c", &query);
		if(query == Y)
			printf("Transaction completed.");
		else if(query == N)
		        printf("Transaction cancelled.");

		getchar();
		getchar();
		return 0;
	}
3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by WaltP
0

Y and N have no values if you want to check if the user used either then compare it to the letter itself e.g.

if(query == 'Y')

also

printf("That will be $%d, confirm? (Y/N)\n", price*numofbees);

price is a float, &d is used for integers use %f

Edited by zeroliken: n/a

0

Problem #1:
After your first scanf() , the ENTER you typed is still in the input buffer, waiting for the next input. Your second scanf() reads that ENTER. So the value is not 'y' nor 'n', so no message.

Problem #2:
And, as zeroliken said, the variables Y and N have no value. You want characters, not variables.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.