Reputation: 3
I'm coding a calculator for my stats class but I'm having issues with my code. This is for calculating Confidence Intervals. I'm getting the "error more than one instance of overloaded function" when I use sqrt
. Also have a couple of other problems which I cannot identify. I'm using Visual Studio 2010 and using the win32 console application. If you could help me it'll help a lot. (I'm new to coding so bear with my awful coding.)
#include "stdafx.h"
#include <cstdlib>
#include <stdio.h>
#include <math.h>
#pragma warning(disable: 4996)
int _tmain(int argc, _TCHAR* argv[])
{
double sample = 0, z = 0, sample1 = 0;
double std = 0, error = 0, sampleroot = 0, error1 = 0;
printf("What is the Z score?");
scanf("%d\n", &z);
printf("What is the std?");
scanf("%lf\n", &std);
printf("What is the sample size?");
scanf("%d\n", &sample);
printf("What is the Error?");
scanf("%lf", &error);
//sampleroot = sqrt(sample);
error1 = z * (std/sqrt((double)sample));
sample1 = ((z * std) / error) * ((z * std) / error);
printf("Error = %lf\n", error1);
printf("Sample is %d\n", sample1);
system("pause");
return 0;
}
That my new code. When i run it, it only asks the first 3 printfs and skips directly to the solution. How do I fix this.
Upvotes: 0
Views: 765
Reputation: 3
Thanks everyone for helping me. Here is the working code if anyone wants to use it for finding error and sample size in Confidence Intervals.
#include "stdafx.h"
#include <cstdlib>
#include <stdio.h>
#include <math.h>
#pragma warning(disable: 4996)
int _tmain(int argc, _TCHAR* argv[])
{
double sample = 0, z = 0, sample1 = 0;
double std = 0, error = 0, sampleroot = 0, error1 = 0;
printf("What is the Z score?");
scanf("%lf", &z);
printf("What is the std?");
scanf("%lf", &std);
printf("What is the sample size?");
scanf("%lf", &sample);
printf("What is the Error?");
scanf("%lf", &error);
//sampleroot = sqrt(sample);
error1 = z * (std/sqrt((double)sample));
sample1 = ((z * std) / error) * ((z * std) / error);
printf("Error = %lf\n", error1);
printf("Sample is %d\n", sample1);
system("pause");
return 0;
}
Upvotes: 0
Reputation: 116
There are three sqrt functions in C++:
double sqrt(double _X);
float sqrt(float _X);
long double sqrt(long double _X);
You have sample
declared as an int (which can convert to float or double), so the compiler doesn't know which function to call.
You could pick the one you want and typecast, sqrt((double)sample)
, or you probably just want to declare sample as double to begin with. In fact, you probably want all your int
s to be double
s.
edit
You need a &
before error
in scanf("%lf", &error);
Also I think you'll want to remove all the \n
from your scanf()
formats.
Upvotes: 2