-3

I am transfering from Java to C and C++ and i am having a problem with easiest tasks like this so please help me if you can:

#include<stdio.h>
#include<math.h>

 void main()
 {
int a, h;
double interres;
double base;
printf("Input  a: ");
scanf("%d", &a);
printf("Input  height h: ");
scanf("%d", &h);
base =(a^2 * sqrt(3))/ 4;//line 13
interres = a ^ 2 * sqrt(3);//line 14

printf("(%d^2*sqrt(3))/4=(%d^2*%f)/4=(%f*%f)/4=%f/4=%f cm",a,a,sqrt(3),a^2,sqrt(3),interres,base);

 }

And I get constantly errors:

error C2297: '^': illegal, right operand has type 'double' line 13
error C2297: '^': illegal, right operand has type 'double' line 14
 warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.  warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
danglingpointer
  • 4,708
  • 3
  • 24
  • 42
Mapet
  • 109
  • 1
  • 8

2 Answers2

0

Several issues:

In C and C++, main returns int, not void. Unless your compiler documentation explicitly lists void main() as a valid signature, using it invokes undefined behavior. main should be defined as

int main( void )

if you don't take any command line arguments, or

int main( int argc, char **argv )

if you do.

Secondly, the ^ operator is the bitwise XOR operator, not an exponentiation operator; neither C nor C++ have an exponentiation operator. You must either use the pow function in the standard library or roll your own.

Finally, scanf can be unsafe when reading input with either the %s or %[ conversion specifiers, which you aren't doing. Personally, I'd disable the warning as described in the error message.

John Bode
  • 119,563
  • 19
  • 122
  • 198
-1
#include<stdio.h>
#include<math.h>

 void main()
 {
int a, h;
double interres;
double base;
printf("Input  a: ");
scanf("%d", &a);
printf("Input  height h: ");
scanf("%d", &h);
base =(double)((a*a) * sqrt(3))/ 4;//line 13
interres =(double) (a *a)  * sqrt(3);//line 14

printf("(%d^2*sqrt(3))/4=(%d^2*%f)/4=(%f*%f)/4=%f/4=%f cm",a,a,sqrt(3),a*a,sqrt(3),interres,base);

 }