您好,我一直收到一个错误信息,说我的函数,电阻和频率任务是未定义的。我对这是什么意思感到困惑。我附上了我的代码,希望可以被引导到正确的方向,因为这是难倒我。谢谢。我声明并定义了原型并调用了函数,所以我搞不懂为什么编译器不接受这些代码并进行编译。
#include <stdio.h>
void resistor (void);
void freqduty (float ra, float rb, float c); // Prototype function for frequency and duty cycle
int main (void)
{
int choice; // choice to run program from start menu or quit
while (1)
{
// Menu for entering resistor and capacitor values
printf("Welcome to the 555 Timer Frequency and Duty Cycle Calculator\n");
printf("Please enter two resistor values in between ");
printf("1 kOhms and 100 kOhms and a capacitor value\n\n");
printf("Menu\n\n");
printf("1. Continue\n");
printf("2. Exit\n");
scanf("%d",&choice);
switch (choice)
{
case 1: resistor();
break;
case 2: printf("Goodbye\n");
break;
default: printf("Wrong Choice. Enter again\n");
break;
}
void resistor (void);
float ra, rb, c; // Float variables for two resistor values and capacitor
while(1) // While loop to gather resistor A value
{
printf("Enter a value for Resistor Ra (kOhms): "); // First Resistor Value
scanf("%f", &ra);
if(ra < 1 || ra > 100) // Will repeat loop until value between 1 and 100 is entered
printf("Invalid selection, choose again.\n");
else
break; // breaks loop when valid data is entered
}
while(1)
{
printf("Resistor Rb (kOhms): "); // Second Resistor Value
scanf("%f", &rb);
if(rb < 1 || rb > 100) // Will repeat loop until value between 1 and 100 is entered
printf("Invalid selection, choose again.\n");
else
break; // breaks loop when valid data is entered
}
while(1)
{
printf("\nCapacitor (uF) : "); // Capacitor Value
scanf("%f", &c);
if(c <= 0)
printf("Invalid selection, choose again.\n");
else
{
freqduty (ra, rb, c); // call function to compute frequency and duty cycle
break; // break while loop and restart program from menu selection screen
}
break;
}
void freqduty (float ra, float rb, float c) // function to compute frequency and duty cycle
{
float freq, dutycyc, add; // Float variables for frequency and duty cycle equations
freq = 2.0 * rb;
add = (1.44 / (ra + ((2.0 * rb) * c)));
dutycyc = (rb / (ra + (2.0 * rb)));
printf("\nThe frequency for these values entered is %.2f and the duty cycle");
printf(" is %.2f \n\n", freq , dutycyc);
printf("%f %f %f", ra, rb, c);
}
}
return 0;
}发布于 2020-09-24 13:26:57
您在文件范围内声明了这两个函数。您在函数main()中定义了两个函数。这意味着它们对于main()函数是局部的,并且在外部是不可见的。
而且,在使用它们的时候,它们还没有定义。
重新排列代码,它就有机会工作了。
我刚刚注意到,他们实际上在main()中定义了while(1),这是非常不寻常的。
https://stackoverflow.com/questions/64038323
复制相似问题