+ 2
Hovercraft code coach help
I currently solved Hovercraft in all programming language except C. This is my code: #include <stdio.h> int main() { int hover=2000000; int hoverprice=3000000; int cost=1000000; int sale; int scan=scanf("&d", &sale); int outcome=(hover*10)+cost; int x=scan*hoverprice; char prof[6] ="Profit"; char loss[4] ="Loss"; char broke[10] ="Broke Even"; if (x>outcome) { printf(prof); } else if (x==outcome) { printf(broke); } else { printf(loss); } return 0; } After I test it, it returns "LossProfit" shit and I fail all tests
2 Respostas
+ 3
You mistakenly put "&d" instead of "%d" in the scanf() function.
When you assign to int x, you assign scan * hoverprice. I believe you wanted for the sale variable to be used instead.
int x = sale * hoverprice.
The other issue is that none of your strings are null-terminated because the size is off by one. You need the length of the string + 1 for the null-terminating character '\0'. Change them to:
char prof[7] = "Profit";
char loss[5] = "Loss";
char broke[11] = "Broke Even";
In your printf() statement you pass the strings directly without a format specifier. Change each printf statement to printf("%s\n", your_string) or even better use:
puts(prof);
puts(broke);
puts(loss);
After those changes I believe it should work.
+ 2
Damyian G Thanks