簡體   English   中英

C++:預期的主表達式

[英]c++: Expected primary-expression

我一直在為一個對我沒有意義的錯誤而苦苦掙扎。 每當我嘗試編譯此程序時,它都會告訴我缺少分號,而實際上卻沒有。

該錯誤似乎與特定的代碼塊有關,即檢查庫存的 if 語句。 由於我知道 c++ 可以是特定於平台的,因此我正在運行 debian 9 和 atom ide(如果有幫助的話)。

這是特定的錯誤:

錯誤:',' 標記前的預期主表達式

getline(string,line);//獲取字符串`

和代碼:

#include <iostream>
#include <fstream>


using namespace std;

int main ()
{
    cout << "store stocking system: \n"; // yadda yadda yadda UX
    cout << "commands: \n";
    cout << "  help: shows available commands\n  check stock: checks store stock\n  enter stock: enter new stock items\n";
    cout << "  exit: terminates the program\n  clean house: deletes all stock\n";
    home: // main loop in program
    string output;
    output = ">> ";
    cout << output;


    string stock;
    string item; // this whole block just defines things and gets input
    int itemNumber;
    string userInput;
    getline(cin,userInput);

    if (userInput == "exit")
    {
      return 0;
    }

    if (userInput == "enter stock")
    { // enters new stock
      cout << "enter item\n>> "; //item name
      cin >> item;
      cout << "enter amount\n>> "; //amount of item
      cin >> itemNumber;
      ofstream myfile; //makes file
      myfile.open("stock.txt"); //opens myfile
      myfile << "\n" << item << "," << itemNumber << "\n"; //writes to file
      myfile.close();// closes file
      cout << "done";
      goto home; //finishes and goes to main loop
    }

    if (userInput == "check stock") // where the problem is
    {
      string line;
      ifstream file("stock.txt");//checks fo file
      file.open("stock.txt");//opens file
      getline(string,line);//gets string
      file.close();//closes it
      cout << line << "\n";
      goto home;
    }

    if (userInput == ""){
      goto home;
    }

    else
    {
      cout << "\033[1;31mplease use a proper command:\033[0m\n";
      goto home;

    }
    return 0;
}    

你有沒有機會錯過這個?

#include <string>

我相信它只需要getline(file,line)而不是getline(string,line)然后你應該被排序。

string被識別為類型名稱,屬於std::string類型,您可以using namespace std;通過行慷慨地公開它using namespace std; . 此特定錯誤消息是由string不是可以計算的表達式這一事實引起的。 在您的代碼上下文中,它應該是

getline(file,line);

附注。 標准會說您必須包含<string>標頭才能使用組件std::string 由於實現定義的功能,您的代碼編譯,在此版本的頭文件中使用<iostream>導入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM