在";"之前应出错

error expected `;' before

本文关键字:出错      更新时间:2023-10-16

我似乎不明白为什么它一直在说期望的`;'在第28行"keep_window_open"之前,请帮助

#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
inline void keep_window_open(){char ch;cin>>ch;}

int main()
{
cout<<"Please enter your first name(followed by 'enter'):n";
string first_name;
cin>>first_name;
cout<<"hello,"<<first_name<<"!n";
keep_window_open();
return 0;
cout<<"please enter last name:n";
string Last_name;
cin>> Last_name;
cout<<"hello,"<<first_name<<Last_name<<"!n"
// this is the only keep_window_open() function that gives me the problem 
keep_window_open();
return 0;  
} 

在c++中,您的语句应该以; 结尾

cout<<"hello,"<<first_name<<Last_name<<"!n"

语句应以; 结尾

cout<<"hello,"<<first_name<<Last_name<<"!n";

以下是需要终止的语句列表

Statement type        Termination required?
==============        =====================
labelled statement              N (a)
expression                      Y
compound statements             N (a)
selection statements            N (a)
iteration statements            N (a) (b)
jump statements                 Y
declaration statement           Y

(a) 尽管有时可能会出现以分号结尾的情况,但事实并非如此。声明:

if(i==1)doSomething();使用分号终止内部表达式语句,而不是复合语句,当您检查上面的第一个包含在{}大括号中的代码段时,这一点应该很明显。

(b) do需要while表达式后面的分号。

因为您之前忘记了行上的;cout,末尾没有;):

更改:

cout<<"hello,"<<first_name<<Last_name<<"!n"

至:

cout<<"hello,"<<first_name<<Last_name<<"!n";

看看情况是否没有好转。