2010-10-15 135 views
0

我已經編寫了這個程序,但是當我嘗試編譯它時,我得到一個語法錯誤。 I 找不到語法錯誤的位置。C++程序指令

它應該計算不同氣體中第二次聲音傳播的次數,用戶給出的信息。

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

    int main() 
     { 
      int choice, gascount=0,i,sec; 
      string gas[10],type; 
      double speed[10],speedd; 
      ifstream input; 
    input.open("input.txt"); 
    if(input.fail())    
      cout<<"file did not open please check it\n"; 
      cin >> gas[gascount++]; 
     while(input) 
      { 
       input>>speed[gascount]; 
       input>>gas[++gascount]; 
      } 
      while(choice!=5) 
       {cout<<"Choose gas would you like to use\n"; 
       cout<<"1 CarbonDioxide\n"; 
       cout<<"2 Air\n"; 
       cout<<"3 Helium\n"; 
       cout<<"4 Hydrogen\n"; 
       cout<<"5 Exit\n"; //5th cout for exiting program 
       cin >>choice; 

    switch(choice) //use swich for user selection of gases 
     {case 1: type="CarbonDioxide"; 
        break; 
     case 2: type="Air"; 
        break; 
     case 3: type="Helium"; 
        break; 
     case 4: type="Hydrogen"; 
        break; 
     case 5: system("exit"); 
     default: printf("Illegal input: Try Again\n"); 
     } 
      i=0; 
      for(i=0;i<gascount;i++)   //loop for number of seconds 
      if(type.compare(gas[i])==0) //speed travel in gases 
       {speedd=speed[i]; 
       i=gascount+1; 
       } 
     cout<<"You chose "<<type<<endl; 
     cout<<"how many seconds did the sound travel? "; 
     cin>>sec; 
     while(sec<0||sec>30) 
     {cout<<"must me between 0 and 30\n"; 
      cout<<"how many seconds did the sound travel? "; 
      cin>>sec; 
     } 
      cout<<"The speed of sound through "<<type<<" is "<<speedd*sec*10.<<endl; 
    } 
    input.close(); 
    system("pause"); 

return 0; 

} 
+2

告訴我們的錯誤,我們會幫你破譯它們。 – 2010-10-15 05:11:25

+0

只有當你可以問這之前http://stackoverflow.com/questions/3929826/writing-a-program-in-c-closed這,你會節省很多時間和負面評級。 – 2010-10-15 05:59:31

回答

2

我可以看到2個問題:

在第一行#include之前缺失(可能是筆誤)

您正在使用system功能,但不包括stdlib,你需要a

#include <cstdlib> 
5

您的編譯器有錯誤消息。您需要查看這些消息並逐個修復它們。如果您無法找到我們轉貼的內容。

+1

雖然通常是一個很好的觀點,但在這種情況下,相當多的編譯器可能會發出相當無益的錯誤消息。 – 2010-10-15 04:45:59

+4

成爲一名優秀的c/C++編碼人員的過程的一部分就是了解編譯器試圖告訴你什麼。 – rerun 2010-10-15 04:51:51

0

修復@codaddict指出的問題後,您可能還想要#include <string>,因爲您正在使用std::string

雖然它不直接相關,但您也應該更有意義地縮進代碼。

看起來你在初始化之前也使用choice

0

是的,你可能缺少

#include <cstdlib> // system() defined here 
#include <string> // std::string here 
  • 不要錯過,包括所需的頭部。
  • 處理您的縮進。
  • choice沒有問題,但在定義點初始化它會是一種很好的風格。
  • 考慮到警告過

    prog.cpp:40: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result

    prog.cpp:60: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result

    prog.cpp:11: warning: ‘speedd’ may be used uninitialized in this function

0

#include <stdlib>#include <stdio>可以幫助...

+0

ITYM''和''? – 2010-10-15 07:57:44

0

剛加#include <string>和你的程序將完美編譯:)

+0

你不能編譯你的程序,因爲'operator >>'是在'string'頭文件中定義的。 – 2010-10-24 17:33:26