2016-12-06 49 views
-2

嘿傢伙我有一個關於我的代碼的問題。以下是我們必須做的: 「讓用戶閱讀文件,該文件的格式與網站上的」items.txt「格式相同 將始終是一個具有名稱和價格的項目列表如果一個 物品的配方不存在,製作該物品的唯一方法就是直接購買它。製作一個程序,讀取所有物品和配方 ,然後說明可以獲得多少利潤通過製作每件物品 如果一件物品沒有配方,您會購買該物品,然後以相同的價格轉售它,並從中獲利 0.如果物品確實有配方,您將購買材料以製作此物品並從最終產品的價格中扣除 的成本 每件商品只有零個或一個配方,這些商品將始終爲b首先列出。 項目的名稱將始終爲單個單詞(使用_加入通常爲多個單詞的名稱)。您 可以假設會有小於50項和每個配方將使用其他不到50個項目,以創建一個 最終產品。」字符串雙向轉換沒有sstream或boost詞法轉換

這是items1.txt我們使用

Item: Wood 2.5 
Item: Metal 5.5 
Item: Cat 900 
Item: Spear 50.7 
Recipe: Spear = Wood + Wood + Metal ; 

我有我認爲會工作,但我不能得到一個工作線,我試圖用stod,但顯然我的學校的電腦不支持它,我也嘗試了提高詞法演員,這也不起作用

它說「stod:沒有在這個範圍內聲明。

這裏是我的代碼:

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <string> 
#include <algorithm> 
#include <sstream> 

using namespace std; 
string nextstring(string str, int start_index); 
int split(string str, string a[], int max_size); 

int main() 
{ 
    ifstream in_stream; 

    string fileName; 
    cout << "Enter the file name : "; 
    cin >> fileName; 

    in_stream.open(fileName.c_str()); 

    //error checking 
    if (in_stream.fail()) 
    { 
     cout << "File could not be opened." << endl; 
     exit(1); 
    } 

    string items[50]; 
    double items_value[50]; 
    string recipe[50]; 
    string rname = recipe[0]; 
    double profit = 0; 
    int j = 0; 
    string lines; 
    int number_of_lines = 0; 

    while(getline(in_stream, lines)) 
    { 
     if(lines.substr(0,5) == "Item:") 
      { 
       int beginning = lines.find_first_of(' ') + 1; 
       int next_space = lines.find(" ", beginning); 
       items_value[j] = stod(lines.substr(next_space)); 
       items[j] = lines.substr(beginning,lines.find_first_of(' ', beginning) - beginning); 
       j++; 
      } 

     if(lines.substr(0,7) == "Recipe:") 
     {   
      int max_size = lines.length(); 
      int cnt = split(lines,recipe,max_size); 
      double profit1 = 0; 
      double profit2 = 0; 
      for(int j = 3; j < cnt; j++) 
      { 
       for(int i = 0; i < 4; i++) 
        { 
         if((recipe[j] == items[i]) && (recipe[j] != "+")&& (recipe[j] != ";")) 
         { 
         cout << "Making " << items[i] << ", " << "profit = 0" << endl;   
         profit1 += items_value[i];      
         } 
         if(recipe[1] != items[i]) 
         { 
         profit2 = 0; 
         } 
        } 
      } 
      for(int i = 0; i < cnt; i++) 
      { 
       if((recipe[1] == items[i])) 
       { 

        profit = items_value[i]; 
        cout << "Making " << items[i] << ", " << "profit = "; 
       } 

      } 
      cout << profit - profit1 << endl; 


     } 
    } 
    in_stream.close(); 
    return 0; 
} 

string nextstring(string str, int start_index) 
{ 

    int y =0; 
    y = str.find(' ',start_index); 
    y = y-start_index; 
    str = str.substr(start_index,y);  
    return str; 

} 

int split(string str, string a[], int max_size) 
{ 
    int i; 
    int num = 0; 
    for (i=0; i<max_size; i++) 
    { 

     a[i] = nextstring(str,num); 
     num = num + a[i].length() + 1; 

     if(num >= str.length()) 
     { 
      i++; 
      break; 
     } 
    } 

    return i; 
} 
+0

嘗試啓用C++ 11(檢查你的編譯器/ IDE的文檔,瞭解如何做到這一點)。爲了將來的參考,請減少不必要的絨毛,這個問題中幾乎所有的東西都不是真的需要證明這個問題。這裏有更多的信息([問])和這裏([mcve])。 –

回答

2

第一步是從本世紀得到一個體面的編譯器;)stod一直以來C++ 11,這實際上意味着它之前,可能在幾年可用可用。

如果stod不適用於您,則可以恢復到cstdlib函數atof。