2015-06-03 238 views
-1
#include "std_lib_facilities.h" 

int main() 
{ 
constexpr double euro_to_dollar = 1.11; 
constexpr double yen_to_dollar = 0.0081; 
double dollar = 1.00; 

char unit= 'A'; 
cout <"Please enter a value followed by e or y: \n"; 
cin >>dollar>> unit; 
if (unit= 'e') 
cout << dollar << "Euro == " << euro_to_dollar*dollar << "dollar\n"; 
else if (unit='y') 
     cout << dollar << "Yen== " << yen_to_dollar * dollar << "dollar\n"; 
} 
 
5 error: 'constexpr' was not declared in this scope 
5 error: expected ';' before 'double' 
7 error: expected ';' before 'double' 
15 error: 'euro_to_dollar' was not declared in this scope 

17 error: 'yen_to_dollar' was not declared in this scope 

我做從編程的問題:原則和由Bjarne Stroustrup的練習使用C++(第二版)。我可以看到我在這裏做錯了什麼。我試圖學習C++,所以我基本上是一個初學者。我很感謝幫助的人。編譯C++錯誤

+0

您正在使用什麼編譯器?它支持C++ 11嗎? –

+0

'cout <「請輸入一個值後跟e或y:\ n」;'應該是'cout <<' –

回答

1

constexpr關鍵字是在C++ 11中引入的,用-std=c++11進行編譯。
實施例:

與克++:g++ main.cpp -o program.exe -std=c++11

與代碼::塊:
設置 - >編譯器 - >編譯器設置 - >編譯器標誌 - >勾選框Have g++ follow the C++ ISO C++ language standard - >確定


您還賦值給一個變量在你的if語句,與==取代:

if (unit= 'e') 
    //^

else if (unit='y') 
     // ^

而你錯過了<在調用到std ::法院:

cout <"Please enter a value followed by e or y: \n"; 
//^
1

我不知道還有什麼在你的本地頭文件

#include "std_lib_facilities.h" 

代替我在代碼中添加了以下幾行代碼

#include<iostream> 

using namespace std; 

if (unit = 'e') //做分配 應該糾正到如下

如果(單元== 'E'),//檢查等於或不

else if (unit ='y')應該糾正到由於相同的原因

如下
else if (unit =='y') 

而且你應該編譯使用編譯器選項-std=c++11