What is wrong with this program?

#include “stdio.h”
main();
{
float total, price == 2.99;
int number;
printf(“How many T-shirts would you like?”);
scanf(“n%f”, &number);
total = number * price;
printf(“That will cost you £total”, %f);
}

I'm very new to C++
closed account (zb0S216C)
Dan Beckwith wrote:
price == 2.99;

You're not assigning a value, but instead, you're comparing price with the value of 2.99. Remove 1 of the equals symbols, and you'll be set.

P.S: Please use code-tags. Thanks.

Wazzak
I didn't write it, its to help me with homework, I need to find 6 things wrong with it
So what have you found so far??
Instead of

#include "stdio.h"

shall be

#include <cstdio>

instead of

main(); // error: presence of the semicolon is invalid

shall be

int main()

instead of

price == 2.99

shall be

price = 2.99

It looks like the string literal has different quotes

“How many T-shirts would you like?”

shall be

"How many T-shirts would you like?"

I think that literal n is not required in this string literal

scanf(“n%f”, &number);

shall be

scanf(“%f”, &number);

The same remark about literal quotes as above.

This statement will not be compiled

printf(“That will cost you £total”, %f);

shall be

printf(“That will cost you £%f”, total);

It is better to use namespace prefix before the functions as for example

std::printf, std::scanf because it is implementation-defined whether standard C names will be placed in the global namespace or not.

Also a return 0; statement could be placed in the body of main as the last statement though it is not obligatory.

Last edited on
@vlad from moscow - why did you do that?
The OP is supposed to find the errors.
Cheers Vlad!
Just a question:
Is your code in c or c++?
@Vagabond
Just a question:
Is your code in c or c++?


I think as he wrote "I'm very new to C++" then the code is in C++.:)
You should do your own homework. So you don't fail the class.
Topic archived. No new replies allowed.