尝试运行我的代码以查看我是否对自己的分配有任何权利,但是在尝试编译该代码时始终收到错误消息,代码如下。如果有人意识到我什至没有正确执行代码的某些部分,请告诉我,因为我不太确定。
当我尝试在Xcode上编译时,我得到的错误是
“ / clang:-1:链接器命令失败,退出代码为1(使用-v查看调用)”
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
// the format of each of the elements in the database
struct OpAmps {
char Name[20]; // the name of the op-amp (e.g. "741")
unsigned int PinCount; // the number of pins in the package
double SlewRate; // the slew rate in volts per microsecond
};
// the length of the fixed array to be used for database - must be at least one
// and no greater the maximum value allowed in an unsigned long (see the file
// limits.h).
#define DATABASE_MAX 10
// file used for the database
#define DATABASE_FILENAME "database.txt"
// function prototypes
void Enter ( OpAmps& OpAmp , unsigned long& length);
void Save ( OpAmps DB[], unsigned long length);
void Load (OpAmps DB[], unsigned long length);
void Display ( OpAmps DB[], unsigned long length);
// Control the entering, saving, loading, sorting and displaying of elements in the
// database.
// Arguments: None
// Returns: 0 on completion
int main()
{
OpAmps OpAmp[DATABASE_MAX]; // the database
unsigned long database_length = 0; // the number of elements in the database
char UserInput;
// loop until the user wishes to exit
while (1) {
// show the menu of options
cout << endl;
cout << "Op-amp database menu" << endl;
cout << "--------------------" << endl;
cout << "1. Enter a new op-amp into the database" << endl;
cout << "2. Save the database to disk" << endl;
cout << "3. Load the database from disk" << endl;
cout << "4. Sort the database" << endl;
cout << "5. Display the database" << endl;
cout << "6. Exit from the program" << endl << endl;
// get the user's choice
cout << "Enter your option: ";
cin >> UserInput;
cout << endl;
// act on the user's input
switch(UserInput) {
case '1':
Enter(OpAmp[database_length], database_length);
break;
case '2':
Save(OpAmp, database_length);
break;
case '3':
Load(OpAmp, database_length);
break;
/* case '4':
Sort(OpAmp, database_length);
break;
*/
case '5':
Display(OpAmp, database_length);
break;
case '6':
return 0;
default:
cout << "Invalid entry" << endl << endl;
break;
}
}
}
// Allow the user to enter a new element into the database. Note that the data is
// simply added to the end the database (if not full) and no sorting is carried
// out.
// Arguments:
// (1) the element in the database to be entered
// (2) the position of the element in the database
// Returns: void
void Enter ( OpAmps& OpAmp , unsigned long& length)
{
// if the database is full, inform the user
if (length >= DATABASE_MAX )
{
cout << "Database is full." << endl;
}
// if the database is not full, get the data from the user and alter the database
// length
else
{
cout << "Enter OpAmp Name: ";
cin >> OpAmp.Name;
cout << "Enter Pin Count: ";
cin >> OpAmp.PinCount;
cout << "Enter Slew Rate: ";
cin >> OpAmp.SlewRate;
length++;
}
}
// Save the database to the file specified by DATABASE_FILENAME. If the file
// exists it is simply overwritten without asking the user.
// Arguments:
// (1) the database
// (2) the length of the database
// Returns: void
void Save (OpAmps DB[], unsigned long length)
{
fstream output_file; // file stream for output
// open the file
output_file.open(DATABASE_FILENAME, ios::out);
if (!output_file.good()) {cout <<"Error"; exit(1);}
// write length information to file
output_file << length << endl;
// write data to file
for (int i = 0; i < length; i++)
{
output_file << DB[i].Name << DB[i].PinCount << DB[i].SlewRate;
}
// close the file
output_file.close();
}
// Load the database from the file specified by DATABASE_FILENAME. If the file
// exists it simply overwrites the data currently in memory without asking
// the user.
// Arguments:
// (1) the database
// (2) the length of the database
// Returns: void
void Load (OpAmps DB[], unsigned long length)
{
fstream input_file; // file stream for input
// open the file
input_file.open(DATABASE_FILENAME, ios::in);
if (!input_file.good()) {cout <<"Error"; exit(1);}
// load database length information from file
for (int i = 0; i < length; i++)
{
input_file >> length;
}
// load data from file
for (int i = 0; i < length; i++)
{
input_file >> DB[i].Name >> DB[i].PinCount >> DB[i].SlewRate;
}
// close the file
input_file.close();
}