函数未提供有效输出(Visual Studio 2010上的C)
我曾尝试将我在电子方面所做的微积分自动化。不幸的是,我的代码似乎不起作用,因为所有结果都是0.000000(见下文) 以下是我尝试过的: -双精度或整型变量 -移除微积分部分并尝试从输入中获取输出 -扫描和扫描 这两个都不起作用。奇怪的是,最后的printf甚至不能打印base_v(例如) 下面是代码本身:函数未提供有效输出(Visual Studio 2010上的C),c,printf,calculator,C,Printf,Calculator,我曾尝试将我在电子方面所做的微积分自动化。不幸的是,我的代码似乎不起作用,因为所有结果都是0.000000(见下文) 以下是我尝试过的: -双精度或整型变量 -移除微积分部分并尝试从输入中获取输出 -扫描和扫描 这两个都不起作用。奇怪的是,最后的printf甚至不能打印base_v(例如) 下面是代码本身: #include "stdafx.h" #include "stdio.h" #include "stdlib.h" #include "math.h" int _tmain(int ar
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
int _tmain(int argc, _TCHAR* argv[])
{
// variable declaration
double base_r, mod_r, base_v, mod_v=0;
double coeff=1;
int asdf=1;
// welcome message
printf("vMod calculator\n");
printf("Please enter resistances in Ohms and voltages in Volts\n");
// retrieve info about vmod
printf("Enter base resistance\n");
scanf_s("%f", &base_r);
printf("Enter modified resistance\n");
scanf_s("%f", &mod_r);
printf("Enter base voltage\n");
scanf_s("%f", &base_v);
// calculus of modified voltage
coeff=asdf*((1/base_r)+(1/mod_r));
mod_v=base_v*base_r*coeff;
// results
printf("Base voltage: %f \n", &base_v);
printf("Base resistance: %f \n", &base_r);
printf("Modified voltage: %f \n", &mod_v);
printf("Modified resistance: %f \n", &mod_r);
system("pause");
return 0;
}
如果您不理解微积分,请参考下图:
谢谢您传递的是
printf
参数&base\u v
的地址,而不是它的值base\u v
。简而言之,摆脱&
printf("Base voltage: %f \n", base_v);
printf("Base resistance: %f \n", base_r);
printf("Modified voltage: %f \n", mod_v);
printf("Modified resistance: %f \n", mod_r);
您传递的是
printf
参数&base\u v
的地址,而不是它的值base\u v
。简而言之,摆脱&
printf("Base voltage: %f \n", base_v);
printf("Base resistance: %f \n", base_r);
printf("Modified voltage: %f \n", mod_v);
printf("Modified resistance: %f \n", mod_r);
首先,您将指针打印为浮点数,因此具有未定义的行为。首先,您将指针打印为浮点数,因此具有未定义的行为。
printf
直接获取变量,而不是其地址:
printf("Base voltage: %f \n", base_v);
printf("Base resistance: %f \n", base_r);
printf("Modified voltage: %f \n", mod_v);
printf("Modified resistance: %f \n", mod_r);
printf
直接获取变量,而不是其地址:
printf("Base voltage: %f \n", base_v);
printf("Base resistance: %f \n", base_r);
printf("Modified voltage: %f \n", mod_v);
printf("Modified resistance: %f \n", mod_r);
scanf在第一次调用后将换行符0x13和0x10保留在stdin缓冲区中,所以对于dos/windows,下一次调用将首先获得13/10 使用fgets,然后使用fscanf或类似的东西 而((dumchar=getch())==\n);
要在每次scanf()调用之前清除换行符,scanf会在第一次调用后在stdin缓冲区中保留换行符0x13和0x10,因此对于dos/windows,下一次调用将首先获得13/10 使用fgets,然后使用fscanf或类似的东西 而((dumchar=getch())==\n);
为了在每次scanf()调用之前清除换行符我尝试不使用
&
,现在我得到了四个“-92559604647977569000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000。也许您可以在scanf_s
调用中使用float或%lf
来尝试这一点。谢谢我尝试不使用&
,现在我得到了四个“-92559604647977569000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000。也许您可以在scanf_s
调用中使用float或%lf
来尝试这一点。谢谢