Ada*_*m G 0 c++ debugging goto
我的功能是在使用goto时给我"在'}'令牌之前预期的主要表达",我不知道为什么.
在我将它放入函数之前,这段代码按原样运行.
当我用'break'替换'goto'时它会起作用,但我需要知道为什么会这样.
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:
}
Run Code Online (Sandbox Code Playgroud)
问题是标签要标记一个语句.换句话说,如果没有声明,您就不能拥有标签.
有了我的评论的警告,你可以通过在标签后面有一个空的"null"语句来解决它:
skip_data_input_loop: /* Empty statement using the semicolon */ ;
Run Code Online (Sandbox Code Playgroud)