2015-02-05 27 views
0

試圖將(回)C++並通過書中的練習,我得到一個奇怪的錯誤。添加簡單的cout後,程序不會運行

使用代碼::塊+ GCC

這是當前程序。

#include "std_lib_facilities.h"; 
    int main() 
    { 

    double val, smallest = 9999, largest = 0; 
    string strunit; 
     for (int i = 0; i < 10; i++) { 
      cout << "Enter value: "; 
      cin >> val >> strunit; 

      if (strunit == "in") { 
        val = val*0.254; 
      } else if (strunit == "cm") { 
       val = val/100; 
      } 
      if (val > largest) { 
       largest = val; 
      } else if (val < smallest) { 
       smallest = val; 
      } 
      cout << "Largest: " << largest << "smallest: " << smallest << endl; 

     } 

    return 0; 
} 

我打算使用while循環,但現在它是一個for。但結果是一樣的。

這是構建結果:

-------------- Build: Debug in drill_4 (compiler: GNU GCC Compiler)--------------- 

mingw32-g++.exe -o bin\Debug\drill_4.exe obj\Debug\exe\principles_and_practices\drill_4\main.o 
Process terminated with status 0 (0 minute(s), 0 second(s)) 
0 error(s), 0 warning(s) (0 minute(s), 0 second(s)) 

的IDE聲稱該計劃尚未建成,並要求我重建。

如果我註釋掉程序將運行的幾乎任何行。

我最初使用了一行if語句,結果相同。

我對IDE也很陌生,所以不確定在哪裏可以找到我需要的所有信息,但我想pgrogram鏈接不正確?

任何想法我做錯了什麼?

+3

你可以嘗試從命令行運行gcc,而無需通過CodeBlocks? – Beta

+0

這是完整的代碼..我不知道在這裏做什麼「std_lib_facilities.h」,但是當我運行這個代碼時,包括這就是沒有問題的運行.. – mhs

回答

2

使用try #include<iostream>using namespace std;

+3

如果它來自Stroustrup的演習頭,它已經在通過['std_lib_facilities.h'](http://www.stroustrup.com/Programming/std_lib_facilities.h),包括使用命名空間std;'的可惡的頭部宿主'。 – WhozCraig

+0

是的,但他的頭似乎沒有工作。他可能缺少一些文件。只是爲了讓他可以讓他的代碼運行,我會建議#include ,#include ,並使用命名空間std;一旦這些被添加,代碼爲我工作得很好。 –

1

COUT是在iostream頭文件命名空間std後面。

編譯器不知道cout或cin的對象實例。

您也可以添加標頭並使用std :: cout。

#include <iostream> 

..... 
std::cout << "hellow world"; 
2

爲我工作,當我鍵入此:

#include <iostream> 
#include<string> 
using namespace std; 


int main() 
{ 

double val, smallest = 9999, largest = 0; 
string strunit; 
    for (int i = 0; i < 10; i++) { 
     cout << "Enter value: "; 
     cin >> val >> strunit; 

     if (strunit == "in") { 
       val = val*0.254; 
     } else if (strunit == "cm") { 
      val = val/100; 
     } 
     if (val > largest) { 
      largest = val; 
     } else if (val < smallest) { 
      smallest = val; 
     } 
     cout << "Largest: " << largest << "smallest: " << smallest << endl; 

    } 
    system("pause"); 
} 

它不喜歡你的頭,包括出於某種原因..您可能需要下載一些額外的文件與VS使用它?你在使用Visual Studios嗎?

+0

試圖從命令行進行編譯,結果相同。現在我將使用單獨的包含。 –