I have been struggling with a certain error that doesn’t make sense to me. Whenever I try to compile this program, it tells me that I’m missing a semicolon when I am not.
It seems the error is linked to a specific block of code, that being the if statement that checks stock. Since I know c++ can be platform specific, I’m running debian 9 and the atom ide if that’s any help.
Here is the specifc error:
error: expected primary-expression before ‘,’ token
getline(string,line);//gets string`
and the code:
#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 commandsn check stock: checks store stockn enter stock: enter new stock itemsn";
cout << " exit: terminates the programn clean house: deletes all stockn";
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 itemn>> "; //item name
cin >> item;
cout << "enter amountn>> "; //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 << "33[1;31mplease use a proper command:33[0mn";
goto home;
}
return 0;
}
3
Answers
Are you missing this by any chance?
I believe it simply needs to be
getline(file,line)
rather thangetline(string,line)
and then you should be sorted.string
is recognized as a type name, of typestd::string
which you generously exposed by lineusing namespace std;
. This particular error message is caused by fact thatstring
isn’t an expression which can be evaluated . In context of your code it should bePS. Standard would say that you have to include
<string>
header to use componentstd::string
. Your code compiles thanks to an implementation-defined feature, was imported with<iostream>
in this version of header.