2013-03-24 66 views
-2

为什么这段代码不工作?该错误消息:main.cpp中:147:5:错误:预期 ';' 前 '鳍'C++字符串连接解决方​​案

string file; 
    ifstream fin; 
    fin.clear(); 

    cout << "\n\t--------------------------------Enter Person's name then surname to display------"; 
    cin>>file; 

    file +=".txt" 
    fin.open(file.c_str()); 

    char word[50]; 
    fin>>word; 
    while(fin.good()){ 


     cout << word << " "; 
     fin >> word; 
    } 


    system("pause"); 
    return 0; 

} 
+0

哪一个是147行?好像有很多你没有显示的代码 – 2013-03-24 00:30:31

+1

错误说明了这一切:你在第146行忘了';'(因为错误在第147行)。咩。 – syam 2013-03-24 00:31:35

+5

错误消息实际上告诉你**完全**错误是什么。 – 2013-03-24 00:31:43

回答

1

那么你错过了;file +=".txt"

string file; 
ifstream fin; 
fin.clear(); 

cout << "\n\t--------------------------------Enter Person's name then surname to display------"; 
cin>>file; 

file +=".txt"; 
fin.open(file.c_str()); 

char word[50]; 
fin>>word; 
while(fin.good()){ 


    cout << word << " "; 
    fin >> word; 
} 


system("pause"); 
return 0; 

}

2

你在该行的末尾需要分号:

file +=".txt" 

即应该修复错误。

+0

谢谢。我不知道为什么Netbeans将错误行显示为错误来源。 – 2013-03-24 00:34:54

+0

@ BaturayE.H.AHMAD Netbeans不是问题。缺少';'总是出现在下一行,而不是它实际上缺少的行。了解C++语法/解析器是如何工作的,这是有道理的。 – syam 2013-03-24 00:37:23

1

你缺少以下行分号。

file +=".txt" <-------- put a semi colon HERE 
    fin.open(file.c_str());