If statements
Hey guys, sorry if you think this problem is too easy but i just can't find what's wrong with it. I am learning c# actually. In the solution, whenever i execute the program it jumps to the ELSE statement no matter what input i provide . If anyone could please help, THANK YOU !!
39 Replies
Are you using capital Y or N instead of lowercase ?
you gotta be typing something wrong because i recreated ur code and it works fine for me
No i used lowercase even so it doesnt work
It actually only executes the esle statement for some reason
For example insert 10 it does 10 * 125 when actually it should be doing the first IF statement
including the premium and discount
hold on i misread ill try it again
ok
its still working fine for me , i have not ran into any issues, using 10 and hitting y prints the first statement for me
wait rlly ?
What is the answer when you do so
when you hit 10 and "y:
1062.5
the problem is how you have ur code
this is how it works for me
int textbook;
char cover;
double rprice;
double fprice;
Console.WriteLine("Enter number of books");
textbook = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Do you want hardback? y/n");
cover = Convert.ToChar(Console.ReadLine());
rprice = textbook * 125;
if (cover == 'y' && textbook > 4)
{
double discount = 0.20; // 20% discount
double tax = 0.05; // 5% tax
fprice = rprice - (discount * rprice) + (tax * rprice);
Console.WriteLine(fprice);
}
The syntax looks the same though , just you assigned variables for discount and tax
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/$codegif
what is this
and yes variables is how i got the progam to work, i was running into the same issue
nothing thats how they wanted me to post the code
You only have one if statement though and it will obviously only execute the said statement
Start with that code. Does it work for you or not?
If so, start building back to the full program.
Go piece by piece to figure out what's going wrong.
It's usually best to test your code a little at a time and make sure each part is already working before you move to the next piece.
I did try it many times and got hopeless and came here
Well, step back, take a deep breath and first try the code that @Austin9675 posted.
ok
Also, step through the code line by line in a debugger or add print statements to print out the values of your variables to make sure you understand exactly what your code is doing.
this works 100% for me
Learning to debug is a very important step.
trust me coding isnt easy it isnt really meant to be
theres plenty of times where i cant figure something out, thats why im in this discord
Ok lemme compare your code and mine hopefully find whats wrong
Ohh i get it
You cant skip the if statements? thats why you printed the calculation in each statement ?
Moving the print to the end (outside all of the if and else checks) should also be fine.
yeah but didnt work for me
am i missing something?
Paste your exact code and someone can help show you what's wrong.
int textbooks;
char cover;
double rprice;
double fprice;
Console.Write("Enter the number of textbooks you wanna buy: ");
textbooks = Convert.ToInt32(Console.ReadLine());
Console.Write("Do you want premium hardcover y/n ? : ");
cover = Convert.ToChar(Console.ReadLine());
rprice = textbooks * 125;
// raw price before premium and discount
if (cover == 'y' && textbooks > 4)
{
fprice = rprice + (20 / 100 * rprice) - (5 / 100 * rprice);
}
else if (cover == 'n' && textbooks > 4)
{
fprice = rprice - (5 / 100 * rprice);
}
else if (cover == 'y' && textbooks <= 4)
{
fprice = rprice + (20 / 100 * rprice);
}
else
{
fprice = rprice;
}
Console.WriteLine("Price of " + textbooks + $" textbooks is {fprice:C}");
When you use 20 / 100 or 5 / 100, the result will be truncated to an integer in C# because both operands are integers.
you should use floating-point literals or explicitly cast one of the operands to a double
So what part isn't working for you?
when you enter 10 it should include premium and discount
to calculate final price
This is directly executing the fprice = rprice statement
when it should be executing the first if statement
Let's step through the code in the debugger
Are you expecting something different to happen?
It is going into the first if and doing the calculation there.
yeah but the calculation isnt right !! 1250 shouldnt be the answer
Austin9675
When you use 20 / 100 or 5 / 100, the result will be truncated to an integer in C# because both operands are integers.
you should use floating-point literals or explicitly cast one of the operands to a double
Quoted by
<@406536255426396160> from #If statements (click here)
React with ❌ to remove this embed.
int
types never have fractions in programming.
You need a floating point type.ahh wait i think i get it now
FINALLLYYYYYYYYYYYY
Its working now
after i changed fraction to decimal
Nvm got it
Thanks !!