最近又开始学编程了。我是初学者。不久前我上了一堂课,但现在我正在尝试编译和运行我在闪存上的一个程序,该程序在使用 Dev C++ 的课堂上运行良好。我现在在家使用最新版本的 Code::Blocks。
下面是一个简单的计算器程序的程序代码:
/* This program adds, subtracts, multiplies, and divides two integers. */
#include <stdio.h>
#include <stdlib.h>
// Function Declarations
int getOption(void);
void getData (int* a, int* b);
float calc (int option, int num1, int num2);
float add (float num1, float num2);
float sub (float num1, float num2);
float mul (float num1, float num2);
divn (float num1, float num2);
void printResult (float num1, float num2, float result, int option);
int main (void)
{
// Local Declarations
int done = 0;
int option;
int num1;
int num2;
int result;
// Statements
while (!done)
{
option = getOption();
if (option == 5)
done = 1;
else
{
do
{
printf("\n\nEnter two numbers: ");
scanf("%f %f", &num1, &num2);
if (option == 4 && num2 == 0)
{
printf("\a\n *** Error *** ");
printf("Second Number cannot be 0\n");
} //if
} while (option == 4 && num2 == 0);
switch (option)
{
case 1: result = add (num1, num2);
break;
case 2: result = sub (num1, num2);
break;
case 3: result = mul (num1, num2);
break;
case 4: result = divn (num1, num2);
} // switch
printResult (num1, num2, result, option);
} // else option != 5
} // while
printf("\nThank you for using Calculator.\n");
return 0;
} // main
/* ========================= getOption ===================================
This function shows a menu and reads the user option.
Pre nothing
Post returns a valid option */
int getOption (void)
{
// Local Declarations
int option;
// Statements
do
{
printf("\n******************");
printf("\n* Menu *");
printf("\n* *");
printf("\n* 1. ADD *");
printf("\n* 2. SUBTRACT *");
printf("\n* 3. MULTIPLY *");
printf("\n* 4. DIVIDE *");
printf("\n* 5. QUIT *");
printf("\n* *");
printf("\n******************");
printf("\n\n\nPlease type your choice ");
printf("and press the return key : ");
scanf("%d", &option);
if (option < 1 || option > 5);
printf("Invalid option. Please re-enter.\n");
} while (option < 1 || option > 5);
return option;
} // getoption
尝试编译时出现以下构建错误:
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `add'|
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `sub'|
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `mul'|
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `divn'|
C:\Users\Christopher\SkyDrive\School\Programming\Practice Stuff\complete calculator.o:complete calculator.c|| undefined reference to `printResult'|
||=== Build finished: 5 errors, 0 warnings (0 minutes, 0 seconds) ===|
我相信错误不是因为代码错误(我知道代码之前工作过),而是因为我现在使用的是 Code::Blocks 而不是 Dev C++,所以我需要引用一个不同的库,但不知道我需要哪个库。
帮助将不胜感激。