[英]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.