problem with sqrt()

Hi guys, I just registered because I'm having a problem I can't seem to be able to fix myself...
I'm working on a program to calculate square root of a number, first i kept getting the "sqrt domain error", now it seems i fixed that, but the program wont calculate!!, with every number I enter, the result is 0.
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
   double A,B=sqrt(A);
   printf("Test 1:raiz\nPor Pablo cassinerio\n\n");
   printf("Ingrese el numero a calcular "); //enter the number
   scanf("%d",&A);
   printf("El resultado es %d",B); //the result is
   getche();
}


Sorry for the code being in spanish but i'm in Argentina :D

I can't find the error, can you help me?

Thanks
you declare B as sqrt(A) when A dont have a value: so B get the value 0. If you declare B after the user enters A is should work
I see, thanks, that's how it was before, however, the test now returns the "sqrt DOMAIN error" again, and the result keeps being 0

this is how the code looks now:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
   double A,B;
   printf("Test 1:raiz\nPor Pablo cassinerio\n\n");
   printf("Ingrese el numero a calcular ");
   scanf("%d",&A);
   B=sqrt(A);
   printf("El resultado es %d",B);
   getche();
}

I'm not familiair with the printf and scanf commands, i would recommend you to use cin and cout. When i run your program, i dont get an error message, but an incredibly high number. So i have made a few changes (cin&cout) and this works perfectly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <math.h>
#include <iostream>
using namespace std;
int main(void)
{
   double A,B;
   cout<<"Test 1:raiz\nPor Pablo cassinerio\n\n";
   cout<<"Ingrese el numero a calcular ";
   cout<<"%d";
   cin>>A;
   B=sqrt(A);
   cout<<"El resultado es %d "<<B;
   return 0;
}
great, it does work perfectly, although I'm not used to cin and cout commands, I don't understand why it works with cin and cout and not with printf and scanf, I copied that code replacing cin and cout with printf and scanf and I keep getting the error message, might there be any way to adapt the program you created to printf and scanf? If it's not possible, it seems i will have to learn to use cin and cout too hehe
You have a format mismatch in your scanf/printf, that's why it doesn't work.

%d means to read/print an int. A and B are both of double type instead. This is a mismatch.

For double off the top of my head I think %lf or %g is correct.
I dont know: i'm not familiair with printf and scanf. But i strongly recommend you to learn cin and cout. Its really simple, from what i have seen i believe it works someting familiair to printf and scanf, and everywhere you look you will see cin and cout instead of printf and scanf.
I agree that if you're doing C++ programming, use cin/cout instead. They avoid the potential type mismatch problem that can occur when passing parameters through varargs and in general makes for more maintainable code.

yes, I'm programming with C++, I got used to printf and scanf because that's what we use in the university, but I guess the most I know in general the better.

Thanks!
Topic archived. No new replies allowed.