错误预期`;'之前

时间:2013-09-03 07:28:36

标签: c++

我似乎无法弄清楚为什么它一直在说';'在“保持窗口打开”之前,仅在第28行请帮助

#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;  
} 

2 个答案:

答案 0 :(得分:3)

在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)在while表达式之后需要使用分号。

答案 1 :(得分:0)

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

变化:

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

为:

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

......看看事情是否变得更好。