在 C++ 中使用 sqrt 时出错
Error while using sqrt in C++
我正在为我的统计数据编写一个计算器 class,但我的代码有问题。这是为了计算置信区间。我在使用 sqrt
时得到 "error more than one instance of overloaded function"。还有一些我无法确定的其他问题。我正在使用 Visual Studio 2010 并使用 win32 控制台应用程序。如果你能帮助我,它会帮助很多。 (我是编码新手,所以请忍受我糟糕的编码。)
#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;
}
那是我的新密码。当 i 运行 它时,它只询问前 3 个 printfs 并直接跳到解决方案。我该如何解决这个问题。
C++中的三个sqrt函数:
double sqrt(double _X);
float sqrt(float _X);
long double sqrt(long double _X);
您已 sample
声明为 int(可以转换为 float 或 double),因此编译器不知道要调用哪个函数。
您可以选择您想要的一个并进行类型转换,sqrt((double)sample)
,或者您可能只想在开始时将样本声明为双精度。事实上,您可能希望所有 int
都是 double
。
编辑
您需要在 scanf("%lf", &error);
中的 error
之前添加一个 &
另外我认为您需要从 scanf()
格式中删除所有 \n
。
感谢大家对我的帮助。这是工作代码,如果有人想用它来查找置信区间中的错误和样本量。
#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;
}
我正在为我的统计数据编写一个计算器 class,但我的代码有问题。这是为了计算置信区间。我在使用 sqrt
时得到 "error more than one instance of overloaded function"。还有一些我无法确定的其他问题。我正在使用 Visual Studio 2010 并使用 win32 控制台应用程序。如果你能帮助我,它会帮助很多。 (我是编码新手,所以请忍受我糟糕的编码。)
#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;
}
那是我的新密码。当 i 运行 它时,它只询问前 3 个 printfs 并直接跳到解决方案。我该如何解决这个问题。
C++中的三个sqrt函数:
double sqrt(double _X);
float sqrt(float _X);
long double sqrt(long double _X);
您已 sample
声明为 int(可以转换为 float 或 double),因此编译器不知道要调用哪个函数。
您可以选择您想要的一个并进行类型转换,sqrt((double)sample)
,或者您可能只想在开始时将样本声明为双精度。事实上,您可能希望所有 int
都是 double
。
编辑
您需要在 scanf("%lf", &error);
中的 error
之前添加一个 &
另外我认为您需要从 scanf()
格式中删除所有 \n
。
感谢大家对我的帮助。这是工作代码,如果有人想用它来查找置信区间中的错误和样本量。
#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;
}