C
C#10mo ago
Chinpoki

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 !!
No description
No description
39 Replies
Austin9675
Austin967510mo ago
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
Chinpoki
ChinpokiOP10mo ago
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
Austin9675
Austin967510mo ago
hold on i misread ill try it again
Chinpoki
ChinpokiOP10mo ago
ok
Austin9675
Austin967510mo ago
its still working fine for me , i have not ran into any issues, using 10 and hitting y prints the first statement for me
Chinpoki
ChinpokiOP10mo ago
wait rlly ? What is the answer when you do so when you hit 10 and "y:
Austin9675
Austin967510mo ago
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); }
Chinpoki
ChinpokiOP10mo ago
The syntax looks the same though , just you assigned variables for discount and tax
mtreit
mtreit10mo ago
$code
MODiX
MODiX10mo ago
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/
mtreit
mtreit10mo ago
$codegif
Chinpoki
ChinpokiOP10mo ago
what is this
Austin9675
Austin967510mo ago
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);
}
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);
}
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
Chinpoki
ChinpokiOP10mo ago
You only have one if statement though and it will obviously only execute the said statement
mtreit
mtreit10mo ago
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.
Chinpoki
ChinpokiOP10mo ago
I did try it many times and got hopeless and came here
mtreit
mtreit10mo ago
Well, step back, take a deep breath and first try the code that @Austin9675 posted.
Chinpoki
ChinpokiOP10mo ago
ok
mtreit
mtreit10mo ago
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.
Austin9675
Austin967510mo ago
this works 100% for me
int textbook;
char cover;
double rprice;
double fprice = 0; // Initialize fprice to 0

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);
}
else if (cover == 'n' && textbook > 4)
{
fprice = rprice - (0.05 * rprice);
Console.WriteLine(fprice);
}
else if (cover == 'y' && textbook <= 4)
{
fprice = rprice + (0.20 * rprice);
Console.WriteLine(fprice);
}
else
{
fprice = rprice;
Console.WriteLine(fprice);
}
int textbook;
char cover;
double rprice;
double fprice = 0; // Initialize fprice to 0

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);
}
else if (cover == 'n' && textbook > 4)
{
fprice = rprice - (0.05 * rprice);
Console.WriteLine(fprice);
}
else if (cover == 'y' && textbook <= 4)
{
fprice = rprice + (0.20 * rprice);
Console.WriteLine(fprice);
}
else
{
fprice = rprice;
Console.WriteLine(fprice);
}
mtreit
mtreit10mo ago
Learning to debug is a very important step.
Austin9675
Austin967510mo ago
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
Chinpoki
ChinpokiOP10mo ago
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 ?
mtreit
mtreit10mo ago
Moving the print to the end (outside all of the if and else checks) should also be fine.
Chinpoki
ChinpokiOP10mo ago
yeah but didnt work for me am i missing something?
mtreit
mtreit10mo ago
Paste your exact code and someone can help show you what's wrong.
Chinpoki
ChinpokiOP10mo ago
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}");
Austin9675
Austin967510mo ago
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
mtreit
mtreit10mo ago
No description
mtreit
mtreit10mo ago
So what part isn't working for you?
Chinpoki
ChinpokiOP10mo ago
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
mtreit
mtreit10mo ago
Let's step through the code in the debugger
mtreit
mtreit10mo ago
No description
mtreit
mtreit10mo ago
Are you expecting something different to happen? It is going into the first if and doing the calculation there.
Chinpoki
ChinpokiOP10mo ago
yeah but the calculation isnt right !! 1250 shouldnt be the answer
MODiX
MODiX10mo ago
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.
mtreit
mtreit10mo ago
int types never have fractions in programming. You need a floating point type.
Chinpoki
ChinpokiOP10mo ago
ahh wait i think i get it now FINALLLYYYYYYYYYYYY Its working now after i changed fraction to decimal Nvm got it Thanks !!
Want results from more Discord servers?
Add your server