我的函数在使用 goto 时给出“'}' 标记之前的预期主表达式”,我不知道为什么。
这段代码在我将其放入函数之前在 main 中按原样工作。
当我将“goto”替换为“break”时它起作用了,但我需要知道这是为什么。
void fileInputLoop(ifstream& inputFile){
do{
cout << "Enter data file name: ";
getline(cin, fileName);
previousFileName = fileName;
// The user will press enter to exit data input
if(fileName == ""){
// If no file name is entered, exit this input loop
goto skip_data_input_loop;
}else{
// Check to see if input is an existing file
inputFile.open(fileName);
if(!inputFile.is_open()){
cout << "File is not available." << endl;
}else{
// FILE IS OPEN, DO SOMETHING WITH IT
ReadData(inputFile);
inputFile.close();
}
}
// If a second++ file is read in, the previous file will be set accordingly
// This is to track if a duplicate is from the same file or a new file
previousFileName = fileName;
}while(true);
skip_data_input_loop:
}
最佳答案
问题是标签是用来标记一个语句的。换句话说,您不能没有后面没有声明的标签。
根据我的评论,您可以通过在标签后添加一个空的“null”语句来解决它:
skip_data_input_loop: /* Empty statement using the semicolon */ ;
关于c++ - 为什么我的功能在使用 goto 时给我一个 "expected primary-expression before ' }' token"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52810452/