C
C#8mo 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
Austin96758mo 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
Chinpoki8mo 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
Austin96758mo ago
hold on i misread ill try it again
Chinpoki
Chinpoki8mo ago
ok
Austin9675
Austin96758mo 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
Chinpoki8mo ago
wait rlly ? What is the answer when you do so when you hit 10 and "y:
Austin9675
Austin96758mo 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
Chinpoki8mo ago
The syntax looks the same though , just you assigned variables for discount and tax
mtreit
mtreit8mo ago
$code
MODiX
MODiX8mo 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
mtreit8mo ago
$codegif
Chinpoki
Chinpoki8mo ago
what is this
Austin9675
Austin96758mo 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
Chinpoki8mo ago
You only have one if statement though and it will obviously only execute the said statement
Want results from more Discord servers?
Add your server