C++ 无法检测导致语法错误的行
你好!我正在学习一门编程课程中的一个老项目,在跟踪代码中哪一行导致语法错误时遇到了一些问题 当我试图编译代码时,Visual Studio告诉我,在包含此函数调用的行的main结尾处有一个语法错误: sortData(carray、numRec、sortby) 我认为情况并非如此,因为注释掉该调用只会将错误移到下一行代码 我不知道是什么导致了这个错误,我希望一个老练的眼睛能帮上忙 我已经包含了所有代码,因为我怀疑错误是由一个函数调用引起的 奥比胡安 还有,有人知道编译器是自上而下编译的,还是在编译过程中它实际上遵循函数调用吗C++ 无法检测导致语法错误的行,c++,syntax,C++,Syntax,你好!我正在学习一门编程课程中的一个老项目,在跟踪代码中哪一行导致语法错误时遇到了一些问题 当我试图编译代码时,Visual Studio告诉我,在包含此函数调用的行的main结尾处有一个语法错误: sortData(carray、numRec、sortby) 我认为情况并非如此,因为注释掉该调用只会将错误移到下一行代码 我不知道是什么导致了这个错误,我希望一个老练的眼睛能帮上忙 我已经包含了所有代码,因为我怀疑错误是由一个函数调用引起的 奥比胡安 还有,有人知道编译器是自上而下编译的,还是在编
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
//customer struct
struct customer
{
int id;
string fname, lname;
double totalQuantity, totalPurchases, totalProfit;
};
//Error Codes
const int INPUT_FILE_FAIL_NO_CONTINUE = 2;
const int INPUT_FILE_DATA_ERROR = 3;
//Global Constants
const int arraySize = 200;
const int numType = 5; //# of the types of coffee we currently offer
//Coffee prices per pound
const double colombWhole = 3.75;
const double colombRetail = 4.85;
const double konaWhole = 4.25;
const double konaRetail = 5.25;
const double ethiopWhole = 4.30;
const double ethiopRetail = 5.75;
const double jamaWhole = 5.25;
const double jamaRetail = 7.75;
const double brazWhole = 4.65;
const double brazRetail = 5.90;
//Function prototypes
int readData (ifstream &infile, customer carray[], int size);
//PRE: The address of the ifstream object, the addres of the array of customer structs and the size of
// the array is passed in
//POST: All of the customer data is read in AND the totalQuantity, totalPurchases and totalProfit for
// each customer is calculated. The number of records read is returned.
void sortData (customer carray[], int recordcount, int sortfield);
//PRE: The address of the array of customers, the number of customer records in the array and the field
// on which to be sorted is passed into the function.
//POST: The array is sorted on the basis of the specified field in ascending order
int findMax (const customer carray[], int startRange, int recordcount, int sortfield);
//PRE: The address of the array of customers, the field to sort on and a range of values is passed in
//POST: The address of the largest value is returned
int main()
{
//Array of customer structs
customer crecords[arraySize];
//Initialize the members of our struct to zero
for (int i = 0; i < arraySize; i++)
{
crecords[i].totalProfit = 0;
crecords[i].totalPurchases = 0;
crecords[i].totalQuantity = 0;
}
//Declare filestream objects
ifstream ifile;
ofstream ofile1, ofile2, ofile3;
//user responses
char pquit = 'Y';
char specAnother;
int sortby;
string ifilename;
//Ask user for name of input file
cout << "Please enter the name of the input file: " ;
cin >> ifilename;
//Attempt to open the input file
ifile.open(ifilename.c_str());
while (ifile.fail())
{
cout << endl << "The input file could not be found. Would you like to specify "
<< "another file?" << endl << "(Enter Y or N):";
cin >> specAnother; //Ask user if they want to specify another
if (specAnother == 'N' || specAnother == 'n')
{
exit(INPUT_FILE_FAIL_NO_CONTINUE);
}
else
{
cout << endl << "Please enter the name of the new input file: " ;
cin >> ifilename;
}
ifile.clear(); //Clear the flags, else the input file fail flag will perpetually return true
ifile.open(ifilename.c_str());
}
//File opened successfully, let's begin reading in data and also keep track of the # of
//records read
int numRec = readData(ifile, crecords, arraySize);
cout << "Finished reading " << numRec << " records" << endl;
do
{
//Ask user how they would like to sort the data for the report
cout << endl << "What would you like to sort on?" << endl;
cout << "1) Sort by POUNDS bought" << endl
<< "2) Sort by PURCHASE AMOUNT" << endl
<< "3) Sort by PROFIT" << endl;
cin >> sortby;
if (sortby > 3 || sortby < 1)
{
cout << "You entered an invalid sorting method, try again" << endl
<< "Enter an option:";
cin >> sortby;
}
cout << "You entered " << sortby << endl;
}
//Sort Data
sortData(carray, numRec, sortby);
return 0;
}
//Function Definitions
int readData (ifstream &infile, customer carray[], int size)
{
int x = 0, coffeeType, quantity;
double currentSale, internalCost, profitOnSale;
while (infile >> carray[x].id && x < size)
{
infile >> carray[x].fname >> carray[x].lname;
while (infile >> coffeeType && coffeeType != 0)
{
infile >> quantity;
switch(coffeeType)
{
case 1:
carray[x].totalQuantity += quantity;
currentSale = quantity * colombRetail;
carray[x].totalPurchases += currentSale;
internalCost = quantity * colombWhole;
profitOnSale = currentSale - internalCost;
carray[x].totalProfit += profitOnSale;
break;
case 2:
carray[x].totalQuantity += quantity;
currentSale = quantity * konaRetail;
carray[x].totalPurchases += currentSale;
internalCost = quantity * konaWhole;
profitOnSale = currentSale - internalCost;
carray[x].totalProfit += profitOnSale;
break;
case 3:
carray[x].totalQuantity += quantity;
currentSale = quantity * ethiopRetail;
carray[x].totalPurchases += currentSale;
internalCost = quantity * ethiopWhole;
profitOnSale = currentSale - internalCost;
carray[x].totalProfit += profitOnSale;
break;
case 4:
carray[x].totalQuantity += quantity;
currentSale = quantity * jamaRetail;
carray[x].totalPurchases += currentSale;
internalCost = quantity * jamaWhole;
profitOnSale = currentSale - internalCost;
carray[x].totalProfit += profitOnSale;
break;
case 5:
carray[x].totalQuantity += quantity;
currentSale = quantity * brazRetail;
carray[x].totalPurchases += currentSale;
internalCost = quantity * brazWhole;
profitOnSale = currentSale - internalCost;
carray[x].totalProfit += profitOnSale;
break;
default:
cout <<"The input file contains an undeclared coffee type at record " << x
<<"Program terminating!" << endl;
//return exit(INPUT_FILE_DATA_ERROR);
}
}
x++; //At this point, we have encountered our sentinel value of 0 that indicates the end of our
//customer record. Let's move on to the next customer.
}
return x;
}
int findMax (const customer carray[], int startRange, int recordcount, int sortfield)
{
int maxLoc = startRange;
switch(sortfield)
{
case 1:
for (int i = startRange + 1; i <= recordcount; i++)
{
if (carray[maxLoc].totalQuantity < carray[i].totalQuantity)
maxLoc = i;
}
case 2:
for (int i = startRange + 1; i <= recordcount; i++)
{
if (carray[maxLoc].totalPurchases < carray[i].totalPurchases)
maxLoc = i;
}
case 3:
for (int i = startRange + 1; i <= recordcount; i++)
{
if (carray[maxLoc].totalProfit < carray[i].totalProfit)
maxLoc = i;
}
}
return maxLoc;
}
void sortData (customer carray[], int recordcount, int sortfield)
{
for (int i = 0; i < recordcount ; i++)
{
int max = findMax(carray, i, recordcount, sortfield);
swap(carray[i], carray[max]);
}
}
#包括
#包括
#包括
#包括
使用名称空间std;
//客户结构
结构客户
{
int-id;
字符串fname,lname;
双倍总数量、总采购量、总利润;
};
//错误代码
const int INPUT\u FILE\u FAIL\u NO\u CONTINUE=2;
常量int输入文件数据错误=3;
//全局常数
常数int arraySize=200;
常量int numType=5;/#我们目前提供的咖啡种类
//每磅咖啡的价格
常数双整=3.75;
常数=4.85;
const-double-konaWhole=4.25;
const-double-konaRetail=5.25;
const-double-ethiopWhole=4.30;
常数双埃塞俄比亚零售=5.75;
常数为5.25;
常数=7.75;
常数为4.65;
常数为5.90;
//功能原型
int readData(ifstream&INFLE,客户携带[],int大小);
//PRE:ifstream对象的地址、客户结构数组的地址和
//数组被传入
//POST:读取所有客户数据,并显示其总数量、总购买量和总利润
//每个客户都经过计算。返回读取的记录数。
作废排序数据(客户carray[],内部记录计数,内部排序字段);
//PRE:客户阵列的地址、阵列中的客户记录数和字段
//将要排序的对象传递到函数中。
//POST:数组根据指定字段按升序排序
int findMax(const customer carray[],int startRange,int recordcount,int sortfield);
//PRE:传入客户数组的地址、要排序的字段和一系列值
//POST:返回最大值的地址
int main()
{
//客户结构数组
客户凭证[排列];
//将结构的成员初始化为零
for(int i=0;iifilename;
//尝试打开输入文件
ifile.open(ifilename.c_str());
而(ifile.fail())
{
cout您的问题是您有以下问题:
do
{
// ...
}
没有尾随的while(…);
调用sortDatawhilewhile子句的do..while语句?do{…}
在末尾需要一个条件,例如do{…}while(busy)如果您只想在块中执行一段代码,只需将该块放在那里并删除do
您的do{}在括号后缺少while子句,这将导致语法错误
我还注意到另一件事——findMax switch语句缺少一个默认标记,也缺少break语句。不确定这是否是您的意图,但通常不是大多数开关的情况。您的while after do在哪里
do {
// something
} while(condition);
这是正确的语法谢谢Richie!我简直不敢相信我错过了。该死!我想我要回去睡觉了!@obijuan:还有一件事。在函数findMax()中,案例缺少break;语句。一般来说:错误的原因通常是前一个(非空)行。谢谢Kaleb。我又错过了一次机会——这就是我在感冒药的影响下编写代码的收获:)